SQLCMD在查询中使用批处理变量 [英] SQLCMD using batch variable in query

查看:169
本文介绍了SQLCMD在查询中使用批处理变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在批处理脚本中使用sqlcmd,是否可以在查询中引用批处理变量?

If I'm using sqlcmd in a batch script, can I reference a batch variable inside the query?

这样,如果我做类似的事情

So that if I do something like

for /f "delims= " %%a in ('sqlcmd -S SERVER -d DATABASE -Q 
    "SELECT Column1 FROM Table1 WHERE Column1=[specific number]"') do set var1=%%a

我该怎么做?

for /f "delims= " %%b in ('sqlcmd -S SERVER -d DATABASE -Q
    "SELECT Column2 FROM Table1 WHERE Column2=[var1]"' do set var2=%%b

以便第二条sqlcmd语句中的WHERE条件正在使用第一条语句中设置的变量吗?

So that the WHERE condition in the second sqlcmd statement is using the variable set in the first statement?

推荐答案

您可以使用-v开关并使用$(variable)语法引用sql语句中的参数,从而将环境中的参数传递给sqlcmd.例如.在第二个语句中:

You can pass arguments from the environment to sqlcmd by using the -v switch and referencing the parameters in your sql statement by the $(variable) syntax. E.g. in your second statement :

for /f "delims= " %%b in ('sqlcmd -S SERVER -d DATABASE -Q
    "SELECT Column2 FROM Table1 WHERE Column2=$(var1)" -v var1=%var1%' do set var2=%%b

这篇关于SQLCMD在查询中使用批处理变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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