views:

33

answers:

2

how to store a value returned from a sql query in a variable in batch programming ?

i can invoke sqlserver queries from my cmd prompt using sqlcmd server name then the qwery

this is query statement i m going to use

SELECT CASE WHEN DATEDIFF(minute, record_timestamp, GETDATE()) < 10 THEN 1 ELSE 0 END

how to store the value returned

i tried using set variablename but it save the statement rather than the return value ..

and if i save this in a variable what type of variable it will can i compare it with numeric values in if condition

A: 

you use the cmd.exe's for loop. eg

for /F "tokens= delims=" %%a in ('sqlcmd....') do(
 set returned=%%a
)

tokens and delims are up to you to define.

ghostdog74
the value returned by the query is a single value so how to use the for loop can u kindly explain more
Arunachalam
+1  A: 

Found this link which might help.

Ardman
i dont want to use a variable in the sql statement but want to store the value returned bu sql query in command line variable
Arunachalam