1

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.

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

thanks the first one worked. the second one gave me some error..but i will debug it somehow.. thanks once again..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.