【问题标题】:Is it possible to pass a variable to a SQL/DDL script?是否可以将变量传递给 SQL/DDL 脚本?
【发布时间】:2012-12-02 21:00:44
【问题描述】:

我们有一堆脚本来重新定义我们的数据库。我们运行的脚本调用了一堆其他的脚本:

@@set_target_schema
@@drop_all

-- do some other stuff - create tables, insert data, etc

commit;

set_target_schema.sql 看起来像:

define TARGET_SCHEMA="OUR_APP_NAME"

有没有办法将可选参数传递给我们的顶级脚本,然后将该参数传递给 set_target_schema.sql 并使用该值作为模式的名称(如果提供),否则使用默认值?

【问题讨论】:

  • 虽然,老实说,通过脚本语言和使用动态 SQL 连接可能是值得的。

标签: sql oracle schema ddl


【解决方案1】:

为了能够使用默认值,您可以执行以下操作:

在你的主文件中:

SET VERIFY OFF

-- specify as many substitution variable as you need to. 
COLUMN 1 NEW_VALUE 1 noprint
COLUMN 2 NEW_VALUE 2 noprint
REM COLUMN 3 NEW_VALUE 3 noprint
REM ..........
REM COLUMN <N> NEW_VALUE <N> noprint    

SELECT '' "1"
     , '' "2"
  FROM dual
 WHERE 0 = 1;

-- Default values.
select decode('&1', null, 'Default1', '&1') "1" 
     , decode('&2', null, 'Default1', '&2') "2"
 from dual;

-- prints substitution variables' values
@@set_target_schema.sql '&1' '&2'

undefine 1
undefine 2

结果:

-- without parameters 
SQL> @c:\main.sql


'DEFAULT 'DEFAULT                                                               
-------- --------                                                               
Default1 Default1    


-- with parameters                                                         
SQL> @c:\main.sql parameter1 parameter2


'PARAMETER 'PARAMETER                                                           
---------- ----------                                                           
parameter1 parameter2                                                           

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-07
    • 2019-04-19
    • 1970-01-01
    • 2022-11-20
    • 1970-01-01
    相关资源
    最近更新 更多