开发者

passing value of sqlplus variable from one script to another

开发者 https://www.devze.com 2022-12-26 17:13 出处:网络
I have a script that gets the current time, and must pass it to another script. variable v_s_time varchar2(30);

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)


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


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 SQLPlus define variable. There doesn't appear to be any really elegant way to convert from bind variables to SQLPlus DEFINE'd variables.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号