If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > How to pipe with a parameter

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-08-05, 03:51
giggi giggi is offline
Registered User
 
Join Date: Mar 2005
Posts: 3
How to pipe with a parameter

Hi there,

I have a simple script that pipes a query to SQL*Plus like this:

ne=`<< END_SQL
SELECT
Names
FROM
MyTable
;
EXIT
END_SQL`

nq=`echo "$ne" |sqlplus -s usr/pwd`

now I'm trying to modify the script in order to use a parameter in the query but I haven't found out a way to do it. Is it possible? Basically the problem is how to pipe into sqlplus a query like the following:

ne=`<< END_SQL
SELECT
Names
FROM
MyTable
WHERE
Names = '&1'
;
EXIT
END_SQL`

Thanks in advance.
Giggi
Reply With Quote
  #2 (permalink)  
Old 03-08-05, 16:13
LKBrwn_DBA LKBrwn_DBA is offline
Registered User
 
Join Date: Jun 2003
Location: West Palm Beach, FL
Posts: 2,456
Cool

Giggi, try this:
Code:
#!/bin/ksh
cat - << END_SQL >ne.sql
set ver off
SELECT EName
FROM EMP
WHERE Deptno = '&1'
;
EXIT
END_SQL

nq=`sqlplus -s scott/tiger @ne.sql 10`

echo "$nq"
__________________
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
Reply With Quote
  #3 (permalink)  
Old 03-08-05, 17:23
giggi giggi is offline
Registered User
 
Join Date: Mar 2005
Posts: 3
Hi LKBrwn_DBA,

thanks for the reply, that works but I'd like to avoid saving the script to a file. Is it possible to have the sql statement stored in a variable and pass it to sqlplus together with the parameter?

Giggi
Reply With Quote
  #4 (permalink)  
Old 03-08-05, 20:54
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,454
Can't you do it like this:
Code:
#!/bin/ksh
sqlplus -s scott/tiger << END_SQL 
set ver off
SELECT EName
FROM EMP
WHERE Deptno = '$1'
;
EXIT
END_SQL
Reply With Quote
  #5 (permalink)  
Old 03-09-05, 13:51
LKBrwn_DBA LKBrwn_DBA is offline
Registered User
 
Join Date: Jun 2003
Location: West Palm Beach, FL
Posts: 2,456
Cool

This will also work:
Code:
#!/bin/ksh
sqlplus scott/tiger <<END_SQL
def p1=$1
set ver off
SELECT EName
FROM EMP
WHERE Deptno = '&&p1'
;
EXIT
END_SQL
__________________
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
Reply With Quote
  #6 (permalink)  
Old 03-10-05, 16:49
giggi giggi is offline
Registered User
 
Join Date: Mar 2005
Posts: 3
Thanks for the hints! Now it's ok.

Giggi
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On