Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting return from OSQL

Status
Not open for further replies.

sagn

Programmer
Jun 7, 2001
166
US
I have written a dos batch file to
perform some sql commands using OSQL.

How can I get the return values from the
executed SQL into a batch variable?

For example,

osql -Uusername -Ppasswd -Q "select count(*) from table"

How can I get what the count(*) is.. I know I can output to
a file but that is really only pratical for a simple
statement like this.. If I have several statements
the file would be too complex to attempt extracting this number... wouldn;t it?

thanks

diane
 
hi diane check out




/**************
Using EXIT to Return Results in osql
You can use the result of a SELECT statement as the return value from osql. The first column of the first result row is converted to a 4-byte integer (long). MS-DOS passes the low byte to the parent process or operating system error level. Windows NT passes the entire 4-byte integer. The syntax is:

EXIT(query)

For example:

EXIT(SELECT @@ROWCOUNT)

EXIT(SELECT 5)

You can also include the EXIT parameter as part of a batch file. For example:

osql /Q "EXIT(SELECT COUNT(*) FROM '%1')"

The osql utility passes everything between the parentheses ( ) to the server exactly as entered. If a stored system procedure selects a set and returns a value, only the selection is returned. The EXIT( ) statement with nothing between the parentheses executes everything preceding it in the batch and then exits with no return value.



/**************
 
Thanks for the info
I hate to be obtuse but...

how to I capture the value that is returned?

For example when I use the -b flag and I have
an error of magnitude 10 or more the variable
%errorlevel% receives a value of 1.

How can I capture the return value into a variable?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top