Oracle Query results to be stored in variables using unix


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Oracle Query results to be stored in variables using unix
# 1  
Old 11-12-2009
Oracle Query results to be stored in variables using unix

I want to store the sql query output into a variable


Code:
#!/bin/ksh
ORACLE_SID=DB01;
export ORACLE_SID;
export FILE_PATH=/home/asg/Tmp

# Order Checking
echo " removing old files "
rm $FILE_PATH/Malformed_Order.txt
echo " Enter the Malformed Order ....!"
read orders
echo "Regrade check : $orders"
echo ""

v_test=$(sqlplus -s abc/xyz <<END 
set feedback off;
set heading off;

select is_Regrade_order from portal.order where number = '"$orders"';

EXIT;
END)

echo "out put is: $v_test"


------------
The output expecting is

Enter the Malformed Order ....!
PO-DBMBDWG
Regrade check : PO-DBMBDWG

out put is : N

But when i tried executing the above scripting its throwing an error

Quote:
Enter the Malformed Order ....!
PO-DBMBDWG
Regrade check : PO-DBMBDWG

out put is ERROR:
ORA-01741: illegal zero-length identifier

The functionallity of the script is

1. value is passed through the command prompt
2. Passing the command line arguement to the sql query input
3. The sql query result has to be stored in a variable

Can some one suggest and modify the script
# 2  
Old 11-12-2009
Try changing this to not have the double quotes:

Code:
select is_Regrade_order from portal.order where number = '"$orders"';

to

Code:
select is_Regrade_order from portal.order where number = '$orders';

# 3  
Old 11-13-2009
Throwing an Invalid Error

Changed to SQL Query to

select A.IS_REGRADE_ORDER from wlportal.orders a where A.TRACKING_NUMBER = '$orders';

Ouput : Throwing an error

Quote:
Enter the Malformed Order ....!
PO-DBMBDWG
Regrade check : PO-DBMBDWG
out put is select A.IS_REGRADE_ORDER from wlportal.orders a where A.TRACKING_NUMBER = "$orders"
*
ERROR at line 1:
ORA-00904: "$orders": invalid identifier
# 4  
Old 11-13-2009
Try this script as a test.
You may need to add
Code:
ORACLE_SID=DB01;
export ORACLE_SID;

to get it to work.

Code:
$ cat ./get_oracle_var.ksh
#!/bin/ksh

MYDATE='20091113'
echo "MYDATE=$MYDATE"

SQL_OUTPUT=$(sqlplus -s abc/xyz <<END
SET SHOW OFF
SET PAGESIZE 0
SET FEEDBACK OFF
SET TERMOUT ON
SET TIME OFF
SET TIMING OFF
SET VERIFY OFF
SET ECHO OFF
SELECT 'True' FROM DUAL WHERE TRUNC(SYSDATE) = TO_DATE('$MYDATE', 'YYYYMMDD');
EXIT;
END)

echo "Output is: $SQL_OUTPUT"
exit 0

$ ./get_oracle_var.ksh
MYDATE=20091113
Output is: True

# 5  
Old 11-14-2009
Invalid Identifier Error

I have executed the below query

Code:
cat pol.sh
RACLE_SID=DB01;
export ORACLE_SID;to get it to work.

MYDATE='20091113'
echo "MYDATE=$MYDATE"
SQL_OUTPUT=$(sqlplus -s wlportal/wlportal <<END
SET SHOW OFF
SET PAGESIZE 0
SET FEEDBACK OFF
SET TERMOUT ON
SET TIME OFF
SET TIMING OFF
SET VERIFY OFF
SET ECHO OFF
SELECT 'True' FROM DUAL WHERE TRUNC(SYSDATE) = TO_DATE('$MYDATE', 'YYYYMMDD');
EXIT;
END)
echo "Output is: $SQL_OUTPUT"
exit 0
 
 
Ouput is 
 
./pol.sh
./pol.sh[2]: to:  not found.
MYDATE=20091113
Output is: SELECT "True" FROM DUAL WHERE TRUNC(SYSDATE) = TO_DATE("$MYDATE", "YYYYMMDD")
                                                                  *
ERROR at line 1:
ORA-00904: "YYYYMMDD": invalid identifier

# 6  
Old 11-16-2009
Three things:
1) Put back
Code:
#!/bin/ksh

