Passing variable from file to sql from script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Passing variable from file to sql from script
# 1  
Old 11-03-2014
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
Code:
45456546456
45464564565
67854353454
67657612132

Number of records are vary every time.

and i need to check the status of all these acc in our database.

Code:
select * from tablename where acc in ( all records which are in acc.txt)

my unix script is

Code:
sqlplus -s username/password @acc.sql

Please let us know how i can pass all acc records which are present in acc.txt from script to sql

i know i can pass parameter like this, but that i know that i can do if i know number of record is fixed. Please let us know how i can do this
# 2  
Old 11-03-2014
Quote:
Originally Posted by pallvi_mahajan
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
Code:
45456546456
45464564565
67854353454
67657612132

Number of records are vary every time.

and i need to check the status of all these acc in our database.

Code:
select * from tablename where acc in ( all records which are in acc.txt)

my unix script is

Code:
sqlplus -s username/password @acc.sql

Please let us know how i can pass all acc records which are present in acc.txt from script to sql

i know i can pass parameter like this, but that i know that i can do if i know number of record is fixed. Please let us know how i can do this
well, first up, don't pass the password on the command line like that, it can be "seen" by anyone using ps -ef ..

There's a lot of other options:

1) externally identified id.
2) sqlplus -s /nolog
connect user@password
...
etc.

Anyway, to your question:

first, stuff the count into a unix variable:

Code:
fileCOUNT=`wc -l acc.txt`

then when you invoke your sql command, pass it in:

Code:
sqlplus -s /nolog << EOF         
   connect <user>/<password>
   @acc.sql   ${fileCOUNT}
   exit
EOF

[edit]
just make sure in your acc.sql script, you capture the input:

Code:
select &1 from dual;

(ie use &1 in a sql statement to use it)

or in pl/sql:

Code:
declare
  ln_input   number := nvl('&1',0);
begin
end;
/

(note that will throw an error if you pass a string in Smilie Up to you how to protect/handle all those side cases)


[/edit]
# 3  
Old 11-03-2014
but how i can call this in .sql, how $filecount replace in .sql file
Code:
select * from tablename where acc in (
45456546456, 45464564565, 67854353454, 67657612132)

# 4  
Old 11-03-2014
Try
Code:
select * from tablename where acc in ($(paste -sd, file))

# 5  
Old 11-03-2014
HI Rudic,

what does
Code:
($(paste -sd, file))


do.
# 6  
Old 11-03-2014
It will convert
Code:
45456546456
45464564565
67854353454
67657612132

in file to
Code:
45456546456,45464564565,67854353454,67657612132

# 7  
Old 11-03-2014
Hi Rudic,

is this command, can i read some tutorial on this, please let me know where i can study this, i did google, not getting anything on this.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

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

3. Shell Programming and Scripting

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: Your help is much appreciated. Thanks, shil (2 Replies)
Discussion started by: infintenumbers
2 Replies

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

5. Shell Programming and Scripting

Replace a var in sql file with shell script variable

I have a requirement where i have a sql file (filetext.sql). This file contains a variable ss_code. Now in a shell script im trying to replace the variable ss_code with a value contained in the shell script variable MTK_DC..tried the below in the script MTK_DC="mit,cit,bit" OUT=`awk -v... (4 Replies)
Discussion started by: michaelrozar17
4 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

passing arguments to sql script

Hi Gurus, i have one requirement in unix script, i have a file called abc.txt in that few lines are there with the empid, i need to read each line and pass to .sql script. ex: abc.txt 2345 2346 1243 1234 i need to pass these arguments to .sql script rom unix ex: select * from... (1 Reply)
Discussion started by: Devendar
1 Replies

8. Shell Programming and Scripting

Passing argumnets from shell script to sql

hi I all , I have sql statment in my shell script , I pass two argument to the script I need to pass the this two arguments to the sql statment example : runsql.sh "1" "2" sql : updat tables_x set y=0 where A=:x should subsituted by "1" and B=:y shuold subsituted bt "2"... (1 Reply)
Discussion started by: habuzahra
1 Replies

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

10. 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
Login or Register to Ask a Question