【问题标题】:Put sql query output in variable powershell将sql查询输出放在变量powershell中
【发布时间】:2019-08-13 21:13:22
【问题描述】:

我想将 sql 查询中的计数值放入 powershell 的变量中...?如果我运行以下命令:

sqlcmd -S "ms-sql-1264" -d "ShellPlus" -q "SET ANSI_WARNINGS OFF; SET NOCOUNT ON; SELECT COUNT(InternalMeasurementID) as number_of_running_measurements FROM [ShellPlus].[dbo].[Measurements] WHERE Archived = 0 AND ClinicalInfo NOT LIKE '%PSG%' AND Classification NOT LIKE '%TEST' AND Classification NOT LIKE '%RESEARCH%' AND Status = 2"

我得到了结果:

PS H:\> C:\install\powershell scripts\mail sturen lopend EEG\test_sql.ps1
number_of_running_measurements
------------------------------
                             0

但程序不会停止...?

我想创建一个 if 语句,其值为“运行测量次数”

有人吗?

最好的问候, 泰斯

【问题讨论】:

    标签: sql powershell


    【解决方案1】:

    Sqlcmd 与大多数 Windows 应用程序不同,它区分大小写。也就是开关-q-Q有不同的用途。

    -q "cmdline query"  
    -Q "cmdline query" (and exit)
    

    由于小写q表示不退出查询,所以改为大写q表示查询退出行为。

    【讨论】:

    • 谢谢,但是如何将我的值放入变量中...?
    • $test = sqlcmd -S "ms-sql-1264" -d "ShellPlus" -Q "SET ANSI_WARNINGS OFF;SET NOCOUNT ON;从 [ShellPlus] 中选择 COUNT(InternalMeasurementID) 作为 number_of_running_measurements。[ dbo].[Measurements] WHERE Archived = 0 AND ClinicalInfo NOT LIKE '%PSG%' AND Classification NOT LIKE '%TEST' AND Classification NOT LIKE '%RESEARCH%' AND St​​atus = 2" $test[2].Trim()这有效!
    猜你喜欢
    • 2021-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多