Avoid $ symbol while calling sqlplus in shellscript.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Avoid $ symbol while calling sqlplus in shellscript.
# 1  
Old 09-29-2017
Avoid $ symbol while calling sqlplus in shellscript.

Hi All,

we have requirement, i am created a shell script , inside i am connecting sqlplus and execute the query. below my code for your reference.

Code:
get_sqlid ()
{
sqlid=$(
  sqlplus -s $PBDW_USERID/$PBDW_PW@$PBDW_SID <<EOF
  DEFINE TBLNAME=$1
  set feedback off
  set serverout on size 1000000
  set verify off

  SELECT MAX(SQL.SQL_ID)
    FROM V$SQL SQL, V$SESSION SES
   WHERE SQL.SQL_ID = SES.SQL_ID
     AND SQL.PARSING_SCHEMA_NAME = SYS_CONTEXT('userenv', 'CURRENT_SCHEMA')
     AND SES.SCHEMANAME = SYS_CONTEXT('userenv', 'CURRENT_SCHEMA')
     AND SES.STATUS = 'ACTIVE'
     AND UPPER(SQL.SQL_FULLTEXT) LIKE '%' || '&&TBLNAME' || '%'
     AND SQL.SQL_FULLTEXT NOT LIKE '%V$SESSION%'
     AND TO_DATE(SQL.LAST_LOAD_TIME, 'YYYY/MM/DD HH24:MI:SS') BETWEEN
         TO_DATE(SQL.LAST_LOAD_TIME, 'YYYY/MM/DD HH24:MI:SS') - 0.15 / 48 AND
         TO_DATE(TO_CHAR(SYSDATE, 'YYYY/MM/DD HH24:MI:SS'),
                 'YYYY/MM/DD HH24:MI:SS');

EOF
)
wlog "${sqlid}"
                
}

---------------------------------------------

if i execute the script, V$SQL , V$SESSION table not available since it looks variable of $..

below error


Code:
20170929 06:34:17, ---- parsing table name : T_PBAMLGLOBUSSPRING
20170929 06:34:18,     FROM V SQL, V SES
                *

Kindly advise how to avoide $ symbol while connecting sqlplus.



Moderator's Comments:
Mod Comment Please start using code tags.

Last edited by zaxxon; 09-29-2017 at 04:12 AM.. Reason: code tags
# 2  
Old 09-29-2017
In "here documents", the input lines are subject to expansion unless (part of) the word is quoted, so try again quoting something.
man bash:
Quote:
Here Documents
.
.
.
<<[-]word
here-document
delimiter
.
.
.
If any characters in word are quoted, the delimiter is the result of quote removal on word, and the lines in the here-document are not expanded. If word is unquoted, all lines of the here-document are subjected to parameter expansion, command substitution, and arithmetic expansion, . . .
This User Gave Thanks to RudiC For This Post:
# 3  
Old 09-29-2017
Another approach is you can put the SQL in a .sql file and call it:-
Code:
  sqlplus -s $PBDW_USERID/$PBDW_PW@$PBDW_SID <<EOF
  @/path/sqlid.sql $1
EOF

Or you can simply escape the dollar sign:-
Code:
  SELECT MAX(SQL.SQL_ID)
    FROM V\$SQL SQL, V\$SESSION SES
   WHERE SQL.SQL_ID = SES.SQL_ID
     AND SQL.PARSING_SCHEMA_NAME = SYS_CONTEXT('userenv', 'CURRENT_SCHEMA')
     AND SES.SCHEMANAME = SYS_CONTEXT('userenv', 'CURRENT_SCHEMA')
     AND SES.STATUS = 'ACTIVE'
     AND UPPER(SQL.SQL_FULLTEXT) LIKE '%' || '&&TBLNAME' || '%'
     AND SQL.SQL_FULLTEXT NOT LIKE '%V$SESSION%'
     AND TO_DATE(SQL.LAST_LOAD_TIME, 'YYYY/MM/DD HH24:MI:SS') BETWEEN
         TO_DATE(SQL.LAST_LOAD_TIME, 'YYYY/MM/DD HH24:MI:SS') - 0.15 / 48 AND
         TO_DATE(TO_CHAR(SYSDATE, 'YYYY/MM/DD HH24:MI:SS'),
                 'YYYY/MM/DD HH24:MI:SS');

# 4  
Old 09-29-2017
Quote:
Originally Posted by Yoda
Another approach is you can put the SQL in a .sql file and call it:-
Code:
  sqlplus -s $PBDW_USERID/$PBDW_PW@$PBDW_SID <<EOF
  @/path/sqlid.sql $1
EOF

Or you can simply escape the dollar sign:-
Code:
  SELECT MAX(SQL.SQL_ID)
    FROM V\$SQL SQL, V\$SESSION SES
   WHERE SQL.SQL_ID = SES.SQL_ID
     AND SQL.PARSING_SCHEMA_NAME = SYS_CONTEXT('userenv', 'CURRENT_SCHEMA')
     AND SES.SCHEMANAME = SYS_CONTEXT('userenv', 'CURRENT_SCHEMA')
     AND SES.STATUS = 'ACTIVE'
     AND UPPER(SQL.SQL_FULLTEXT) LIKE '%' || '&&TBLNAME' || '%'
     AND SQL.SQL_FULLTEXT NOT LIKE '%V$SESSION%'
     AND TO_DATE(SQL.LAST_LOAD_TIME, 'YYYY/MM/DD HH24:MI:SS') BETWEEN
         TO_DATE(SQL.LAST_LOAD_TIME, 'YYYY/MM/DD HH24:MI:SS') - 0.15 / 48 AND
         TO_DATE(TO_CHAR(SYSDATE, 'YYYY/MM/DD HH24:MI:SS'),
                 'YYYY/MM/DD HH24:MI:SS');

