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!

how to use xp_cmdshell with a variable?

Status
Not open for further replies.

sumgirl

IS-IT--Management
Mar 19, 2003
55
US
Hey all. I am no DB / query guru, so bare with me if this is dumb. I
want to run a query that provides a value and then run xp_cmdshell
using that value. Something like the following two step manual op I do
now:

(1) select packageid from v_package where pkgsourcepath like 'c:
\packages'
-- statement above will always return a single value


(2) EXEC master..xp_cmdshell 'dir /s "[value returned from query above
would go here without brackets of course]'


I just want to find a way to do this in one shot so I can batch it up
in some from that can be scheduled. Please help! This is SQL Server
2000 and I am simply using Query Analyzer. Thanks in advance.


 
You can use dyncamic sql to do this. There are many examples here if you search the posts.
 
I did a search on dynamic SQL and was instantly lost. Any chance you could post an example based on my question so I can try and understand knowing what the results would be?
 
Have a go at implementing some dynamic SQL yourself. If you get stuck, post what you have created so far and people will be happy to show you where you may have gone wrong.


____________________________________________________________
Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]

Need help finding an answer? Try the Search Facility or read FAQ222-2244.
 
I know this is lack of expertise / experience and probably totally doofuslike for this form. Heres what I started with, and I have tweaked a bit here and there and cant make it work:

DECLARE @SQLvar nvarchar(200)
DECLARE @SQLwork nvarchar(200)

SET @SQLvar = 'select pkgsourcepath from v_package where name like ' + '''myinstall%''' + ''

SET @SQLwork = 'EXEC master..xp_cmdshell ' + '''dir /s ''' + EXEC(@SQLvar)

EXEC(@SQLwork)
 
Before running this (and instead of of until you are certain it is correct):
Code:
EXEC(@SQLwork)
run this instead:
Code:
PRINT @SQLwork
You should then be able to see what is actually being executed. If it isn't what you expected, you can go back through your code to see where you went wrong.


____________________________________________________________
Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]

Need help finding an answer? Try the Search Facility or read FAQ222-2244.
 
Try somthing like this
Code:
DCLARE @SQLvar varchar(200)
DECLARE @SQLwork varchar(200)

SET @SQLvar = select pkgsourcepath from v_package where name like 'myinstall%'

SET @SQLwork = 'EXEC master..xp_cmdshell ' + '''dir /s ''' + @SQLvar
 
I tried that jbenson001, but I keep getting "Incorrect syntax near the keyword 'select' on line 3."

I can pull the select out and execute it seperately with no problem, and it returns one value as expected.

I appreciate the help all.
 
Put parens around the select statement:
Code:
SET @SQLvar = [b]([/b]select pkgsourcepath from v_package where name like 'myinstall%'[b])[/b]
 
That got it! Thanks jbenson001, this method has all kinds of obvious uses for me!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top