如何从Windows批处理文件到SQL传递变量* Plus的 [英] How to pass a variable from a Windows Batch File to SQL*Plus

查看:681
本文介绍了如何从Windows批处理文件到SQL传递变量* Plus的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从一个Windows批处理文件SQLPLUS传递变量,
同时显示SQL结果批变量应与SQL结果一起打印。

结果应存放于csv文件。

我怎样才能做到这一点。

这是可能的UNIX(shell脚本)我怎么能做到这一点在Windows(批处理脚本)。


解决方案

  

我想从一个Windows批处理文件SQLPLUS传递变量


只要将它作为参数传递到SQL脚本。并使用替换变量的相同顺序的参数列表的&功放; 1和2 ...

例如,

mybatchfile.BAT:

  SQLPLUS -S用户名/密码@ SID
@c:\\ SQL \\ mysqlfile.sql 1000 7369

mysqlfile.SQL:

 更新EMP集SAL =放大器; 1其中EMPNO =和2


  

在显示SQL结果批变量应与SQL结果一起打印。


要显示您作为参数传递给SQL脚本,首先需要定义绑定变量的变量,然后分配的参数值,然后打印该值。

例如,

我的 TEST.SQL:

对于号码类型

   - 定义绑定变量
VAR萨尔号 - 将第一个参数的值
EXEC:SAL:=&放大器; 1 - 显示值
打印:SAL - 定义绑定变量
VAR EMPNO号 - 分配第二个参数的值
EXEC:EMPNO:=和2 - 显示值
打印:EMPNO

现在,让我们来测试脚本:

  SQL> @D:\\ TEST.SQL 1000 7369PL / SQL过程已成功完成。
       SAL
----------
      1000
PL / SQL过程已成功完成。
     EMPNO
----------
      7369SQL>

对于字符型

   - 定义绑定变量
VAR的ename VARCHAR2(10) - 分配参数的值
高管:为ename:='和1' - 显示值
打印:为ename

现在,让我们来测试脚本:

  SQL> @D:\\ TEST.SQL LALITPL / SQL过程已成功完成。
ENAME
--------------------------------
LALITSQL>


  

结果应存放于csv文件。


在SQL脚本处理这个问题。使用正确的SQL * Plus格式为逗号分隔的结果。要存储的结果集,你只需要 SPOOL

例如,

 集COLSEP, - 用逗号分隔的列
集PAGESIZE 0 - 无头
设置trimspool上 - 删除尾随空白阀芯mycsvfile.csv选择 ....线轴断

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

这篇关于如何从Windows批处理文件到SQL传递变量* Plus的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