Storing passing and executing select statement in loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Storing passing and executing select statement in loop
# 1  
Old 08-25-2015
Storing passing and executing select statement in loop

Hi,

i want to do the following:

Grep the following kind of strings for the 15digit ID which is stored in filename1:

Code:
"14:06:51.396 [Aug-21-2015]  INFO  BMCREMEDYSD INPUT-ACTION Failed to retrieve Remedy Incident Modification record: 000000000039047 org.apache.axis2.AxisFault: Read timed out - complete 
 
14:07:50.826 [Aug-21-2015]  INFO  BMCREMEDYSD INPUT-ACTION Failed to retrieve Remedy Incident Modification record: 000000000039048 org.apache.axis2.AxisFault: Read timed out - complete"

I achieved this using the following:

Code:
grep -Eo '[0-9]{15}' filename1

But i want this output in another file , say demo.txt

expected output of demo.txt:
Code:
000000000039047 
000000000039048

Now, if we are able to get this , i want to pass the contents of this file, line by line, ID by ID to an SQL select Statement, using for loop:
For eg:
Code:
for each line in (demo.txt)
do
select * from tablename where ID=(content of demo.txt)
done

And while doing this we want to store the select staement's output in some variable. Then use this final variable for further processing.

please help in achieving this.

Last edited by Corona688; 08-25-2015 at 12:36 PM.. Reason: code tags, not font tags
# 2  
Old 08-25-2015
Hello Khushbu,

Kindly use code tags as per forum rules while posting any command/codes/Inputs in your posts, following may help you in same.
Code:
 grep -Eo '[0-9]{15}' filename1 > demo.txt
 ### To print only the select statements following.
 awk '{print "select * from tablename where ID= " $0}' demo.txt
  
 ### To take output in a output file if happy with above print command.
 awk '{print "select * from tablename where ID= " $0}' demo.txt > output.txt
  
 ### In case you need your id inclosed within ' then following may help.
 awk -vs1-"'" '{print "select * from tablename where ID= " s1 $0 s1}' demo.txt

Hope above helps you.


Thanks,
R. Singh
# 3  
Old 08-25-2015
To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags [code] and [/code] by hand.)



Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums
These 2 Users Gave Thanks to Corona688 For This Post:
# 4  
Old 08-25-2015
Thanks for the quick reply Ravinder.

When i execute the following in my script (named demon_script.sh):

Code:
awk '{print "select * from apsdi-apaction where request_id= " $0}' demo.txt

i get following error in output file:

Code:
SP2-0553: Illegal variable name "=000000000019879".
SP2-0223: No lines in SQL buffer.
SP2-0734: unknown command beginning "grep -Eo '..." - rest of line ignored.
SP2-0734: unknown command beginning "awk '{prin..." - rest of line ignored.
 
no rows selected

how do i troubleshoot this?
This User Gave Thanks to Khushbu For This Post:
# 5  
Old 08-25-2015
Show us more of your script please, we can't tell what's going on without knowing the context.
# 6  
Old 08-26-2015
Well, I'm now trying the following script where I'm trying to run a .sql file from this script. But when i open the output file everything is blank. Why is this so?

Script:
Code:
PATH=..............
export PATH
ORACLE_HOME=...............
export ORACLE_HOME
TNS_ADMIN=...................
export TNS_ADMIN
cut -d " " -f15 demon | sort -n -r > /home/remedy/demo.txt
awk '{print "select * from apsdi-apaction where request_id= "$0}' /home/remedy/demo.txt > /home/remedy/output.sql
sqlplus -S user/pwd@databasename.co > /home/remedy/variable.txt << EOF
@output.sql
EOF

1.demon file contains the string which I'm able to cut for the 15 digit ID.

2.demo.txt file contains the list of IDs as follows:
Code:
000000000019879
000000000039043

3.output.sql file contains following:
Code:
select * from apsdi-apaction where request_id= 000000000039043
select * from apsdi-apaction where request_id= 000000000019879

4.variable.txt file should contain the output of above sql statements, but the file appears blank.

