How to Assign SQL Query Results to Variables in Linux?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to Assign SQL Query Results to Variables in Linux?
# 1  
Old 12-24-2012
RedHat How to Assign SQL Query Results to Variables in Linux?

Hi,

I am new to linux...

How to Assign SQL Query Results to Variables in Linux,i want ti generate it in param files, Can anyone please explain me.

Ex:
Code:
Code:
SQL> Select * from EMP;
O/P: Emp_No	Emp_Name
       1	       AAA
       2	       BBB
       3	       CCC

and I want expected output is.....

V1=1
V2=AAA these both variables should store in one flat file...and other two records need to store in another flat file....

--Thanks in Advance
$ravan

Last edited by Scott; 12-24-2012 at 10:58 AM.. Reason: Code tags
# 2  
Old 12-24-2012
Code:
#!/bin/bash

# Connect to DB and spool query result into a CSV file
sqlplus -s user/password@instance << EOF
set echo off head off feed off pagesize 0 trimspool on linesize 1000 colsep ,
spool output.csv
select emp_no, emp_name from emp;
spool off;
exit;
EOF

# Using while loop read values into variables from CSV file and create flat file for each records
counter=1
while IFS=, read V1 V2
do
   echo "${V1} ${V2}" > param_${counter}.txt
   counter=$(( counter + 1 ))
done < output.csv

# 3  
Old 12-26-2012
RedHat How to Assign SQL Query Results to Variables in Linux?

Hi bipinajith

Thank you for quck reply...i have tried but i need the output like...

Code:
$PMFailureEmailUser=abcd@gmail.com,ssssss@gmail.com.com
$DBConnection_Bmd=Orcl
$$DBConnection_Bmd=Msg_ETL_Orcl
$$SOURCE_NAME='EMPLOYEE'
$$LOAD_TYPE='DELTA'
$$SUBJ_NAME=MICROSOFT
$$Script_Path=D:/PMRootDir/Scripts/CallProfile.sh
$$IS_TEMP_FLAG=N
$$Start_Task_Name=wkl_Map_PROCESS_LOG
$$Restart_Workflow=N
$$Param_File_Path=D:/PMRootDir/Param/masaga
$$Create_Param_Wf_Name=wf_Create_Paramfiles
$$Target_Schema_Type=C
$$Where_Clause=
$$Trgt_Load_Order=2

i need to assign each coloumn valuse to one variable like above and all these values need to append in one file (Param_Create.prm)...
i am trying with your previous example but i am getting error with '$$' symbols please tell me is there any other way we can concat that symobl to output result before appending to file...
Please help me on this....

Thx..Sravan

Last edited by Franklin52; 12-26-2012 at 11:38 AM.. Reason: Code tags
# 4  
Old 12-26-2012
If you want to write $ sign to a file then you have to escape this sign. Otherwise it will be consider as a variable. See the difference below:
Code:
bash# echo "$sravana"

bash# echo "\$sravana"
$sravana

# 5  
Old 12-26-2012
Hi bipinajith

I am trying to execute this script, it is executing but it is not generating values to actual flat file, but it wil generate values into .log file and .csv file, Please correct me if i went wrong in the code....
Code:
#!/bin/bash

# Connect to DB and spool query result into a CSV file
filepath="/sqlany_data/infa_shared/Scripts/masaga"
DBUSER='etl_user'
DBPWD='etl_dev'
DB='keymsga'
sqlplus -s $DBUSER/$DBPWD@$DB <<EOF  > ${filepath}/Masaga_Param_1226.log

set echo off head off pagesize 0 trimspool on linesize 1000 colsep ,
spool Masaga_Param_var.csv
select  EMAIL_USER_NAME,DB_CONN_SCHEMA  from Msg_Ks_Paramfiles where WORKFLOW_NAME='wf_Msg_Ks_Start_Build_Feat_Delta';

spool off;
exit;
EOF

while IFS=, read USER_NAME  DB_CONN_SCHEMA

do
   echo "{\$USER_NAME}"  > param_Ks_Build_Feat.txt
   echo "{\$$DB_CONN_SCHEMA}" > param_Ks_Build_Feat.txt
done < Masaga_Param_var.csv

and how i should pass query if i want o/p variable value like " $$DB_CONN_SCHEMA=ORACLE"...
Thanks...
Sravan

Moderator's Comments:
Mod Comment Please use code tags next time for your code and data.

