Actual SQL instead of using a file from within a shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Actual SQL instead of using a file from within a shell script
# 8  
Old 08-01-2013
Quote:
Originally Posted by biobill
There are no spaces after any of the lines, in the code section posted it was a direct cut and paste and you cant highlight anything after the last character on any line.
As far as the ^M... that is not the case either. I learned that the hard way last year. I am not a big fan of VI so I modify the files in a windows environment then ftp over, once transferred I remove the ^M via vi before running the file.

So, no spaces/tabs before any line, no spaces/tabs after any line, no ^Ms anywhere and no blank lines, correct?


It's unusual for the here-document to not work. I tried a similar script in my Cygwin + bash + Oracle 11g system and it works fine. Could you check a few more things?


(1) The error message:

Quote:
Originally Posted by biobill
...
Code:
syntax error at line 213 : `<<' unmatched

...

Does the line number 213 correspond to the following line you showed in your post?


Code:
$ORACLE_HOME/bin/sqlplus $ORA_DBCU/$ORA_DBCP <<EOD


Do you have more than one EODs in your script? Maybe you are looking at an EOD other than the one mentioned by the error message?


(2) What shell are you using? (Hopefully not too old.)


(3) Everything else remaining the same, replace this line:

Code:
$ORACLE_HOME/bin/sqlplus $ORA_DBCU/$ORA_DBCP <<EOD


by this:


Code:
$ORACLE_HOME/bin/sqlplus $ORA_DBCU/$ORA_DBCP <<-EOD


Doing this allows the leading TABs before the ending EOD to be ignored.


(4) Remove everything in your shell script and simply test the smallest piece of code that balks:

Code:
... put the shebang; assign the variables
sqlplus $ORA_DBCU/$ORA_DBCP <<EOD
select sysdate from dual;
EOD

# 9  
Old 08-01-2013
I still think the here document works, I see folks running it with -s or -silent switch. ^M should not be an issue but the can always be removed with the dos2unix tool or
Code:
cat <file> | tr -d '\r'

. I also don't think there is anything wrong with your alternative, it's not pretty, I would have used sed or awk...but as long as this thing does not consume to many cycles it's just fine. You might want to look into the functionality of sqlplus...you can probably pipe your sql to it as well... I know for mysql I can do a here document and can pipe to it and I can redirect standard input, by example....
Code:
cat foo.sql | mysql
mysql < foo.sql
mysql <<EOD
use foo;show tables;
EOD

This User Gave Thanks to blackrageous For This Post:
# 10  
Old 08-03-2013
Ok, I tested the original mysql idea by blackrageous and it appears that something similar could be done for Oracle's sqlplus as well.
Using the "printf" built-in of Bash or print/printf of Ksh, you could do combine all commands together and pipe them to sqlplus.

In the following testcase, I have an Oracle procedure called "sp_test" in the "test" schema, that simply prints its input parameter value.

Code:
$ 
$ # In Bash
$ printf "set serveroutput on\n exec sp_test(p_yr_qtr => 4)" | sqlplus -s test/test
From procedure sp_test: input p_yr_qtr = 4

PL/SQL procedure successfully completed.

$ 
$

This could be incorporated in a shell script like so -

Code:
$ 
$ # display the content of the shell script
$ cat -n call_ora_proc.sh
     1    #!/usr/bin/bash
     2    export ORA_DBCU=test
     3    export ORA_DBCP=test
     4    export YR_QTR=4
     5    export spool_file=call_ora_proc.log
     6    printf "clear columns breaks computes
     7    set recsep off wrap off heading off pagesize 0 space 0 newpage 0 serveroutput on
     8    spool ${spool_file}
     9    exec sp_test(${YR_QTR});
    10    spool off
    11    exit" | $ORACLE_HOME/bin/sqlplus -s $ORA_DBCU/$ORA_DBCP 1>/dev/null
$ 
$ # run the shell script
$ . call_ora_proc.sh
$ 
$ # and check the log file created by it
$ cat call_ora_proc.log
From procedure sp_test: input p_yr_qtr = 4                                      

PL/SQL procedure successfully completed.

$ 
$

thereby avoiding a here-document.
This User Gave Thanks to durden_tyler For This Post:
# 11  
Old 08-03-2013
Sorry for the late responses, didnt get any emails until this morning that replies had been posted.

I appreciate the help. Based on blackrageous's comment

Quote:
I also don't think there is anything wrong with your alternative, it's not pretty, I would have used sed or awk...but as long as this thing does not consume to many cycles it's just fine.
I think I will stick with that for now. This script loads data every quarter and is working, that being said it runs once per quarter thereby not taxing the server too bad.

Thanks again for taking the time to help. I am going to play around with your suggestions and try to get them to work for the sake of trying to improve my unix skillset.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to call sql file

hi , the below script contains sql query and after executed it sends the output of the query (output.txt) to an email body with conditional subject line based on the output of all_counts_match.txt. i want to make this script generic so that it can accept the sql file as parameter and can... (5 Replies)
Discussion started by: itzkashi
5 Replies

2. Shell Programming and Scripting

Unable to pass value from .Shell script to .SQL file

Hi All, I am new to shell script. I am trying to pass value from .sh file to .sql file . But I am able to run the .sql file from .sh file with values in sql file. But I am unable to pass the values from .sh file. can some one please help to resolve this. here is my .sh file s1.sh ... (4 Replies)
Discussion started by: reddy298599
4 Replies

3. Shell Programming and Scripting

Shell script that will compare two config files and produce 2 outputs 1)actual config file 2)report

Hi I am new to shell scripting. There is a requirement to write a shell script to meet follwing needs.Prompt reply shall be highly appreciated. script that will compare two config files and produce 2 outputs - actual config file and a report indicating changes made. OS :Susi linux ver 10.3. ... (4 Replies)
Discussion started by: muraliinfy04
4 Replies

4. Shell Programming and Scripting

Calling sql file from shell script with parameters.

Hi, I am calling a sql file script.sql from shell script and passing few parameters also as shown below: sqlplus -S id/password @script.sql $param1 $param2 Now,In sql file I have to create a extract text file after querying oracle tables based on the parameters passed(param1,param2) as... (7 Replies)
Discussion started by: anil029
7 Replies

5. Shell Programming and Scripting

calling a sql file in my shell script

Hi, I want to call a sql file in my shell script. see the below code:- if ] then ( isql -U${S_USER} -S${S_SERV} -w100 -b -h0 <<ENDSQL | sed -e "s/Password://" ${S_PWD} set nocount on go use ${S_DB} go // need to call a file name... (16 Replies)
Discussion started by: dazdseg
16 Replies

6. Shell Programming and Scripting

How to use sql data file in unix csv file as input to an sql query from shell

Hi , I used the below script to get the sql data into csv file using unix scripting. I m getting the output into an output file but the output file is not displayed in a separe columns . #!/bin/ksh export FILE_PATH=/maav/home/xyz/abc/ rm $FILE_PATH/sample.csv sqlplus -s... (2 Replies)
Discussion started by: Nareshp
2 Replies

7. Shell Programming and Scripting

(Urgent):Creating flat file using sql script and sqlplus from UNIX Shell Script

Hi, I need help urgently for following issue. Pls help me to resolve this issue. I am calling sql script file(file1.sql) from UNIX Shell Script(script1.ksh) using sql plus and trying to create flat file that contains all records returned from SQL query in SQL script(file1.sql) I given... (6 Replies)
Discussion started by: praka
6 Replies

8. UNIX for Advanced & Expert Users

Calling sql file from shell script

Hi I have a shell script that call a sql file. The sql file will create a spool file. My requirement is, when ever i get an OS error like file not found. I have to log it in a log file. Could some who worked in a like scenario help me by giving the code sample. Many Thanks.. (1 Reply)
Discussion started by: chintapalli001
1 Replies

9. Shell Programming and Scripting

calling sql file from shell script

Hello everybody I need help calling sql file from shell script. Can anyone help me creating a small shell script which calls an sql file . The .sql file should contain some select statements like select emp_no from emp_table; select emp_id from emp_table; And the results should be... (6 Replies)
Discussion started by: dummy_needhelp
6 Replies

10. Shell Programming and Scripting

How to execute a .sql file with shell script

hi everybody... can anyone help me in executing the .sql file with shell scripting.... thanx in advance (2 Replies)
Discussion started by: abuanas
2 Replies
Login or Register to Ask a Question