2012-12-14 122 views
4

我们有一堆脚本来重新为我们的数据库建立基线。我们运行脚本调用一堆其他的脚本:是否可以将变量传递给SQL/DDL脚本?

@@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,并使用该值作为模式的名称,否则使用默认值?

+0

虽然,说实话,这可能是值得通过脚本语言连接并使​​用动态SQL。 – 2012-12-14 20:34:02

回答

3

为了能够使用默认值,你可以做这样的事情:

在你的主文件:

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               
相关问题