Last edited by Franklin52; 12-26-2012 at 11:38 AM..
# 6  
Old 12-26-2012
You don't need that while loop. You can format the result in the SQL itself and spool it to file: param_Ks_Build_Feat.txt Here is the modified code:
Code:
sqlplus -s $DBUSER/$DBPWD@$DB << EOF
set echo off head off pagesize 0 trimspool on linesize 1000 
spool param_Ks_Build_Feat.txt
select '$USER_NAME=' || EMAIL_USER_NAME || '$$DB_CONN_SCHEMA=' || DB_CONN_SCHEMA 
from Msg_Ks_Paramfiles where WORKFLOW_NAME='wf_Msg_Ks_Start_Build_Feat_Delta';
spool off;
exit;
EOF

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

Storing the SQL results in array variables

Requirement 1) I need to execute 15 SQL queries in oracle through linux script. All these query results needs to be stored in array variables. Requirement 2) And these 15 queries needs to be executed in parallel. Requirement 3) Once all the queries executed then the shell script should... (3 Replies)
Discussion started by: Niranjancse
3 Replies

3. UNIX for Dummies Questions & Answers

Getting values of 2 columns from sql query in UNIX variables

Hi, I have connected to oracle database with sqlplus -s / <<EOF select ename, age from emp where empid=1234; EOF I want to save the values of ename and age in unix shell variables. Any pointers would be welcome.. Thanks in advance!!1 Cheers :):):):) (1 Reply)
Discussion started by: gonchusirsa
1 Replies

4. Shell Programming and Scripting

How to store results of multiple sql queries in shell variables in ksh?

Hi, I have a script where I make a sqlplus connection. In the script I have multiple sql queries within that sqlplus connection. I want the result of the queries to be stored in shell variables declared earlier. I dont want to use procedures. Is there anyway else. Thanks in advance.. Cheers (6 Replies)
Discussion started by: gonchusirsa
6 Replies

5. Shell Programming and Scripting

Multiple Query Results to Variables

Hello, I am very new to shell scripting and I am not sure of how best to handle the following scenario. I need to query a list of values from a table. I need to store those results and use them to selectively delete values in yet another table in a separate database. I do know how to store the... (3 Replies)
Discussion started by: flowervz
3 Replies

6. Shell Programming and Scripting

SQL/Plus in a coprocess example. Also saves query results into shell variables

While assisting a forum member, I recommended running SQL/Plus in a coprocess (to make database connections and run a test script) for the duration of his script rather than starting/stopping it once for every row in a file he was processing. I recalled I made a coprocess example for folks at... (2 Replies)
Discussion started by: gary_w
2 Replies

7. Shell Programming and Scripting

Oracle Query results to be stored in variables using unix

I want to store the sql query output into a variable #!/bin/ksh ORACLE_SID=DB01; export ORACLE_SID; export FILE_PATH=/home/asg/Tmp # Order Checking echo " removing old files " rm $FILE_PATH/Malformed_Order.txt echo " Enter the Malformed Order ....!" read orders echo "Regrade... (5 Replies)
Discussion started by: Nareshp
5 Replies

8. Shell Programming and Scripting

Oracle Query results to be stored in variables

Hi I would like to know if there is a way to just have one ORACLE connection established, using which we can execute different queries and store the results under different variables. For e.g the following uses to two silent ORACLE connection to store the result under two different... (4 Replies)
Discussion started by: ashokjaysiv
4 Replies

9. UNIX for Advanced & Expert Users

Set shell variables from SQLPLUS query results

Hi All, I needed to get the result of two sqlplus queris into shell variables. After days of looking for the ultimate solution to this problem.. i found this... sqlplus -s USER/PASS@DB <<EOF | awk '{if(NR==1) printf("%s ", $1); if(NR==2) printf("%s ", $1);}' | read VAR1 VAR2 set head off... (2 Replies)
Discussion started by: pranavagarwal
2 Replies

10. UNIX for Dummies Questions & Answers

sql query results in unix shell script

Hi I want to get the a field from a SQL query into unix shell script variable. the whole situation is like this. 1. Opened a cursor to a table in DB2 databse. 2. Fetching individual rows with the help of cursor. 3. Each row has 4 fields. I want each of the field in individual shell... (1 Reply)
Discussion started by: skyineyes
1 Replies
Login or Register to Ask a Question