jp2631
jp2631

Reputation: 39

Batch script IF ELSE type command

I'm batch script beginner and here is my code...

SQLCMD -E -S DevSSRS01 -Q "SET NOCOUNT ON;
SELECT COUNT(1) FROM tbl_CustAddress WHERE ZipCode = ''" goto :end

if ZipCode ='' no record return, I would like to goto :end without further task, but if ZipCode ='' return with some records I would like to do some tasks before it exit.

Please help.

Upvotes: 0

Views: 54

Answers (1)

jopa
jopa

Reputation: 26

You can use the console output of a program in batch scripts using the for command. From help for:

FOR /F ["options"] %variable IN ('command') DO command [command-parameters]

In this case, best add -h -1 to the sqlcmd command line, to suppress the column headers. Try the following in your batch script (I only tested a simplified version using SELECT 0 AS foo):

for /f %%a in ('SQLCMD -E -S DevSSRS01 -h -1 -Q "SET NOCOUNT ON; SELECT COUNT(1) FROM tbl_CustAddress WHERE ZipCode = ''"') do if "%%a" == "0" goto :end

Upvotes: 1

Related Questions