create a spool file based on values passed from korn shell to sql script


 
Thread Tools Search this Thread
Top Forums Programming create a spool file based on values passed from korn shell to sql script
# 1  
Old 11-10-2011
create a spool file based on values passed from korn shell to sql script

this is my issue.
4 parameters are passed from korn shell to sql script.
Code:
 
parameter_1=  varchar2 datatype or no value entered my user.
parameter_2=  number datatype or no value entered my user.
parameter_3= number datatype or no value entered my user.
parameter_4= number datatype or no value entered my user.

But I have to use these 4 parameters in the select statement. And this is my problem. How to initialize these parameters to get all rows when the user does not enter any value?
# 2  
Old 11-10-2011
And what values do you want to pass to your database in case the user doesn't pass a value?
# 3  
Old 11-10-2011
may be anything . Can I pass an empty string " " or any special character ?
# 4  
Old 11-10-2011
You can pass whatever you want. The result of your SQL statement depends on those values, so they are rather important.
# 5  
Old 11-11-2011
Okay. If i pass an empty string as in " " , can you please tell how can I write the select statement that will work no matter what value(s), the user enters.
The user can pass 4 parameters or choose not to pass any value for the 4 paramaters or just pass 1 st and 2nd parameters etc .
# 6  
Old 11-11-2011
You could use something like this:

Code:
_where=
for p; do
  [ -n "$_where" ] &&
    _where="$_where and $p" ||
      _where="where $p"    
done
      
select 
  col1, col2 ..
from
  table_name
"$_where";

And invoke the script like this:

Code:
./script_name 'col1 = val1' 'col2 = val2' ...

Consider the following example and output:

Code:
% cat s
_where=
for p; do
  [ -n "$_where" ] &&
    _where="$_where and $p" ||
      _where="where $p"    
done
      
cat << !
select 
  col1, col2 ..
from
  table_name
$_where;
!


Code:
% ksh s "col1 = 'val1'" 'col2 = val2'
select 
  col1, col2 ..
from
  table_name
where col1 = 'val1' and col2 = val2;

You could invoke it without arguments too and it will generate valid SQL:

Code:
% ksh s                              
select 
  col1, col2 ..
from
  table_name
;

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Append date to sql*plus spool (log) file in shell script

SQL*Plus version : 11.2.0.4 OS : Oracle Linux 6.5 SQL*Plus is a client application to connect to oracle database. The log file for this tool is generated via spool command as shown below. I am trying to append date ( $dateString ) to spool file as shown below. $ cat test2.sh #!/bin/bash... (4 Replies)
Discussion started by: kraljic
4 Replies

2. Shell Programming and Scripting

Shell script to create runtime variables based on the number of parameters passed in the script

Hi All, I have a script which intends to create as many variables at runtime, as the number of parameters passed to it. The script needs to save these parameter values in the variables created and print them abc.sh ---------- export Numbr_Parms=$# export a=1 while do export... (3 Replies)
Discussion started by: dev.devil.1983
3 Replies

3. Shell Programming and Scripting

How to create SQL statement out of data using shell script?

Table TAB1 contains following example data (its a tree sitting in table data format & its driven based CHILD & PARENT column pick the RULE condition to generate the below SQL: CHILD PARENT SS MID MNM VNM RULE FLG 1 ? S1 ? ? V1 rule004 I 2 1 S1 ? ? V1 0 Z 3 1 S1 ? ? V1 1 Z ... (6 Replies)
Discussion started by: gksenthilkumar
6 Replies

4. Shell Programming and Scripting

Korn shell script - SQL statement challenges

Hi scripting experts. I have some coding challenges that I'm hoping you can help me out. I have one file#1 that contains the following sql statement that spans over multiple lines: sql Select /*+ use_has(a,b) */ * from customer a, customer_address b where a.id = b.id... (1 Reply)
Discussion started by: pchang
1 Replies

5. Shell Programming and Scripting

pass null value to sql script from korn shell script

There are 4 parameters that I have to pass from korn shell to sql script. 1) I have to check if $1 , $2 , $3 and $4 are null values or not . How can I do that ? 2) Once its determined that these values are null (in the sense they are empty) how can I pass null values to sql script... (11 Replies)
Discussion started by: megha2525
11 Replies

6. Shell Programming and Scripting

Passing filename dynamically in SPOOL of SQL*PLUS in shell script

Hi all, I am executing shell script in which I am using SQLLDR In this SQLLDR I am passing text file having PL/SQL script. This script will produce some formated output, this output I have to spool in another text file. Currently I have given this in script file as following Spool... (2 Replies)
Discussion started by: shekharjchandra
2 Replies

7. Shell Programming and Scripting

How to pass arguments to SQL file passed in shell script?

Hi, I am using SYBASE database. in my script i am connecting to DB via using isql. isql -U${S_USER} -S${S_SERV} -D${S_DB} -P${S_PWD} -b0 -w3000 -h0 -s"|" -i${MYDIR}/ABC.sql -oXYZ.txt << FINSQL i am taking a ABC.sql file to use the queries written in it and storing the output in... (3 Replies)
Discussion started by: dazdseg
3 Replies

8. UNIX for Advanced & Expert Users

Accessing PL/SQL OUT variables in Korn Shell Script

Hello All, I was just wondering if there is any direct way to access PL/SQL OUT variables from Korn Shell Script. I could already figure out how to return a single value back from PL/SQL to Shell Script (using bind variable). But, what if we want to return multiple values? One option I... (4 Replies)
Discussion started by: bright_future
4 Replies

9. Shell Programming and Scripting

passing values from sql to shell script

Hi guyz, Posting a thread after a long time. I want to pass two variables to unix shell script from sql script. Note: I am calling sql script from unix script. sql script has 2 variables one is the return code for status of program run and second one email flag. I don't know how to capture... (3 Replies)
Discussion started by: sachin.gangadha
3 Replies

10. Shell Programming and Scripting

Korn Shell Script - Read File & Search On Values

I am attempting to itterate through a file that has multiple lines and for each one read the entire line and use the value then to search in other files. The problem is that instead of an entire line I am getting each word in the file set as the value I am searching for. For example in File 1... (2 Replies)
Discussion started by: run_unx_novice
2 Replies
Login or Register to Ask a Question