views:

217

answers:

2

I have a script that gets the current time, and must pass it to another script.

variable v_s_time varchar2(30);
exec :v_s_time := to_char(sysdate,'YYYY-MM-DD HH:MI:SS AM');
--Lots of unrelated code here
variable v_e_time varchar2(30);
exec :v_e_time  := to_char(sysdate,'YYYY-MM-DD HH:MI:SS AM');
@"test report script.sql" :v_s_time :v_e_time; --yes, I also tried ":v_s_time", didn't seem to do anything.

This does not work, it seems that the literal :v_s_time is passed to the script, instead of what I want: "2010-04-14 05:50:01 PM".

To execute this manually, I could enter:

@"test report script.sql" "2010-04-14 05:50:01 PM" "2010-04-14 05:57:34 PM"

I found that what does work is this:

define v_s_time = "2010-04-14 05:50:01 PM"
--Lots of unrelated code here
define v_e_time = "2010-04-14 05:57:34 PM"
@"test report script.sql" "&&v_s_time" "&&v_e_time";

But it is unrealistic to hardcode the datetimes. Anyone know how to handle this?

(Oracle 10g)

+1  A: 

Can you just reference the bind variables in the "test report script.sql" script? In other words, in "test report script.sql", you have a direct reference to v_s_time and v_e_time, skipping the SQL*Plus define variable. There doesn't appear to be any really elegant way to convert from bind variables to SQL*Plus DEFINE'd variables.

DCookie
Does that work? Hmm I'll have to try it tomorrow. I don't like the idea of `scriptB` depending on variables that may or may not have been created in `scriptA`, and it makes it harder to run `scriptB` for testing purposes, but if that's the only way...
FrustratedWithFormsDesigner
+3  A: 

You can use the NEW_VALUE clause of the COL command to dynamically retrieve a value into a substitute variable:

SQL> /*Declare that the "dat" column will be stored in the v_date variable*/
SQL> COL dat NEW_VALUE v_date

SQL> SELECT to_char(sysdate,'YYYY-MM-DD HH:MI:SS AM') dat FROM dual;

DAT
----------------------
2010-04-15 09:54:29 AM

SQL> select '&&v_date' from dual;

'2010-04-1509:54:29AM'
----------------------
2010-04-15 09:54:29 AM

You can then call your script with this substitute variable:

@"test report script.sql" &&v_date
Vincent Malgrat
Yeah, this looks like it's working! :)
FrustratedWithFormsDesigner