脚本变量,环境变量,反之亦然 [英] Scripting variable to environment variable and vice versa
问题描述
我们可以通过脚本变量环境变量值,当我们在SQLCMD脚本或批处理脚本和witing codeS反之亦然?
Can we pass values of scripting variables to environment variables when we are witing codes in sqlcmd scripts or batch scripts and vice versa ?
推荐答案
在使用批处理变量SQLCMD
一)直接将它们放置在命令行。在CMD解析器将扩展变量的值,因为它是写在行
a) Directly placing them in the command line. The cmd parser will expand the variables to the value as it is written in the line
set "lastName=Smith"
sqlcmd -Q "SELECT * From myTable Where LastName='%lastName%'"
二)在SQL查询中使用声明的变量,并使用previous方法来声明他们在SQLCMD命令。如果我们有一个包含 query.sql的
文件名
SELECT * From myTable Where LastName='$(LastName)'
然后,它是可以做到以下调用
Then it is possible to do the following call
set "lastName=Smith"
sqlcmd -i query.sql -v LastName="%lastName%"
在批处理文件中使用SQLCMD的输出
A)发送输出到文件( -o
开关SQLCMD),然后处理它。参见为/?
a) Send the output to a file (-o
switch in sqlcmd) and then process it. See for /?
sqlcmd -i query.sql -o data.txt -h -1
for /f "tokens=*" %%a in (data.txt) do ....
二)直接处理该命令的输出。再次与 FOR / F
命令。
for /f "tokens=*" %%a in ('sqlcmd -i query.sql -h -1') do ....
在这两种情况下,如果有什么需要的是值赋给变量批次,您将有类似
In both cases, if what is needed is to assign value to a batch variable, you will have something like
for /f "... options ..." %%a in ('sqlcmd -i query.sql -h -1') do (
....
set "varname=%%a"
....
)
处理输出/文件和数据分配给变量
processing the output/file and assigning the data to the variables.
这篇关于脚本变量,环境变量,反之亦然的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!