Thijs
Thijs

Reputation: 387

Put sql query output in variable powershell

I want to put a counted value from a sql query into a variable of powershell...? If i run the following command:

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"

I get the result:

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

but the program won't stop...?

And i want to create an if statement with the value "number of running measurements"

Anyone?

Best regards, Thijs

Upvotes: 0

Views: 163

Answers (1)

vonPryz
vonPryz

Reputation: 24071

Sqlcmd is different from most Windows applications that it is case sensitive. That is, switches -q and -Q have different purposes.

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

Since lower case q means to query without exit, change it to upper case q for query and exit behavior.

Upvotes: 2

Related Questions