Call sql script from UNIX shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Call sql script from UNIX shell script
# 1  
Old 04-25-2015
Question Call sql script from UNIX shell script

I know this question is out there in many forums, but I tried all the combinations in vain.

I'm basically trying to call a sql script from a shell script.

Below is my sql script (plsql.sql)

Code:
DELCARE
v_empno NUMBER := '&empno';
BEGIN
select ename,sal from emp where empno = v_empno;
dbms_output.put_line('Inside the plsql file');
END;

This is my unix shell script - I'm caling the plsql.sql file with the parameter passed(97882). I don't get any output. at least I should be able to view the dbms output if not for the sql query inside the sql script.

Code:
#!/usr/bin/ksh
sqlplus -s sam/olo01 << HERE
@plsql.sql 97882;
HERE

What am I missing here ? It doesn't seem to work. I tried removing the <<HERE condition, but it when executing the shell script, it keeps prompting for an input(I am not sure why) and never returns the control to the bash screen(-bash-3.2-$)

Moderator's Comments:
Mod Comment Use code tags, thanks.

Last edited by zaxxon; 04-27-2015 at 11:30 AM..
# 2  
Old 04-27-2015
Hello,

please use code tags when posting code or sample data.

There are several problems I see with your code:
The pl/sql-script does not know what &empno is. The arguments you pass are addressed like positional parameters in a shell script.
pl/sql is designed for background processing. Queries inside pl/sql-blocks are never displayed. Put the query outside the pl/sql-block to see its result.
You have to enable serveroutput to see the output of dbms_output.put_line.
So your plsql.sql script should look like this:
Code:
select ename,sal from emp where empno = &1;
set serveroutput on
BEGIN
   dbms_output.put_line('Inside the plsql file');
END;

# 3  
Old 04-27-2015
the DBMS_OUTPUT pacakge requires:
Code:
set serveroutput on size 100000

in order to write to the terminal - the 100000 is the max number of bytes displayed.
This is arbitrary - but do not make it too small.
The output only appears after the sql has completed.

Note: DECLARE is misspelled in your example.

cero's code will work, I think. It has to be run using sqlplus from the command line. It cannot be used in a trigger because triggers and stored procedures do not have a controlling terminal when they are executing.
# 4  
Old 04-27-2015
Can I also suggest that you take the credentials out of your sqlplus command line. Anyone running a simple ps will be able to see them whilst your database connection is active.

It might only be a short time, but if this account is a DBA (which I'm guessing that it is) then you are effectively shouting the out the number for a combination lock on your most secure safe. If no-one is listening, then you get away with it. If someone hears, it depends on their integrity if they do something with it.



Robin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to call sql file

hi , the below script contains sql query and after executed it sends the output of the query (output.txt) to an email body with conditional subject line based on the output of all_counts_match.txt. i want to make this script generic so that it can accept the sql file as parameter and can... (5 Replies)
Discussion started by: itzkashi
5 Replies

2. Shell Programming and Scripting

How to call SQL Loader in shell script?

HI Experts, I am pretty new to scripting and i need to create a perl or shell script which should fetch a file from local directory and insert the data into a table using sql loader. This will be later added to chron job to run daily and fetch all files and load them into the table. Also i... (1 Reply)
Discussion started by: sam1234
1 Replies

3. Shell Programming and Scripting

Need to write shell script for my .sql file call

Hi Guys, I need to write a simple shell script which will generate a .csv file/report by calling .sql file inside a shell script. Can somebody help me on this. Thanks in advance! Regards, LK (7 Replies)
Discussion started by: lakshmanraok117
7 Replies

4. UNIX for Advanced & Expert Users

call sql through shell script

Hi i am not able to connect sqlplus my script is as follows $ORACLE_HOME/bin/sqlplus << ! > /tmp/extract/DM.txt and output is SQL*Plus: Release 11.1.0.7.0 - Production on Wed Jan 18 02:53:54 2012 Copyright (c) 1982, 2008, Oracle. All rights reserved. Enter user-name: t175481... (1 Reply)
Discussion started by: tushar_spatil
1 Replies

5. Shell Programming and Scripting

call shell script from pl/sql block

Hi Experts, I want to call script_name.ksh as many time as id in customer table and also pass it as a parameter to script. someting Like below. for i in select id from customer do ./script_name.ksh $i & done I have figured out how to have ID from customer but now how to call... (3 Replies)
Discussion started by: Opamps123
3 Replies

6. Shell Programming and Scripting

how to call shell script from pl/sql loop

Hello, I am doing a shell script which contain a pl/sql loop to search for 3 values, i would like to call another shell script inside this sql loop each time it find the values. so how can i call shell script from pl/sql using its variables, any idea? Here is idea about the code: my... (1 Reply)
Discussion started by: rosalinda
1 Replies

7. Shell Programming and Scripting

how can i call a shell script from pl/sql

I would like to call the shell script from pl/sql and i need to uses the value returned by the shell script in pl/sql procedure. can any one suggest me how can i do that? (3 Replies)
Discussion started by: rajesh.P
3 Replies

8. UNIX for Dummies Questions & Answers

how can a call shell script from pl/sql

I like to call a shell script from pl/sql proceduere and i have to use the shell script return value in that procedure. i am using oracle 9i and cygwin. can any one suggest me how can i do this (0 Replies)
Discussion started by: rajesh.P
0 Replies

9. Shell Programming and Scripting

how can I call a pl/sql funciton in unix script

who can show me how to call pl/sql function or precudure in unix script.. cheers, (6 Replies)
Discussion started by: YoYo
6 Replies

10. Shell Programming and Scripting

How to call pl/sql in unix script

sample code as following: test_sql(){ #test#echo test_sql str=`$ORACLE_BIN/sqlplus -s $user/$passwd <<EOM set verify off set heading off set feedback off #--------start pl/sql { DECLARE CURSOR pah_cs IS select id from table where letter = 'abcd';... (6 Replies)
Discussion started by: YoYo
6 Replies
Login or Register to Ask a Question