Regarding file input to SQL from command line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Regarding file input to SQL from command line
# 1  
Old 11-29-2015
Oracle 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 the in operator to update the status to inactive

Pseudo code for the same : (please ignore syntax error)

Code:
#! /bin/bash
set -vx

req_ids=$1

sqlplus -s xxxx/yyyy@zzzz  << EOF >> /grs/sql_logs
update table_name  set status='INACTIVE' where req_id in ($req_ids);
commit;
exit;
EOF

The above method was working like a charm for some small set of request_ids.
As presently I am getting a huge volume of request_ids(~1200) I fear whether the command line argument will allow a string of 5000bytes.
If at all it allows the in operator will not allow > 2499 bytes of data.

I have tried inserting the record into a temp table and run a for loop in sql but can't as i am not privileged to create temp table in prod env.
Also tried to pass the file as the command line (.csv file) argument then split the file into 300 records each by pivoting using awk command and process one by one file and remove them once processed but still restricted to create temp files in prod box.

Can you please focus some idea to deal with this situation without using temporary table and split method.

Many Thanks to all the members and UNIX.com
# 2  
Old 11-29-2015
Do you really (need to) supply the IDs on the command line? How do you construct the line? How about creating an sql- file with several update lines for a max number of IDs? And then executing that?
# 3  
Old 11-29-2015
Oracle

Thanks Rudic. Good idea of creating multiple updates in a single SQL
File . but the ids are not constant. Everyday I am getting request 3-4 times with different sets of ids.
# 4  
Old 11-29-2015
How do those IDs come in?
# 5  
Old 11-29-2015
Oracle

user provides in the form of txt file (single column) through mail. then Sent to the deployment team through some defined process.

However i have access to transfer the files to production box.

I am wondering whether it is possible to cut 300 lines every time pivot them to a string and pass to the sql.

say 900 ids are there then 3 loops of unix same update with different strings of ids will suffice.Smilie
# 6  
Old 11-29-2015
I think the line length limitation has to do with parameters that are passed to the script and not what is in the script itself. You say there is a text file with arguments in a single column, so that means this?

Code:
$ cat file.txt
1111
2222
3333
....

?

If so, you could try something like this
Pass the filename a parameter to the script:
Code:
#! /bin/bash
set -vx

req_ids=( $(<"$1") )
IFS=,
sqlplus -s xxxx/yyyy@zzzz  << EOF >> /grs/sql_logs
update table_name  set status='INACTIVE' where req_id in ($(printf "%s" "${req_ids[*]}"));
commit;
exit;
EOF

Obviously this is just a principle. Your script would need exception handling, for example to test where the filename is passed as an argument and whether it exists.
IFS=, is used for the expansion of the array so that the values are separated by comma's. If you have more lines in your script then you'll probably want to save the contents of IFS and restore it afterwards..

Code:
oldIFS=$IFS
IFS=,
....
IFS=$oldIFS


Last edited by Scrutinizer; 11-29-2015 at 04:41 PM.. Reason: setting reg_ids array was wrong
This User Gave Thanks to Scrutinizer For This Post:
# 7  
Old 11-29-2015
Oracle

Yes ! I meant that. I will simulate the solution you have provided in dev and post the result. Thanks for your time.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read input file and used it to SQL query

Hi All, Seeking for your assistance to read each line $1 and $2 of input file and used it to query. Ex. file1.txt(number range) 9064500000 9064599999 9064600000 9064699999 9064700000 9064799999 Database name: ranges_log a_no message 9064500001 test 9064700000 ... (7 Replies)
Discussion started by: znesotomayor
7 Replies

2. Shell Programming and Scripting

Pass command line arg to sql file

Hi all, How to pass the command line argument to a sql file Script: #!/bin/ksh if ] ; 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 (5 Replies)
Discussion started by: Roozo
5 Replies

3. Shell Programming and Scripting

Read input file with in awk script not through command line

Hi All, Do we know how to read input file within awk script and send output toanother log file. All this needs to be in awk script, not in command line. I am running this awk through crontab. Cat my.awk #!/bin/awk -f function test(var){ some code} { } END { print"test code" } (5 Replies)
Discussion started by: random_thoughts
5 Replies

4. Shell Programming and Scripting

Need help to run sql query from a script..which takes input from a file

I need to run sql script from shell script which takes the input from a file and contents of file will be like : 12345 34567 78657 and query will be like : select seq_nbr from bus_event where event_nbr='12345'; select seq_nbr from bus_event where event_nbr='34567'; select seq_nbr... (1 Reply)
Discussion started by: rkrish
1 Replies

5. UNIX for Dummies Questions & Answers

SQL Script to use variable value from input file

Here is the requirement, When I run the "run file KSH (sql)", it should substitute '${pCW_Bgn_DT}' with 201120 and '${pCW_End_DT}' with 201124 Input File ---------- $ cat prevwk.dat 201124 20110711 run file KSH (sql) ------------------ In this file, I want to use the variables... (1 Reply)
Discussion started by: shanrice
1 Replies

6. UNIX for Dummies Questions & Answers

Bash script to delete file input on command line

1) I wrote a script and gave the desired permissions using "chmod 755 scriptname". Now if i edit the script file, why do i need to set the permission again? Didn't i set the permission attribute.. or if i edit the file, does the inode number of file changes? 2) I am running my unix on a server... (1 Reply)
Discussion started by: animesharma
1 Replies

7. Shell Programming and Scripting

SQL PLUS Command 'ACCEPT' is not waiting for user input with sh shell script

Dear All, The sqlplus 'Accept' command is not waiting for user input when I include the command within a shell script. Note: The 'Accept' command is working fine if I execute it in a SQLPLUS Prompt. Please fins the below sample script which i tried. SCRIPT: -------- #!... (4 Replies)
Discussion started by: little_wonder
4 Replies

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

9. Shell Programming and Scripting

isql input file with multiple sql statements

I've got: isql -U $USERID -S $SERVER -D $DATABASE -i inputfile.sql -o outputfile.txt in inputfile I have: go sql#1 go sql#2 go sql#3 go I also tried without "go" and with";" instead which did not work SQL statements will work if I paste them directly into the script and use EOF ... (0 Replies)
Discussion started by: Cailet
0 Replies

10. Shell Programming and Scripting

SED + Regex + SQL Input file

Here's the problem... I have a mysqldump file and I need to put single quotes around the date/time timestamp. So for example I have a line like: INSERT INTO attachments VALUES (1,182,2004-08-06 09:24:04,'description'... and I need it to become INSERT INTO attachments VALUES... (10 Replies)
Discussion started by: primal
10 Replies
Login or Register to Ask a Question