2) You misspelled the ORACLE_SID environment var
Code:
RACLE_SID=DB01

should be
Code:
ORACLE_SID=DB01

3) Don't use semi-colons!! Semi-colons are not comment characters.
If you want to comment something use '#'.
The system is complaining about
Code:
;to

Code:
export ORACLE_SID;to get it to work.

should be
Code:
export ORACLE_SID #to get it to work.

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Assigning multiple column's value from Oracle query to multiple variables in UNIX

Hi All, I need to read values of 10 columns from oracle query and assign the same to 10 unix variables. The query will return only one record(row). I tried to append all these columns using a delimiter(;) in the select query and assign the same to a single variable(V) in unix. I thought I... (3 Replies)
Discussion started by: hkrishnan91
3 Replies

2. Shell Programming and Scripting

Need to run Oracle stored procedure from UNIX env

Hi Everyone, I want to create a script where i need to run the oracle stored procedure from unix script and get the output(sequence number ) into a variable which i will pass in my datastage job. Below is my stored procedure:- DECLARE P_TRANSTYPE VARCHAR2(20); ... (4 Replies)
Discussion started by: prasson_ibm
4 Replies

3. Shell Programming and Scripting

How to Assign SQL Query Results to Variables in Linux?

Hi, I am new to linux... How to Assign SQL Query Results to Variables in Linux,i want ti generate it in param files, Can anyone please explain me. Ex: SQL> Select * from EMP; O/P: Emp_No Emp_Name 1 AAA 2 BBB 3 CCC and I want expected... (5 Replies)
Discussion started by: Sravana Kumar
5 Replies

4. Shell Programming and Scripting

Multiple Query Results to Variables

Hello, I am very new to shell scripting and I am not sure of how best to handle the following scenario. I need to query a list of values from a table. I need to store those results and use them to selectively delete values in yet another table in a separate database. I do know how to store the... (3 Replies)
Discussion started by: flowervz
3 Replies

5. Shell Programming and Scripting

SQL/Plus in a coprocess example. Also saves query results into shell variables

While assisting a forum member, I recommended running SQL/Plus in a coprocess (to make database connections and run a test script) for the duration of his script rather than starting/stopping it once for every row in a file he was processing. I recalled I made a coprocess example for folks at... (2 Replies)
Discussion started by: gary_w
2 Replies

6. Shell Programming and Scripting

how to call oracle stored procedure from unix shell

Hi i want to call a oracle stored procedure from unix (using bash shell). consider this is my oracle stored procedure with parameter create procedure testproc(name IN varchar, age IN Number, id OUT Number ) AS begin id=1; dbms_output.put.line('successfull validation') end;... (6 Replies)
Discussion started by: barani75
6 Replies

7. Shell Programming and Scripting

how to pass the values to unix shell from the oracle stored procedure.

Hi i am calling a stored procedure from unix shell like this call test_proc('0002','20100218'); the stored procedure was giving output like this dbms_output.put_line(' processed earlier'); i want to see the output in the unix shell where i called. Thanks barani (6 Replies)
Discussion started by: barani75
6 Replies

8. Shell Programming and Scripting

Oracle Query results to be stored in variables

Hi I would like to know if there is a way to just have one ORACLE connection established, using which we can execute different queries and store the results under different variables. For e.g the following uses to two silent ORACLE connection to store the result under two different... (4 Replies)
Discussion started by: ashokjaysiv
4 Replies

9. UNIX for Advanced & Expert Users

Set shell variables from SQLPLUS query results

Hi All, I needed to get the result of two sqlplus queris into shell variables. After days of looking for the ultimate solution to this problem.. i found this... sqlplus -s USER/PASS@DB <<EOF | awk '{if(NR==1) printf("%s ", $1); if(NR==2) printf("%s ", $1);}' | read VAR1 VAR2 set head off... (2 Replies)
Discussion started by: pranavagarwal
2 Replies

10. Solaris

Calling Oracle Stored Procedures in UNIx(sun solaris)

I have created 3 Procedures all similar to this one: I then created 3 shell sripts which will call the sql? finally created a calling script to call the procedure. I am a bit unsure how to this all works, can someone check my code and I am doing this right? Also could I add my procedure (first... (0 Replies)
Discussion started by: etravels
0 Replies
Login or Register to Ask a Question