如何将变量从 Windows 批处理文件传递到 SQL*Plus

How to pass a variable from a Windows Batch File to SQL*Plus

我想将 Windows 批处理文件中的变量传递给 SQLPLUS, 在显示 sql 结果批处理变量时应与 sql 结果一起打印。

结果应存储在 csv 文件中。

我该怎么做。

这在 Unix 中是可能的(shell 脚本)我如何在 Windows(批处理脚本)中做到这一点。

I want to pass a variable from a Windows Batch File to SQLPLUS

只需将其作为参数传递给 SQL 脚本即可。并按照与参数列表 &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

while displaying sql result batch variable should print along with the sql result.

要显示作为参数传递给 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>

Result should be stored in csv file.

在 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