Tuesday, August 18, 2009

How to store the result of a SQL query run by SQLCMD and store that in a command variable

For achieving this we can direct the output of the SQLCMD run to a text file and then store the output of this text file in a command variable

sqlcmd -d database -E -Q"set nocount on; select field from table where field = 'value'" -h -1 > tmp.txt

Now store the result in a text file

set /P mySessionID= < tmp.txt

echo mySessionID

No comments:

Post a Comment