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

Thanks and Regards,
Ganapathi. :confused:

shell_var=`$ORACLE_HOME/bin/sqlplus -S test/passwd@service > ${logfile} 2>&1 << EOF
SET FEED OFF;
SET TERMOUT ON;
SET VERIFY OFF;
SET ECHO OFF;
SET HEAD OFF;
SELECT sysdate from dual FROM DUAL;
EXIT
EOF`

echo $shell_var

Roopla, Thanks a lot. It worked for me with some fine tuning as below.

shell_var=`sqlplus -s scott/tiger << !
SET HEAD OFF;

SELECT sysdate from dual;
EXIT
!`
echo $shell_var

But still I dont know how to pass multiple values from SQL to Shell script.
Any help in this regard will definitely help all of us to enhance our knowledge.

Once again Many thanks for your sincere help, Roopla.

With Regards,
Ganapati. :wink:

check this

Great help and many thanks for your guide 'Radoulov'.

Below code is working perfectly to pass multiple values from SQL to Shell script.

printf "%s\n" "set pages 0 serverout on feed off" \
"select ENAME from emp where ENO=7021;" \
"select sysdate from dual;" \
"select min(sal) from emp;" \
"select max(sal) from emp;" \
"select avg(sal) from emp;" \
| sqlplus -s scott/tiger \
| { read var1; read var2; read var3; read var4; read var5;
}
echo "Employee Name is $var1"
echo "Date is $var2"
echo "Minimum of Salary is $var3"
echo "Maximum of Salary is $var4"
echo "Average of Salary is $var5"

But still I've one more requirement.....,
What is the case for the same query where if I want assign more values to var1 ?

printf "%s\n" "set pages 0 serverout on feed off" \
"select ENAME from emp;" \
"select sysdate from dual;" \
"select min(sal) from emp;" \
"select max(sal) from emp;" \
"select avg(sal) from emp;" \
| sqlplus -s scott/tiger \
| { read var1; read var2; read var3; read var4; read var5;
}
echo "Employee Name is $var1"
echo "Date is $var2"
echo "Minimum of Salary is $var3"
echo "Maximum of Salary is $var4"
echo "Average of Salary is $var5"

Please help me last time.

With Regards,
Ganapati. :confused:

You can try somthing like that (not tested) :

printf "%s\n" "set pages 0 serverout on feed off" \
"select sysdate from dual;" \
"select min(sal) from emp;" \
"select max(sal) from emp;" \
"select avg(sal) from emp;" \
"select ENAME from emp;" \
| sqlplus -s scott/tiger \
| { read read var2; read var3; read var4; read var5;
     var1=""
     while read emp
     do
         var1="$var1$emp "
     done
}
echo "Employee Name is $var1"
echo "Date is $var2"
echo "Minimum of Salary is $var3"
echo "Maximum of Salary is $var4"
echo "Average of Salary is $var5"

Jean-Pierre.

Do it all in the database, if possible.
If you _really_ have to mix sql/plsql and shell programming:

  • as array in bash:
$ var1="($(printf "%s \n" "set pages 0 feed off" \
> "select ename from emp;"|sqlplus -s scott/tiger))"; \
> printf "The first element in \$var1 array is: %s, \
> the second: %s and so on ...\n" \
> "${var1[1]}" "${var1[2]}"
The first element in $var1 array is: ALLEN, the second: WARD and so on ...
  • as a single variable:
$ var1="$(printf "%s \n" "set pages 0 feed off" \
> "select ename from emp;"|sqlplus -s scott/tiger)"; \
> echo "Quoted: $var1"; \
> echo "Unquoted:" $var1
Quoted: SMITH
ALLEN
WARD
JONES
MARTIN
BLAKE
CLARK
SCOTT
KING
TURNER
ADAMS
JAMES
FORD
MILLER
Unquoted: SMITH ALLEN WARD JONES MARTIN BLAKE CLARK SCOTT KING TURNER ADAMS JAMES FORD MILLER

I'm sure that it will be better
if you use only one programming language.

Radoulov,

Your code is OK if I use only one variable. What the case for my below script?
== Uncoted $var1