Please let know what changes are to be made in above script to make it work. How do i excute the .sql file from above script. Any other alternative will also work.
# 7  
Old 08-26-2015
There are two issues I see:
A SQL-Statement is terminated by a semicolon, which seems to be missing in output.sql.
A minus sign should be avoided in table names. If it is present the table name has to be put into double quotes. The double quotes make the reference to the table case sensitive, so make sure you reference it the way it was created (the user_tables view tells you the correct name).
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

SQLPLUS command with more than 1 select statement

Hi all, I'm using below code processId=`sqlplus -s ${sysuser}/${syspwd} <<CHK_PROCESS whenever sqlerror exit sql.sqlcode; set head off feedback off echo off pages 0 SELECT PROCESS_ID FROM LSHADMIN.DATA_DOMAIN WHERE DOMAIN_NAME = '${tabname}' ... (8 Replies)
Discussion started by: Pratiksha Mehra
8 Replies

2. Shell Programming and Scripting

Problem with select statement

Hi I have run out of ideas as to why this select doesn't work in a script I am writing. The script sources a file of common functions and I am trying to use a select statement within one of the functions - PS3="Try? " select X in CONT EXIT; do if ] ... (4 Replies)
Discussion started by: steadyonabix
4 Replies

3. Shell Programming and Scripting

Help in executing select query from perl script

Hi, I have a perl snippet that call a select query with a bind variable,when i compile the script I'm unable to get the query output. The same query when i fire in sqlplus fetches few rows. The query takes bit time to fetch results in sqlplus. my $getaccts = $lda->prepare("select distinct ... (1 Reply)
Discussion started by: rkrish
1 Replies

4. Shell Programming and Scripting

Reading a string and passing passing arguments to a while loop

I have an for loop that reads the following file cat param.cfg val1:env1:opt1 val2:env2:opt2 val3:env3:opt3 val4:env4:opt4 . . The for loop extracts the each line of the file so that at any one point, the value of i is val1:env1:opt1 etc... I would like to extract each... (19 Replies)
Discussion started by: goddevil
19 Replies

5. Shell Programming and Scripting

for each value in an array, execute select statement

Hello All, I am new to shell scripting. I am working on Solaris O/S, bash script and sybase programming. I want to loop through multiple values in an array and for each value, I want to select a row from the database. following is the code written for it. output="loop.csv" ... (8 Replies)
Discussion started by: arundhati_s
8 Replies

6. Shell Programming and Scripting

Select variable within a if statement

i want to select a variable created and use it in a if statement, but not getting the desired results LINE='device for 0101a01: lpd://172.25.41.111:515' prt=`echo $LINE | awk '{print $3 }' | cut -c 1-7` echo $prt My if statement to select just what i want.. IFS=$":" while read prt... (11 Replies)
Discussion started by: ggoliath
11 Replies

7. Shell Programming and Scripting

How can i assign an select statement into a variable?

I am trying to assign an select statement into a variable. Can someone hel me with this. example : a='select * from dual' echo $a should give me select * from dual But this is not working. I trying with \ before * and quotes too. (1 Reply)
Discussion started by: rdhanek
1 Replies

8. Shell Programming and Scripting

using SELECT sql statement in shell script

Hi there I have a database on a remote box and i have been using shell script to insert data into it for example, i could have a script that did this SN=123456 n=server1 m=x4140 sql="UPDATE main SET hostname='$n',model='$m' WHERE serial='$SN';" echo $sql |/usr/sfw/bin/mysql -h... (4 Replies)
Discussion started by: hcclnoodles
4 Replies

9. UNIX and Linux Applications

Oracle Select IN statement

If I recall, when I used informix I could do a sql statement like: SELECT Value from Table WHERE ID in (100,200,300); How do I do this in Oracle? I believe I am using Oracle 10 if that matters. Thanks. (1 Reply)
Discussion started by: benefactr
1 Replies

10. UNIX for Dummies Questions & Answers

Reading from a file and passing the value to a select query

Hi all, Here is my problem. I want to read data from a file and pass the variable to a select query. I tried but it doesn't seem to work. Please advise. Example below. FileName='filekey.txt' while read LINE do var=$LINE print "For File key $var" ${ORACLE_HOME}/bin/sqlplus -s... (1 Reply)
Discussion started by: er_ashu
1 Replies
Login or Register to Ask a Question