I want to pass the variable to an environment variable so I can use it again within the batch file.
Main Topics
Browse All TopicsI have a SQL statement that I am running via OSQL. Here is that statement
OSQL -S -E -Q"SELECT [name] FROM sysusers WHERE [name] = 'public'"
I would like to pass the result set into an environment variable so that I can echo it or use it for some other purpose.
Here is an example of what I am trying to accomplish:
@echo off
OSQL -S -E -Q"SELECT [name] FROM sysusers WHERE [name] = 'public'"
echo %result%
pause
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
If the command returns one line try either using a set/p or for command, you can combine these onto one line or use a temp file, e.g.
@echo off
set tempfile="%Temp%\tempfile.
del %tempfile 2>NUL
OSQL -S -E -Q"SELECT [name] FROM sysusers WHERE [name] = 'public'">%tempfile%
set /p myvar1=<%tempfile%
echo Using set/p for one lines worth: %myvar1%
set lastline=
for /f "tokens=*" %%a in ('type %tempfile%') do set lastline=%%a & echo Line found: %%a
echo The last line in the file was %lastline%
Steve
You might be able to use -o"%tempfile%" on the end of the SQL instead of using redirection too by the looks of it.
The set/p takes input from a file or console and applies the first line to the variable (intended for console input). The for command can read the file / command output line by line and return whatever it is you need. Combined with a FIND command to find specific lines or a counter to find line "N" give us the expected output of the OSQL command and what you want to see if needed.
Steve
You won't be able to create the environment variable per se, but you can capture the output with the /o option. Just make your SQL select (or print) a very recognisable string...
What you can also do is then search for a string and set the variable or results accordingly... which is (just realised it sounded familiar) what dragon-it is suggesting...
Or if possible, from within your script, output a batch/command file from SQL server (if you are permitted to use xp_cmdshell) and then run that...
Business Accounts
Answer for Membership
by: BrandonGalderisiPosted on 2009-09-15 at 17:55:33ID: 25340953
This may not put it into the right output, but you can try:
Select allOpen in new window