printf "%s\n" "set pages 0 serverout on feed off" \
"select ENAME from emp;" \
"select sysdate from dual;" \
"select min(sal) from emp;" \
"select max(sal) from emp;" \
"select avg(sal) from emp;" \
| sqlplus -s scott/tiger \
| { read var1; read var2; read var3; read var4; read var5;
}
echo "Employee Name is" $var1
echo "Date is $var2"
echo "Minimum of Salary is $var3"
echo "Maximum of Salary is $var4"
echo "Average of Salary is $var5"

Result is as below:
Employee Name is SMITH
Date is ALLEN
Minimum of Salary is WARD
Maximum of Salary is JONES
Average of Salary is MARTIN

== For coted $var1

printf "%s\n" "set pages 0 serverout on feed off" \
"select ENAME from emp;" \
"select sysdate from dual;" \
"select min(sal) from emp;" \
"select max(sal) from emp;" \
"select avg(sal) from emp;" \
| sqlplus -s scott/tiger \
| { read var1; read var2; read var3; read var4; read var5;
}
echo "Employee Name is $var1"
echo "Date is $var2"
echo "Minimum of Salary is $var3"
echo "Maximum of Salary is $var4"
echo "Average of Salary is $var5"

Result is same as previous case (which is incorrect):
Employee Name is SMITH
Date is ALLEN
Minimum of Salary is WARD
Maximum of Salary is JONES
Average of Salary is MARTIN

Result which Iam expecting is:
Employee Name is SMITH ALLEN WARD JONES MARTIN BLAKE CLARK
Date is 30-JAN-07
Minimum of Salary is 12000
Maximum of Salary is 99999
Average of Salary is 24571.2857

Please help me again with your knowledge.

With Regards,
Ganapati :o

What's wrong with this:

var1="$(printf "%s \n" "set pages 0 feed off" \
"select ename from emp;"|sqlplus -s scott/tiger)"; \
printf "%s;\n" "set pages 0 serverout on feed off" \
"select sysdate from dual" \
"select min(sal) from emp" \
"select max(sal) from emp" \
"select avg(sal) from emp" \
| sqlplus -s scott/tiger  |{ read var2; read var3; read var4; read var5;
echo "Employees' Names Are: " $var1
echo "Date is $var2"
echo "Minimum of Salary is $var3"
echo "Maximum of Salary is $var4"
echo "Average of Salary is $var5"
}

For example:

$ var1="$(printf "%s \n" "set pages 0 feed off" \
> "select ename from emp;"|sqlplus -s scott/tiger)"; \
> printf "%s;\n" "set pages 0 serverout on feed off" \
> "select sysdate from dual" \
> "select min(sal) from emp" \
> "select max(sal) from emp" \
> "select avg(sal) from emp" \
> | sqlplus -s scott/tiger  |{ read var2; read var3; read var4; read var5;
> echo "Employees' Names Are: " $var1
> echo "Date is $var2"
> echo "Minimum of Salary is $var3"
> echo "Maximum of Salary is $var4"
> echo "Average of Salary is $var5"
> }
Employees' Names Are:  SMITH ALLEN WARD JONES MARTIN BLAKE CLARK SCOTT KING TURNER ADAMS JAMES FORD MILLER
Date is 30-JAN-07
Minimum of Salary is 800
Maximum of Salary is 5000
Average of Salary is 2073.21429

With one connection:

{ printf "set pages 0 feed off\n";
  printf "select %s from %s;\n" \
  "ename||' \ '" "emp" \
  "'
'||sysdate" "dual" \
  "min(sal),max(sal)||'
'||round(avg(sal),2)" "emp" 
} | sqlplus -s scott/tiger \
| { read var1; read var2; read; read var3; read var4; read var5;
printf "%s\n" "Employee Name is $var1" \
"Date is $var2" \
"Minimum of Salary is $var3" \
"Maximum of Salary is $var4" \
"Average of Salary is $var5"
}

But I repeat: it's OK only if you're doing it for learning purposes.
Writing a report like this is quite trivial and straightforward in sql.

Great help to enhance my knowledge from you 'radoulov'.
I never forget your help. Pls keep in touch.

With Love and Regards,
Ganapati. :slight_smile: