How to call an sql script inside a while statement in KSH


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to call an sql script inside a while statement in KSH
# 1  
Old 04-08-2010
How to call an sql script inside a while statement in KSH

Hi all,

I'm trying to run an sql inside a loop which looks like this

Code:
#!bin/ksh
while IFS=, read var1 var2 
do

 sqlplus -s ${USERNAME}/${PASSWORD}@${ORACLE_SID} << EOF
    insert into ${TABLE}
        (
        appt_date
        )
  values
        (
        '${var1 }'
        );
    COMMIT;
    exit;
    EOF

done < "$file"

An error pop out and tells:
Code:
sh: Syntax error: '<<' is not matched.

Does anyone knows how to fix this error to be able to run the sql script successfully? Need help.

Last edited by pludi; 04-08-2010 at 06:39 AM.. Reason: code tags, please...
# 2  
Old 04-08-2010
If you post code, please surround it with [CODE][/CODE] tags, either by entering them manually, or by selecting the code and clicking the # button at the top of the edit box.

With a heredoc (the '<<' stuff), the ending token (in your case 'EOF') has to start at the first column on a line of it's own. Remove the leading whitespace in that line, and it should be fine.
# 3  
Old 04-08-2010
Hi pludi,

thanks for replying, i do what you had said giving this input:

Code:
sqlplus -s ${USERNAME}/${PASSWORD}@${ORACLE_SID} <<EOF

but the same error still occurs
# 4  
Old 04-08-2010
Read the post again. I said the ending token has to be the first and only thing on a line of it's own. The ending token is the EOF. Try to spot the difference:
Code:
#!bin/ksh
while IFS=, read var1 var2 
do

 sqlplus -s ${USERNAME}/${PASSWORD}@${ORACLE_SID} << EOF
    insert into ${TABLE}
        (
        appt_date
        )
  values
        (
        '${var1 }'
        );
    COMMIT;
    exit;
EOF

done < "$file"

# 5  
Old 04-08-2010
Quote:
Originally Posted by ryukishin_17
Code:
sh: Syntax error: '<<' is not matched.

Does anyone knows how to fix this error to be able to run the sql script successfully?
The problem is rather simple: you indented your code and indented the "here-document" too, but this is not allowed. When you write "process << EOF" the shell will search for a line starting with " EOF" and interpret everything between your statement and this line as part of the document.

As you have indented your source code you have changed this closing line of your here-document to several spaces and only then the "EOF" and this makes this line invalid. Example:

Code:
# will work:
some_process <<EOF
blah blah
EOF

# will not work:
some_process << EOF
blah blah
     EOF

I hope this helps.

bakunin
# 6  
Old 04-08-2010
thanks pludi and bakunin. the solution you gave worked Smilie
# 7  
Old 04-08-2010
'man ksh' yields:
Code:
     << [-]word
           The shell input is read up to a line that is the  same
           as word, or to an EOF. No parameter substitution, com-
           mand substitution, or file  name  generation  is  per-
           formed  on  word.  The  resulting  document,  called a
           here-document, becomes  the  standard  input.  If  any
           character  of  word  is  quoted,  no interpretation is
           placed upon the characters of the document. Otherwise,
           parameter  and command substitution occur, \NEWLINE is
           ignored, and \ must be used to quote the characters \,
           $,  `,  and  the  first  character  of  word.  If - is
           appended to <<, then all  leading  tabs  are  stripped
           from word and from the document.

Code:
 will work:
some_process <<EOF
blah blah
EOF

# will work as well:
some_process <<-EOF
blah blah
     EOF

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Call a UNIX script inside another and dont wait for it

Hi I have two scripts script1.sh and script2.sh(say this script is a long running). I want to call script2.sh inside and script1.sh,but when i call script2.sh i dont want to wait for script2 to complete and want this to run in back ground and go on next commands in script 1.sh and finally at the... (2 Replies)
Discussion started by: lijjumathew
2 Replies

2. UNIX for Advanced & Expert Users

Using PHP , call a sql inside a unix script

I am running the xampp on WINDOWS, and my php script is connecting to a unix script on a different server (ssh2_connect("11.31.138.56", 22). I am running the unix script and inside this script I am calling the .sql file . The SQL is connecting to oracle db on the unix server. But the sqlplus... (2 Replies)
Discussion started by: madfox
2 Replies

3. Shell Programming and Scripting

issues with sql inside if statement

Hi, I have problem with the following code. My IF block is not executed. And I see "syntax error near unexpected token `)'" error for line "EOF" in the stats_function(). but when I comment the IF block I don't see this error. Kindly help me with this issue. clean_function() {... (10 Replies)
Discussion started by: babom
10 Replies

4. UNIX for Dummies Questions & Answers

Call sql script

I want to call the sql query from UNIX..but how to set page size and other necessary parameters i don't know plz guide me how to do this (2 Replies)
Discussion started by: sagar_1986
2 Replies

5. UNIX for Advanced & Expert Users

call sql through shell script

Hi i am not able to connect sqlplus my script is as follows $ORACLE_HOME/bin/sqlplus << ! > /tmp/extract/DM.txt and output is SQL*Plus: Release 11.1.0.7.0 - Production on Wed Jan 18 02:53:54 2012 Copyright (c) 1982, 2008, Oracle. All rights reserved. Enter user-name: t175481... (1 Reply)
Discussion started by: tushar_spatil
1 Replies

6. Shell Programming and Scripting

how to pass a variable to an update sql statement inside a loop

hi all, i am experiencing an error which i think an incorrect syntax for the where clause passing a variable was given. under is my code. sqlplus -s ${USERNAME}/${PASSWORD}@${SID} << END1 >> $LOGFILE whenever sqlerror exit set serveroutput on size 1000000 declare l_rc ... (0 Replies)
Discussion started by: ryukishin_17
0 Replies

7. Shell Programming and Scripting

Exit statement ignored inside KSH function

Hi All, I have multiple functions in my script and I'm trying to capture stdout from some of them, but I also do some error checking in them (in the functions that output something to their stdout that needs capturing) and I need to be able to end the entire script with an error message. ... (2 Replies)
Discussion started by: gkubok
2 Replies

8. Shell Programming and Scripting

how can i call a shell script from pl/sql

I would like to call the shell script from pl/sql and i need to uses the value returned by the shell script in pl/sql procedure. can any one suggest me how can i do that? (3 Replies)
Discussion started by: rajesh.P
3 Replies

9. UNIX for Dummies Questions & Answers

how can a call shell script from pl/sql

I like to call a shell script from pl/sql proceduere and i have to use the shell script return value in that procedure. i am using oracle 9i and cygwin. can any one suggest me how can i do this (0 Replies)
Discussion started by: rajesh.P
0 Replies

10. Shell Programming and Scripting

How to call pl/sql in unix script

sample code as following: test_sql(){ #test#echo test_sql str=`$ORACLE_BIN/sqlplus -s $user/$passwd <<EOM set verify off set heading off set feedback off #--------start pl/sql { DECLARE CURSOR pah_cs IS select id from table where letter = 'abcd';... (6 Replies)
Discussion started by: YoYo
6 Replies
Login or Register to Ask a Question