How to pass variable to a query?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to pass variable to a query?
# 1  
Old 06-18-2015
How to pass variable to a query?

Hi All,

How to pass date variable to a query?

I have tried the below one , but it's not working.

ost.ksh

Code:
#!/bin/ksh

v_date=$1
var=$(sqlplus -s $ORACON <<ENDOFSQL
SELECT TO_DATE('$v_date','DD-MON-YYYY'),-1) FROM DUAL;
exit;
ENDOFSQL
)
#End

I have executed as below.

Code:
ksh ost.ksh 20150612

Please help me.

Thanks.
# 2  
Old 06-18-2015
Quote:
Originally Posted by ROCK_PLSQL
Code:
#!/bin/ksh

v_date=$1
var=$(sqlplus -s $ORACON <<ENDOFSQL
SELECT TO_DATE('$v_date','DD-MON-YYYY'),-1) FROM DUAL;
exit;
ENDOFSQL
)
#End

The single quotes will make that a literal `$v_date'
Change it for double quotes to preserve the special meaning of the `$'
# 3  
Old 06-18-2015
Hi,

I have changed single quote to double quote.


I got the below error.

Code:
var SELECT get_dat_function(TO_DATE("20150612","DD-MON-YYYY"),-1) FROM DUAL;
                                                             *
ERROR at line 4:
ORA-06550: line 4, column 62:
PL/SQL: ORA-00904: "DD-MON-YYYY": invalid identifier
ORA-06550: line 4, column 1:
PL/SQL: SQL Statement ignored

Code:
#!/bin/ksh

v_date=$1
var=$(sqlplus -s $ORACON <<ENDOFSQL
SELECT get_dat_function(TO_DATE("$v_date",'DD-MON-YYYY'),-1)) FROM DUAL;
exit;
ENDOFSQL
)
#End

Thanks.
# 4  
Old 06-18-2015
The single qutotes are not the problem.
You pass the date in format YYYYMMDD but the statement expects it to be in format DD-MON-YYYY. And there is a surplus brace.
Code:
v_date=$1
var=$(sqlplus -s $ORACON <<ENDOFSQL
SELECT get_dat_function(TO_DATE('$v_date','YYYYMMDD'),-1) FROM DUAL;
exit;
ENDOFSQL
)
#End

# 5  
Old 06-18-2015
Hi,

I have tried this still same error.

Thanks
# 6  
Old 06-18-2015
Please post the exact errormessage. When I execute this after setting the ORACON variable it works for me:
Code:
$ sqlplus $ORACON

SQL*Plus: Release 11.2.0.2.0 Production on Thu Jun 18 13:28:27 2015

Copyright (c) 1982, 2011, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production

SQL> create or replace function get_dat_function(v_dat date, v_offset number) return date
  2  is
  3  begin
  4     return v_dat-v_offset;
  5  end get_dat_function;
  6  /

Function created.

SQL> exit
Disconnected from Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production
$ 
$ cat x.sh
v_date=$1
var=$(sqlplus -s $ORACON <<ENDOFSQL
SELECT get_dat_function(TO_DATE('$v_date','YYYYMMDD'),-1) FROM DUAL;
exit;
ENDOFSQL
)
#End
echo "$var"
$ 
$ ./x.sh 20150612

GET_DAT_FUNCTION(T
------------------
13-JUN-15
$

# 7  
Old 06-18-2015
Hi,

The error is

Code:
ORA-00904: "YYYYMMDD": invalid identifier


Thanks.

Moderator's Comments:
Mod Comment Use code tags please.

Last edited by zaxxon; 06-18-2015 at 10:20 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Need to pass variable in a command and assign value to a variable

Hello All, Hope you're doing well ! I am trying below command to be passed in a shell script, header_date_14 is a variable and $1 is the name of a file I intend to pass as a command line argument, however command line argument is not being accepted. header_date_14=$(m_dump... (8 Replies)
Discussion started by: ektubbe
8 Replies

2. Shell Programming and Scripting

Need script to pass all sql file names in a directory to DB query

Hi All, In this path /home/all_files we have follwing files and direcotries proc_edf_sot.sql proc_ssc_sot.sql func_dfg_sot.sql sot unic cmr sdc under sot directory we have other directories sql pas ref under sql directory we have sql_sot sql_mat (6 Replies)
Discussion started by: ROCK_PLSQL
6 Replies

3. Shell Programming and Scripting

How to pass string into sql query?

Hi Gurus, I have a request which needs to pass string into sql. dummy code as below: sqlplus -s user/password@instance << EOF >>output.txt set echo off head off feed off pagesize 0 trimspool on linesize 1000 colsep , select emp_no, emp_name from emp where emp_no in ('a', 'b', 'c'); exit;... (4 Replies)
Discussion started by: ken6503
4 Replies

4. Shell Programming and Scripting

How to pass Variable from shell script to select query for SqlPlus?

echo "set echo off"; echo "set feedback off"; echo "set linesize 4000"; echo " set pagesize 0"; echo " set sqlprompt ''"; echo " set trimspool on"; Select statement is mentioned below echo "select res.ti_book_no from disney_ticket_history res where res.ti_status =${STATUS} and... (7 Replies)
Discussion started by: aroragaurav.84
7 Replies

5. UNIX for Dummies Questions & Answers

To pass multiple arguments from file in to an sql query

Hi all , I want to pass contents from a file say f1 as arguments to a sql query which has In statement using a script example select * from table_1 where login in ( `cat f1`) ; will this work or is there any other way to do it. (1 Reply)
Discussion started by: zozoo
1 Replies

6. Red Hat

How to pass value of pwd as variable in SED to replace variable in a script file

Hi all, Hereby wish to have your advise for below: Main concept is I intend to get current directory of my script file. This script file will be copied to /etc/init.d. A string in this copy will be replaced with current directory value. Below is original script file: ... (6 Replies)
Discussion started by: cielle
6 Replies

7. Shell Programming and Scripting

How to pass a function with a variable parameter into another variable?

Hello again :) Am currently trying to write a function which will delete a record from a file. The code currently looks as such: function deleteRecord() { clear read -p "Please enter the ID of the record you wish to remove: " strID ... (2 Replies)
Discussion started by: U_C_Dispatj
2 Replies

8. Programming

JDBC code to pass the SQL query as parameter and execute?

Below i have the sample code. i need to pass the entire query from file or as parameter and read the results and write into a output file. here the number of columns are unknown. some times it may be 2,3 or entire columns from the table. read all the column results and write into a comma... (0 Replies)
Discussion started by: laknar
0 Replies

9. Shell Programming and Scripting

add the output of a query to a variable to be used in another query

I would like to use the result of a query in another query. How do I redirect/add the output to another variable? $result = odbc_exec($connect, $query); while ($row = odbc_fetch_array($result)) { echo $row,"\n"; } odbc_close($connect); ?> This will output hostnames: host1... (0 Replies)
Discussion started by: hazno
0 Replies

10. Shell Programming and Scripting

pass result from one query to another

Can any one help me how to pass the value of result of one query to another query. I to pass the value of result of 'select max(id) from a' into another query like update table set col =<value from last query> where ....; updatestaging() { xx=`$ORACLE_HOME/bin/sqlplus -s... (1 Reply)
Discussion started by: u263066
1 Replies
Login or Register to Ask a Question