Pass command line arg to sql file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Pass command line arg to sql file
# 1  
Old 08-04-2013
Pass command line arg to sql file

Hi all,
How to pass the command line argument to a sql file

Script:
Code:
#!/bin/ksh

if  [[ ${2} = -n && ${#} = 3 ]] ; then
 
test.sql       
 
fi

My Sql Informix DB:
echo "select * from table where col1 = 2234
and col2 = '$3'"|dbaccess ddname

But im getting `:' unexpected error
# 2  
Old 08-04-2013
you have "$3" surrounded by single quotes which means the shell will not evaluate it ...
# 3  
Old 08-04-2013
Try this:
Code:
test.sql $3

and
Code:
select * from table where col1 = 2234
and col2 = '&1'

# 4  
Old 08-07-2013
Thanks, how to pass variable to sql file.
Im tryin in two ways,

Method 1:
my.sql
Code:
select * from table where col1 = '$1' and col2 = 'text'; 
 
Method 1execute: dbaccess database my.sql $var

Method2:

Code:
select * from table col1 in (`cat inputfile`) and col2 = 'text';
 
method 2execute: dbaccess database my.sql

But both throwing error.
# 5  
Old 08-08-2013
I dont know about the db connection and execution of sql commands, but it seems that you are passing the my.sql itself as a parameter file to the dbaccess command.
# 6  
Old 08-08-2013
HERE and echo example to expand value of variable using some kind of "template":
Code:
x=111
# here
cat <<EOF
select * from table where col1 = '$x' and col2 = 'text';
EOF

# normal command line expansion. ' ' are inside " " = only characters without special meanings
echo "delete from table where col1 = '$x' and col2 = 'text';"

I'm not used dbaccess cmd, but something like:
Code:
x=111

dbaccess args <<EOF
select * from table where col1 = '$x' and col2 = 'text';
EOF

dbaccess args <<EOF > output.result.file
select * from table where col1 = '$x' and col2 = 'text';
EOF
#or
echo "delete from table where col1 = '$x' and col2 = 'text'  ;  " | dbaccess args  > output.result.file

dbaccess <<EOF
select * from table col1 in (  $(cat inputfile)  ) and col2 = 'text';
EOF


Last edited by kshji; 08-08-2013 at 11:54 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to read the output of a command line by line and pass it as a variable?

Hi, I have some 2000 names in a table like below. Java Oracle/SQL ANSI SQL SQL,DWH,DB DB&Java And by using for loop in my code i am able to get a single word but if there is any special character or space then it is considering as a next line. I have to execute the below queries in... (10 Replies)
Discussion started by: Samah
10 Replies

2. Shell Programming and Scripting

How to pass each line of a text file as an argument to a command?

I'm looking to write a script that takes a .txt filename as an argument, reads the file line by line, and passes each line to a command. For example, it runs command --option "LINE 1", then command --option "LINE 2", etc. I am fetching object files from a library file, I have all the object file... (2 Replies)
Discussion started by: Paul Martins
2 Replies

3. Shell Programming and Scripting

Regarding file input to SQL from command line

Hi friends, Need your help again to get a best approach for the below scenario. I am previously having one shell script which accepts request_id/s as the command line argument. single req_id arg= 1111 Multiple arg= 1111,2222,3333 which gets passed to the embedded sql inside to... (9 Replies)
Discussion started by: Showdown
9 Replies

4. 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

5. Shell Programming and Scripting

How to pass a shellscript variable to a sql file?

Hi, i wan't to pass a shellscript variable to a sql file. a.sql select $field from dual; the way i am calling this is through sqlplus field_name="sysdate" sqlplus -s username/password@hostname:port/servicename <<EOF @a.sql $field_name EOF (4 Replies)
Discussion started by: reignangel2003
4 Replies

6. UNIX for Dummies Questions & Answers

Stumped .. is this a command line arg?

I need a bit of explanation: LogFile=${LOGS_DIR}/${1}_$$ I know: - LOGS_DIR is an environment variable - $$ is the PID ... but what is ${1} ?? Is it another method to access a command line variable, or the job name? Thanks! Jon (3 Replies)
Discussion started by: jdorn001
3 Replies

7. UNIX for Dummies Questions & Answers

To pass multiple arguments from file in to an sql query

Hi all , I want to pass contents from a file say f1 as arguments to a sql query which has In statement using a script example select * from table_1 where login in ( `cat f1`) ; will this work or is there any other way to do it. (1 Reply)
Discussion started by: zozoo
1 Replies

8. Shell Programming and Scripting

Pass string arg from shell to perl

My shell script generates a bunch of lines of text and passes this text as an argument to a perl script. I'm able to do this, but for some reason newlines don't get recognized in the perl script and so the script just prints actual '\n' instead of carriage returning, otherwise everything gets... (3 Replies)
Discussion started by: stevensw
3 Replies

9. Shell Programming and Scripting

How to pass an array as arg to a script..

Hi, Please guide to pass an array as a arg to a script... for example, I have a script small.sh to find the small no of given arg as below... #! /bin/sh # this script is for finding the small number set -A arr_no_updates small=$1 i=1 for arr in $@ do if (3 Replies)
Discussion started by: little_wonder
3 Replies

10. Shell Programming and Scripting

command line arg issue with awk

Hi friends, I am trying to pass input from command line and trying to print that column values. (FYI: I am using ksh) My code goes like this... #!/bin/sh column=$1 awk '{print $'$column'}' I execute using command like this --> ls -l | file_name parameter Hope I am clear with my... (2 Replies)
Discussion started by: divzz
2 Replies
Login or Register to Ask a Question