如何将sqlcmd输出设置为批处理变量? [英] How to set a sqlcmd output to a batch variable?

查看:89
本文介绍了如何将sqlcmd输出设置为批处理变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将sqlcmd查询的输出设置为批处理文件中的变量.

I'm trying to set the output of a sqlcmd query to a variable in a batch file.

这是我的查询:

sqlcmd -S <SERVER> -d <DATABASE> -Q "select max(Column1)+1 from Table1"

这给了我确切的期望和期望:

This gives me exactly what I would expect and what I want:

-----------
         10
<1 rows affected>

但是,当我尝试将其设置为变量时,我使用了以下脚本:

However, when I try to set it to a variable, I used this script:

for /f %%a in ('sqlcmd -S <SERVER> -d <DATABASE> -Q "select max(Column1)+1 from Table1"') 
    do set ColumnVar=%%a
echo %ColumnVar%
pause

这给了我这个结果:<1 rows affected>我猜这是因为循环将变量设置为最后一行.那么有没有办法我可以使用令牌和delims解析出10个呢?

This gives me this result instead: <1 rows affected> I'm guessing this is because the loop is setting the variable to the last line. So is there a way I could use tokens and delims to parse out the 10 instead?

推荐答案

尝试打开NOCOUNT:

Try turning on NOCOUNT:

for /f %%a in ('sqlcmd -S <SERVER> -d <DATABASE> -Q "SET NOCOUNT ON; select max(Column1)+1 from Table1"') do set ColumnVar=%%a
echo %ColumnVar%
pause

这篇关于如何将sqlcmd输出设置为批处理变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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