unix variables from sql / pl/sql


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting unix variables from sql / pl/sql
# 1  
Old 05-06-2009
unix variables from sql / pl/sql

How do I dynamically assign the below output to unix shell variables
so I can build a menu in a shell script?

Example:

var1 = 1 var2= SYSTEM
var3 = 2 var4= UNDOTBS1

and so on, then in the shell script I can use the variables to build a menu.




set serveroutput on

declare
v_counter number(2):=0;
v_tbs dba_tablespaces.tablespace_name%type;

cursor tbs_cur is
select tablespace_name
from dba_tablespaces
where tablespace_name not in ('TEMP');

begin
open tbs_cur;
dbms_output.put_line('Number'||' '||'Tablespace Name');
loop
exit when tbs_cur%notfound;
fetch tbs_cur into v_tbs;
v_counter := v_counter+1;
exit when tbs_cur%notfound ;
dbms_output.put_line(v_counter||' '||v_tbs);
end loop;
close tbs_cur;
end;
/


SQL> /
Number Tablespace Name
1 SYSTEM
2 UNDOTBS1
3 SYSAUX
4 USERS
5 MAINT
6 MAINT_IDX
7 OFP
8 OFP_IDX
9 TMX
10 TMX_IDX

PL/SQL procedure successfully completed.
# 2  
Old 05-06-2009
one way:
Code:
#!/bin/ksh

sqlplus -s user/pswd@somedb <<EOF > outputfile.lis
set serveroutput on

declare
v_counter number(2):=0;
v_tbs dba_tablespaces.tablespace_name%type;

cursor tbs_cur is
select tablespace_name
from dba_tablespaces
where tablespace_name not in ('TEMP');

begin
open tbs_cur;
dbms_output.put_line('Number'||' '||'Tablespace Name');
loop
exit when tbs_cur%notfound;
fetch tbs_cur into v_tbs;
v_counter := v_counter+1;
exit when tbs_cur%notfound ;
dbms_output.put_line(v_counter||' '||v_tbs);
end loop;
close tbs_cur;
end;
/
exit
EOF

while read num val
do
   arr[$num]="$val"
done < outputfile.lis

You now have an array: arr
# 3  
Old 05-07-2009
Quote:
How do I dynamically assign the below output to unix shell variables
so I can build a menu in a shell script?
When I want to dynamically create menus in shell scripts from data in an Oracle db I do something like the following:

Code:
#!/bin/ksh

function getTabsp {
  sqlplus -S <logon/pass> <<_EOD
  set echo off
  column tablespace_name format a30
  set feedback off
  set heading off
  set pagesize 0
  select tablespace_name 
  from dba_tablespaces
  where tablespace_name not in ('TEMP');
  exit
_EOD
}

PS3="Which Tablespace? "
select tabsp in $(getTabsp)
do
  if [[ -z $tabsp ]]; then
    <error processing here>>
  else
     <$tabsp equal the user's choice of tablespace names>
     <$REPLY equals the number of that choice>
     <do whatever you need with those values>
  fi
done

If you need to know ahead of time what the number of the choice will be (it's assigned automatically by the 'select' statement, you will have to make sure you can use the sql statement to properly order your results with 'order by'. You can use a different column than "tablepsace_name" to 'order by' if you select it also, but use a
Code:
column <name> noprint

before the select statement, this way the function only returns the tablespace_name column.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Storing multiple sql queries output into variable by running sql command only once

Hi All, I want to run multiple sql queries and store the data in variable but i want to use sql command only once. Is there a way without running sql command twice and storing.Please advise. Eg : Select 'Query 1 output' from dual; Select 'Query 2 output' from dual; I want to... (3 Replies)
Discussion started by: Rokkesh
3 Replies

2. UNIX for Dummies Questions & Answers

Getting values of 2 columns from sql query in UNIX variables

Hi, I have connected to oracle database with sqlplus -s / <<EOF select ename, age from emp where empid=1234; EOF I want to save the values of ename and age in unix shell variables. Any pointers would be welcome.. Thanks in advance!!1 Cheers :):):):) (1 Reply)
Discussion started by: gonchusirsa
1 Replies

3. Shell Programming and Scripting

How to split the sql output into two different variables

Hi, How to set as variable from sql output. Query: select aa.serial, ao.name from ann_amit aa JOIN ann_object ao on (aa.classid=ao.classid); I got two values from aa.serial and ao.name, I wanna make two different variable for aa.serial and ao.name. The value of aa.serial should be in... (2 Replies)
Discussion started by: KarthikPS
2 Replies

4. UNIX for Advanced & Expert Users

Call parallel sql scripts from shell and return status when both sql are done

Hi Experts: I have a shell script that's kicked off by cron. Inside this shell script, I need to kick off two or more oracle sql scripts to process different groups of tables. And when both sql scripts are done, I will continue in the shell script to do other things like checking processing... (3 Replies)
Discussion started by: huasheng8
3 Replies

5. Shell Programming and Scripting

Execute multiple SQL scripts from single SQL Plus connection

Hi! I would like to do a single connection to sqlplus and execute some querys. Actually I do for every query one connection to database i.e echo 'select STATUS from v$instance; exit' > $SQL_FILE sqlplus user/pass@sid @$SQL_FILE > $SELECT_RESULT echo 'select VERSION from v$instance;... (6 Replies)
Discussion started by: guif
6 Replies

6. Shell Programming and Scripting

How to use sql data file in unix csv file as input to an sql query from shell

Hi , I used the below script to get the sql data into csv file using unix scripting. I m getting the output into an output file but the output file is not displayed in a separe columns . #!/bin/ksh export FILE_PATH=/maav/home/xyz/abc/ rm $FILE_PATH/sample.csv sqlplus -s... (2 Replies)
Discussion started by: Nareshp
2 Replies

7. UNIX for Dummies Questions & Answers

Execute PL/SQL function from Unix script (.sql file)

Hi guys, I am new on here, I have a function in oracle that returns a specific value: create or replace PACKAGE BODY "CTC_ASDGET_SCHED" AS FUNCTION FN_ASDSCHEDULE_GET RETURN VARCHAR2 AS BEGIN DECLARE ASDSchedule varchar2(6); ASDComplete... (1 Reply)
Discussion started by: reptile
1 Replies

8. Shell Programming and Scripting

put value of multiple sql statements into unix variables

i want to use multple sql count statements and store these count values in unix variable but in one connection only i.e. in only 1 time database should be hit ,which is the main requirement. (1 Reply)
Discussion started by: sw@pnil
1 Replies

9. Shell Programming and Scripting

Calling SQL LDR and SQL plus scripts in a shell script

Hi- I am trying to achieve the following in a script so I can schedule it on a cron job. I am fairly new to the unix environment... I have written a shell script that reads a flat file and loads the data into an Oracle table (Table1) via SQLLDR. This Works fine. Then, I run a nested insert... (5 Replies)
Discussion started by: rajagavini
5 Replies

10. Shell Programming and Scripting

Pass multiple variables to SQL script

I am trying to close of multiple users in an Oracle database. Each users has records in multiple tables what I need to do is use a script that call each SQL seperately passing either CLI arguments or gathered arguments from the users during run time. ## Accept variable(s) from the command line... (1 Reply)
Discussion started by: jagannatha
1 Replies
Login or Register to Ask a Question