[英]How to pass a variable from a Windows Batch File to SQL*Plus
我想将Windows批处理文件中的变量传递给SQLPLUS,同时显示sql结果,批处理变量应与sql结果一起打印。
结果应存储在csv文件中。
我怎样才能做到这一点。
在Unix(shell脚本)中这是可能的,如何在Windows(批处理脚本)中做到这一点。
我想将变量从Windows批处理文件传递到SQLPLUS
只需将其作为参数传递给SQL脚本即可。 并以与参数列表&1 &2...
相同的顺序使用替换变量&1 &2...
例如,
mybatchfile.BAT:
sqlplus -S username/password@sid
@c:\sql\mysqlfile.sql 1000 7369
mysqlfile.SQL:
update emp set sal = &1 where empno = &2
在显示sql结果时,批处理变量应与sql结果一起打印。
要显示作为参数传递给SQL脚本的变量,首先需要定义绑定变量,然后分配参数值,然后打印该值。
例如,
我有test.sql:
对于NUMBER类型
-- define the bind variable
var sal number
-- assign the first argument's value
exec :sal := &1
-- display the value
print :sal
-- define the bind variable
var empno number
-- assign the second argument's value
exec :empno := &2
-- display the value
print :empno
现在,让我们测试一下脚本:
SQL> @D:\test.sql 1000 7369
PL/SQL procedure successfully completed.
SAL
----------
1000
PL/SQL procedure successfully completed.
EMPNO
----------
7369
SQL>
对于STRING类型
-- define the bind variable
var ename varchar2(10)
-- assign the argument's value
exec :ename := '&1'
-- display the value
print :ename
现在,让我们测试一下脚本:
SQL> @D:\test.sql LALIT
PL/SQL procedure successfully completed.
ENAME
--------------------------------
LALIT
SQL>
结果应存储在csv文件中。
在SQL脚本中进行处理。 对逗号分隔的结果使用正确的SQL * Plus格式。 要存储结果集,您只需要SPOOL
例如,
set colsep , -- separate columns with a comma
set pagesize 0 -- No headers
set trimspool on -- remove trailing blanks
spool mycsvfile.csv
SELECT ....
spool off
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.