If i use in sql file ,we cant get the result and assign in sqlid variable
# 5  
Old 09-29-2017
Quote:
Originally Posted by KK230689
If i use in sql file ,we cant get the result and assign in sqlid variable
That is not a true statement. Here is an example:-
Code:
$ cat script.ksh 
#!/bin/ksh

conn_str="user@passwd@dbinstance"

sqlid=$(
        sqlplus -s $conn_str << EOF
        set echo off head off feed off pagesize 0 trimspool on linesize 1000 num 20
        @sqlid.sql
EOF
)

print $sqlid

Code:
$ cat sqlid.sql
select sql_id from v$session where sql_id is not null and rownum < 2;

Here is the output I got:-
Code:
$ ./script.ksh
0akaxw65f1nym

This User Gave Thanks to Yoda For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

SQLPLUS calling Via Script

Hello All, Could you please help me if i am doing anything wrong in below script, especially the sqlplus part performance wise or anything else i could improvise in the script. Thank you. #!/bin/ksh ## Batch Obj Id MP_BCH_OBJ_ID=$1 PASS=$2 partition=$3 ## script dir... (6 Replies)
Discussion started by: Ariean
6 Replies

2. Post Here to Contact Site Administrators and Moderators

Calling Sybase Stored proc from UNIX Shellscript.

Hi, I am new to shell scripting and Sybase database i need a help that i try to execute a SYBASE stored procedure from a Unix shell script and wanna write the output of the SP into a Text File, somehow i tried to find a solution but when i try to run the script i am not getting the output file with... (1 Reply)
Discussion started by: Arun619
1 Replies

3. Shell Programming and Scripting

Calling sqlplus from Korn shell heredoc issue

Hi, I am facing an issue wherein some temporary files (here docs) are getting created in /tmp and are not getting deleted automatically. When i check the list of open files with below command i can see one file is getting appended continuously.(In this case /tmp/sfe7h.34p) The output is... (4 Replies)
Discussion started by: Navin_Ramdhami
4 Replies

4. UNIX for Dummies Questions & Answers

calling a unix shell script from sqlplus

I want to execute a shell script from sqlplus prompt and get its output back to sqlplus. Is this possible? if yes just give me an example for doing that. (2 Replies)
Discussion started by: boopathyvasagam
2 Replies

5. Shell Programming and Scripting

Error in calling store procedure using SQLPLUS in unix

Hi, I am facing the following error in calling the stored procedure from SQLPLUS in unix environment. SQL> set serveroutput on SQL> var store number; SQL> exec test_proc(:store, 200); BEGIN TEST_PROC(:store, 200); END; * ERROR at line 1: ORA-01858: a non-numeric character was found... (8 Replies)
Discussion started by: pradeep7986
8 Replies

6. Shell Programming and Scripting

calling sqlplus, read table return etc

I have korn shell scripts. I want to pass a variable to a script which will execute a a sql script to read a table that contains env. variables. I want to read and then somehow export at unix level variables example for every row selected from the table build export command line field1... (5 Replies)
Discussion started by: TimHortons
5 Replies

7. Shell Programming and Scripting

Problems with SQLPLUS in shellscript

Hi All, I need to run one sql query from my shellscript. query is something like this SELECT DISTINCT 1 FROM ******* WHERE ***** ('CRE','CRA') AND TO_CHAR (***** , 'DD:MM:YYYY') = TO_CHAR ( SYSDATE, 'DD:MM:YYYY') AND ****** IN ( 0) AND ******** =0; I can't call sql file inside... (7 Replies)
Discussion started by: scorpio
7 Replies

8. Shell Programming and Scripting

calling sqlplus from within a for loop

i'm not new to programming, but i AM new to unix scripting. here's my deal. this works: #!/bin/ksh echo "HELLO" /oracle_home/bin/sqlplus username/password@MYDB<<EOF SELECT COUNT(*) FROM EMPLOYEES; EOF exit echo "GOODBYE" this doesn't: #!/bin/ksh echo "HELLO" for x in 1 2... (4 Replies)
Discussion started by: akosz
4 Replies

9. UNIX for Advanced & Expert Users

Problem while calling Oracle 10g SQLPLUS files

Hi all, Iam facing a lot of problem while calling Oracle 10g SQLPLUS files from shell. What is the standard procedures to be taken care. Any help would be useful for me. Thanks in advance, Ganapati. (2 Replies)
Discussion started by: ganapati
2 Replies

10. Shell Programming and Scripting

calling sqlplus from shell

Hi All, I am executing the following code :- sqlplus -s ${DATABASE_USER} |& print -p -- 'set feed off pause off pages 0 head off veri off line 500' print -p -- 'set term off time off serveroutput on size 1000000' print -p -- "set sqlprompt ''" print -p -- "SELECT run_command from... (2 Replies)
Discussion started by: suds19
2 Replies
Login or Register to Ask a Question