Passing a string variable from Unix to Sql Plus


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Passing a string variable from Unix to Sql Plus
# 1  
Old 07-31-2012
Passing a string variable from Unix to Sql Plus

Hi Guys,

I am trying to pass a string variable from Unix shell script to sqlplus as a parameter.

I have tried using single quotes with the variable name but it does not work. Please help me with it.

I am using BASH.

My code:
Quote:
for line in $(head -$to_line table_list.txt | tail -$no_lines)
do
echo $line
sqlplus -S -L $from_db >> $sqlscript <<EndSQL1

SELECT LTRIM(MAX(SYS_CONNECT_BY_PATH (col_name,', ')),',') FROM (
SELECT column_id,CASE WHEN data_type LIKE 'TIMESTAMP%' THEN 'TRUNC(' ||column_name || ')'
ELSE column_name END col_name FROM (SELECT * FROM all_tab_columns
WHERE table_name= '$line'
) ORDER BY column_id ) START WITH column_id = 1 CONNECT BY PRIOR column_id = column_id -1;

exit
EndSQL1
Your help is much appreciated.


Thanks,
shil

Last edited by infintenumbers; 07-31-2012 at 05:34 PM.. Reason: Minor typo in code tags.
# 2  
Old 08-01-2012
Does the script threw any error..? If so please post the same. And there is a semicolon miss in your code exit;
Below is a sample code to login to sqlplus and querying by passing a parameter
Code:
#!/bin/bash
line=sysdate
cnt=2

sqlplus -s user_t/user_t@ORADEV <<EOF > $(echo $PWD)/outfile.txt
select $line from dual;
select * from emp where emp_id='$cnt';
exit;
EOF

# 3  
Old 08-01-2012
The mistake I did was:

I created the text file (table_list.txt) from windows. Now I created from UNIX and it works.

The simple mistake costed me a day. Smilie

Thanks for all your help.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Passing variable from file to sql from script

Hi Friend, I have one file in which some number are mentioned and number of lines are vary every time And i need to pass that number to my sql command from script. Suppose i have file acc.txt 45456546456 45464564565 67854353454 67657612132 Number of records are vary every time.... (20 Replies)
Discussion started by: pallvi_mahajan
20 Replies

2. Shell Programming and Scripting

Passing string from SQL to a BASH script

OS Solaris 10, DB oracle 10g Hello, We currently have a BASH script that runs and moves image files from a remote server to the local db server. A snippet of the code shows that we are picking up all Images that are 'mtime -1' some code... for file in `ssh user@10.200.200.10 'find... (3 Replies)
Discussion started by: JonP
3 Replies

3. Shell Programming and Scripting

Passing variable to sql

How to pass variable to sql file. Im tryin in two ways, Method 1: my.sql select * from table where col1 = '$1' and col2 = 'text'; Method 1execute: dbaccess database my.sql $var Method2: select * from table col1 in (`cat inputfile`) and col2 = 'text'; method... (2 Replies)
Discussion started by: Roozo
2 Replies

4. Shell Programming and Scripting

Passing string as variable(s) in bash

I'm trying to write a basic bash script that takes input you give (what directory, if any, what name, if any ....) and passes the information to find. I'm trying to just create a string with all variables and then pass it to find. So far I have this extremely simple: #!/bin/bash -f ... (2 Replies)
Discussion started by: Starting_Leaf
2 Replies

5. Shell Programming and Scripting

passing an unix variable to an XML

I need help I have a unix command : VERSION=$(ls -d /vsn/v12.??.??.?? | sort | tail -1) when i do echo $VERSION, i get the exact value, i want. Now i want to use this variable and pass it to an xml. How can i do that? (1 Reply)
Discussion started by: samk
1 Replies

6. Shell Programming and Scripting

Passing sql as parameter to unix function

Hi, I have a function which connects to the db and runs the sql. it works fine when I run it like: function "select empname from emp;" but when I try to pass the sql string to a variable which in turn in fed to the function , it throws error. please advise. Thanks, Arnie. (1 Reply)
Discussion started by: itsarnie
1 Replies

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

8. UNIX for Advanced & Expert Users

passing value to a variable in a SQL

Hi Folks, This is a small chunk of the bigger problem which i am facing and some help here will help me resolve the rest of the issue. Problem is that i need to pass the value of a variable from a shell script to a SQL query (infact a lot of SQL's) i have the following solution but somehow... (4 Replies)
Discussion started by: kamitsin
4 Replies

9. Shell Programming and Scripting

Passing PL/SQL variable value to Shell Varible

Hi my UNIX Friends, Im calling some SQL scripts through Unix Shell scripting. How do I export the value of PL/SQL variable value into a Unix shell script variable? Also could any one inform me about the 'search' and 'cut' utility of PL/SQL (like 'grep' and 'cut' in Shell scripting). ... (10 Replies)
Discussion started by: ganapati
10 Replies

10. Programming

Passing C Variable To Unix

I want to pass a variable set in my c program to a shell script (which will also be invoked or initiated from the same C program using the C's system command). Is it possible ? :confused: (3 Replies)
Discussion started by: kapilv
3 Replies
Login or Register to Ask a Question