mysql 批处理文件传参,如何从Windows批处理文件到SQL传递变量* Plus的

这篇博客介绍了如何在Windows批处理文件中将变量传递给SQLPLUS,并在执行SQL命令时显示这些变量的值。同时,它展示了如何将查询结果存储到CSV文件中。通过定义SQL*Plus的绑定变量,赋值并打印变量,然后使用SPOOL命令来保存查询结果到CSV格式的文件。
摘要由CSDN通过智能技术生成

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

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

Result should be stored in csv file.

How can I do that.

This is possible in Unix(shell script) how can I do that in Windows(batch script).

解决方案

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

Just pass it as an argument to the SQL script. And use substitution variables in the same order that of the arguments list &1 &2...

For example,

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.

To display the variables which you pass as arguments to the SQL script, first you need to define the bind variables, then assign the argument value and then print the value.

For example,

I have test.sql:

For NUMBER type

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

Now, let's test the script:

SQL> @D:\test.sql 1000 7369

PL/SQL procedure successfully completed.

SAL

----------

1000

PL/SQL procedure successfully completed.

EMPNO

----------

7369

SQL>

For STRING type

-- define the bind variable

var ename varchar2(10)

-- assign the argument's value

exec :ename := '&1'

-- display the value

print :ename

Now, let's test the script:

SQL> @D:\test.sql LALIT

PL/SQL procedure successfully completed.

ENAME

--------------------------------

LALIT

SQL>

Result should be stored in csv file.

Handle this in the SQL script. Use proper SQL*Plus formatting for comma separated result. To store the result set, you just need to SPOOL

For example,

set colsep , -- separate columns with a comma

set pagesize 0 -- No headers

set trimspool on -- remove trailing blanks

spool mycsvfile.csv

SELECT ....

spool off

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值