【问题标题】:passing value of sqlplus variable from one script to another将 sqlplus 变量的值从一个脚本传递到另一个脚本
【发布时间】:2011-02-08 03:18:19
【问题描述】:

我有一个获取当前时间的脚本,必须将它传递给另一个脚本。

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.

这不起作用,似乎将文字 :v_s_time 传递给脚本,而不是我想要的:"2010-04-14 05:50:01 PM"

要手动执行,我可以输入:

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

我发现起作用的是:

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";

但是硬编码日期时间是不现实的。有人知道如何处理吗?

(Oracle 10g)

【问题讨论】:

    标签: oracle sqlplus bind-variables


    【解决方案1】:

    您可以使用NEW_VALUE clause of the COL command 将值动态检索到替代变量中:

    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
    

    然后您可以使用此替代变量调用您的脚本:

    @"test report script.sql" &&v_date
    

    【讨论】:

      【解决方案2】:

      您可以只引用“测试报告脚本.sql”脚本中的绑定变量吗?换句话说,在“测试报告 script.sql”中,您可以直接引用 v_s_time 和 v_e_time,跳过 SQLPlus 定义变量。似乎没有任何真正优雅的方式可以将绑定变量转换为 SQLPlus DEFINE 变量。

      【讨论】:

      • 这行得通吗?嗯,我明天得试试。我不喜欢scriptB 的想法,这取决于可能在scriptA 中创建或未创建的变量,并且为了测试目的而运行scriptB 变得更加困难,但如果这是唯一的方法......
      最近更新 更多