user3227783
user3227783

Reputation: 111

getting output of sqlcmd query in to batch variable

Hi i want to get output of query into batch variable. so i did

sqlcmd /U sqladmin /P pwd /d mydb /v nesql= xxx hostsql = yyy -e /I /Q "set nocount on;select count(*) from mytable where host='$(hostsql)' and ( LocaA='$(nesql)' or LocaB='$(nesql)' )" -h -1 >prajyodh.txt

I am getting the output but getting the extra first line with proper query.

set nocount on;select count(*) from mytable where host='yyy' and ( LocaA='xxx' or LocaB='xxx' )
  11105

I want to get the first line ommited so that i can get count to a variable by doing

set /p varname=<prajyodh.txt

and use it in my batch script further.

I have gone trough sqlcmd-? but i couldnot find the appropriate option.

please help.Thanks in advance.

Upvotes: 1

Views: 1930

Answers (1)

npocmaka
npocmaka

Reputation: 57252

1.This approach is similar to yours:

< prajyodh.txt (
  set /p line1=
  set /p line2=

) 
set line

2. FOR /F and without temp file.

for /f "skip=1 tokens=* delims=" %%s in ('sqlcmd /U sqladmin /P pwd /d mydb /v nesql= xxx hostsql = yyy -e /I /Q "set nocount on;select count(*^) from mytable where host='$(hostsql^)' and ( LocaA='$(nesql^)' or LocaB='$(nesql^)' ^)" -h -1^) do (
     set "second_line=%%S"
     goto :break
)
:break

Upvotes: 1

Related Questions