【问题标题】:need to assign sqlcmd output to a variable需要将 sqlcmd 输出分配给变量
【发布时间】:2018-03-13 13:31:39
【问题描述】:
Dim testvar

testvar = ""sqlcmd.exe -S SERVERNAME\INSTANCENAME -U USERNAME -P PASSWORD -r1 -W -Q "select * from Event.dbo.ALL_EVENTS where (POLICY_TYPE!='LOGFILE' and state='OPEN' and Category like 'Windows-High' and not (POLICY_NAME like 'Level 2 and 3 alerts%') and not (TITLE like '%Level 2 :%') and not (TITLE like :%')₀and not (TITLE like :%')%'严重性 = 'MAJOR' or severity = 'MINOR' or severity = 'CRITICAL'))"" wscript.echo 命令

【问题讨论】:

  • 几个问题.. 1) VBS 不允许您一次声明和分配。您需要将其分为 2 个步骤 2) sqlcmd 不会自行运行 - 您可以查看 .Exec
  • 感谢@pankaj 的回复...按照建议编辑了命令...仍然无法正常工作...“脚本已完成,错误代码为 1”
  • 您将字符串分配给 testvar 而不是命令的输出。正如我在之前的评论中提到的,使用 sheel.exec 来实际执行命令并从控制台窗口读取任何输出(我假设 sqlcmd 将输出到控制台)
  • 哦,我的错...当然我会尝试并告诉你结果

标签: vbscript sqlcmd


【解决方案1】:

Hey Sahil,您是否尝试过使用函数返回结果

参考:How to save the result of a SQL query into a variable in VBA?

【讨论】:

  • 当然,我也会试试的
【解决方案2】:

我就是这样做的。谢谢大家的帮助。

Dim printRes, queryErr Set cmdExec = CreateObject("WScript.Shell")

Set messages = cmdExec.Exec("sqlcmd -S ******** -U **** -P ***** -i C:\file\fullpath\mssql.sql")

Set queryRes = messages.StdOut

'Set queryErr = messages.StdErr 

printRes = queryRes.ReadAll 'printerr = queryErr.ReadAll

WScript.Echo "Result is " & printRes 'WScript.Echo "Error is " & printErr

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-19
    • 2017-09-28
    • 2012-12-12
    • 2018-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多