script variable within a sql query


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting script variable within a sql query
# 1  
Old 12-11-2007
script variable within a sql query

I created a script to read a csv file with four columns. The script also saved values of each col in a arry.
In the script, i connected to db try to run a query to pull out data baisc on the values from the csv file.

select Num from tableName
where Sec_Num in ('${isin[@]}') /*isin[@] is an arry which its data are from extracted from the csv file.

could you please tell me what wrong with this query. When I run the script,it is unable to pull out any data from db. Do you have any suggestion?

Thanks You!

Last edited by Sherry_Run; 12-11-2007 at 06:21 PM..
# 2  
Old 12-12-2007
Quote:
Originally Posted by Sherry_Run
select Num from tableName
where Sec_Num in ('${isin[@]}') /*isin[@] is an arry which its data are from extracted from the csv file.

When I run the script,it is unable to pull out any data from db.
First off, a variable in single quotes won't get expanded (in fact this is what single quotes are for - protect variables from expansion). So probably your SQL script wont see the content of $isin[], but the string "${isin[@]}", which, of course, won't be found in the database. To correct this write your SQL statement into an intermediate file and then execute this file. Like here:

Code:
exec 3>/tmp/tmpSQL.sql
print -u3 "select Num from tableName"
print -u3 "where Sec_Num in (\'${isin[@]}\')"
exec 3>&-

db2 -tvf /tmp/tmpSQL.sql
# rm -f /tmp/tmpSQL.sql

Once the script is running you can delete the intermediate file immediately, but for debugging purposes it is good to have a look into into it, which is why i put a comment in front of "rm".

Second (possible) point: I dont know all too much about SQL, but i think a list of values has to be comma-separated. If this is not the case, than this point is moot, but in case this is true the variable expansion "${isin[@]}" would be wrong: the shell expands the array to a space-separated list instead of a comma-separated one. Expand the list prior to using it and put commata in between:

Code:
typeset -i iCnt=2
typeset    chList="${isin[1]}"

while [ $iCnt -le ${#isin[@]} ] ; do
     chList="${chList}, ${isin[$iCnt]}"
     (( iCnt += 1 ))
done

You would have to use the variable "$chList" instead of the array in the first code snippet of course.

Hope this helps.

bakunin
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 Assign the Output of an SQL Query to a Variable?

Hi iam new to shell scripting how to declare variables as redshift query and I have to compare two counts by using if condition . ex:count=select count(*) from prd; select count(*) from prd; select count(*) from tag; can any one help me . Please use CODE tags when displaying... (1 Reply)
Discussion started by: sam526
1 Replies

2. Shell Programming and Scripting

Asign to variable ksh some values of sql query

Hi, I'm trying to asign to ksh varible some values of a sql query. The output query would be: xxxx 1 yyyy 2 I do: values=`$PATH_UTI/query_sh " select think1||'------'||think2 from some_table where think3 = '$1'; ... (2 Replies)
Discussion started by: mierdatuti
2 Replies

3. UNIX for Dummies Questions & Answers

Script to run sql query.

Please read How To Ask Questions The Smart Way (1 Reply)
Discussion started by: balu_279013
1 Replies

4. Shell Programming and Scripting

SQL query within an awk script !!

Hello Experts, This sounds crazy, but one of my requirements is that if ID in 5th column in my input file (: separated) is not present in the oracle database table (say t_id) then this record should be skipped. I am using awk for all other requirements. So I was thinking if this requirement... (4 Replies)
Discussion started by: juzz4fun
4 Replies

5. Shell Programming and Scripting

Run SQL thru shell script: how to get a new line when run sql query?

Hi, this's Pom. I'm quite a new one for shell script but I have to do sql on shell script to query some information from database. I found a concern to get a new line...When I run my script, it retrieves all data as wondering but it's shown in one line :( What should I do? I'm not sure that... (2 Replies)
Discussion started by: Kapom
2 Replies

6. UNIX for Advanced & Expert Users

Passing Hash variable in to sql query in perl

Hi Everyone, Can anyone help me how do i call hash variable in to sql query in perl. Please see the script below i have defined two Hash %lc and %tab as below $lc{'REFF'}='V_RES_CLASS'; $lc{'CALE'}='V_CAP_CLASS'; $lc{'XRPD'}='V_XFMR_CLASS'; $tab{'V_RES_CLASS'}='V_MFR_SERS';... (6 Replies)
Discussion started by: jam_prasanna
6 Replies

7. Shell Programming and Scripting

query sql using shell script

query sql using shell script, is it possible? my friend told me to do a file.sql and link to my shell script, but can i query sql using shell script? thanks in advance! (2 Replies)
Discussion started by: kingpeejay
2 Replies

8. Shell Programming and Scripting

Problem while storing sql query value in a variable

Hi, When i execute the below statement , the value is not getting stored in the variable. AnneeExercice=`sqlplus $LOGSQL/$PASSWORDSQL << FIN >> $GEMOLOG/gemo_reprev_reel_data_ventil_$filiale.trc SELECT bi09exercice FROM bi09_scenario WHERE bi09idfiliale=UPPER('de') AND ... (1 Reply)
Discussion started by: krishna_gnv
1 Replies

9. Shell Programming and Scripting

How to store the sql query's output in a variable

Hi, My requirement is : We are calling an sql statement from a UNIX session, and fetching data into some variables from a table .. now we are unable to access these variables from outside the SQL part. Please let me know how can I achieve this. Can you please share a code snippet which... (4 Replies)
Discussion started by: venkatesh_sasi
4 Replies

10. Shell Programming and Scripting

sql query variable not exactly unix

I know htis isnt exactly unix.... but hopefully someone can help me or direct me someplace to get help. I can run sql queries in scripts against my informix db using: dbaccess mydb myquery.sql >> sql.output I need to write my script to select based on todays date. Its very... (5 Replies)
Discussion started by: MizzGail
5 Replies
Login or Register to Ask a Question