August 11, 2005 at 6:06 am
In DOS, is it possible to return the result of an OSQL call to a variable which would then be used in another call?
eg osql /q "Select process_num from processess"
Within Query Analyser this would return a value of say 200.
I know you can pipe the result to an output file but I would much rather trap the output here and then use it in my next query, which may be
osql /q "Select process_info from process_table where process_num = variable"
thanks in advance
August 11, 2005 at 7:03 am
I know a way. It ain't pretty. ("Don't look, Ethel!!!!")
SELECT @sql = 'SET NOCOUNT ON SELECT COUNT (1) FROM foo'
SELECT @cmd = 'osql -E -Sfoo_server -dfoo_db -h-1 -Q" + @sql + '"'
INSERT INTO #temp1 EXEC master..xp_cmdshell @cmd
August 11, 2005 at 8:41 am
Ugh
August 11, 2005 at 8:46 am
I forgot to mention I am making the OSQL call from a .bat or .cmd file. How do I capture the output and feed it in to another OSQL call as a variable?
August 11, 2005 at 8:47 am
Yer right, Steve, she ain't much to look at.
Right now, though, I'm concerned that I just misread the whole requirement. I assumed he wanted to know how to do that from T-SQL, and right there in big bold letter, he said, "In DOS".
I'll wait for Hammer to clarify before I suggest anything even uglier.
If it's not too late, I could also suggest that maybe a linked server might save the need for an osql call.
August 11, 2005 at 9:04 am
Sorry, Hammer, my fault. I saw "Query Analyzer" in your first post, and that's all I saw.
Maybe you can try using the NT-shell "FOR" command with the "text parser" option. I think the syntax would look something like:
FOR /F %i IN ('isql -h-1 -E -d{dbname} -S{servername} -Q"SET
NOCOUNT ON SELECT {etc.}"') DO @echo %i
I include the SET NOCOUNT to remove the feedback about the number of rows affected.
Instead of @echo, substitute the command of your choice. The result from the first isql call is trapped in the @i variable.
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply