Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Using MASTER..XP_CMDSHELL to run store proc


Using MASTER..XP_CMDSHELL to run store proc

Author
Message
rhd110
rhd110
SSC Rookie
SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)

Group: General Forum Members
Points: 42 Visits: 271
Hi,

Using the command prompt we use
1. Open Run window
2. Type SQLCMD enter
3. Type USE TestDB enter
4. Type GO enter
5. EXEC dbo.sp_InsertTempValue 101
6. Type GO enter

The following steps insert values 101 in a certain table

How can we do using MASTER..XP_CMDSHELL
Following are the steps I have tried

EXEC MASTER..XP_CMDSHELL 'SQLCMD'
EXEC MASTER..XP_CMDSHELL 'USE TOOLDB'
EXEC MASTER..XP_CMDSHELL 'GO'
EXEC MASTER..XP_CMDSHELL 'EXEC dbo.InsertTempValue 10'
EXEC MASTER..XP_CMDSHELL 'GO'

But this doesn't execute
Any other method to achieve this
winash
winash
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1424 Visits: 1883
The way to do this using xp_cmdhsell is as follows:

EXEC master.dbo.xp_cmdshell 'sqlcmd -S ServerNameComesHere -E -d ToolDB -Q "EXEC dbo.InsertTempValue 101" ';



Could you let us know why you are using xp_cmdshell to execute a stored procedure - depending on what you want to achieve there might be other options available.



mark_copley
mark_copley
SSC Veteran
SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)

Group: General Forum Members
Points: 264 Visits: 240
I think I would try to do it something like this:

DECLARE @myCmd VARCHAR(4000)
SET @myCmd = 'sqlcmd -d AdventureWorks2008R2 -Q "SELECT FirstName, LastName FROM Person.Person WHERE LastName LIKE 'Whi%';" '

EXEC master..XP_CMDSHELL @myCmd
SQLKnowItAll
SQLKnowItAll
SSCrazy
SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)

Group: General Forum Members
Points: 2798 Visits: 3682
rhd110 (3/1/2012)
Hi,

Using the command prompt we use
1. Open Run window
2. Type SQLCMD enter
3. Type USE TestDB enter
4. Type GO enter
5. EXEC dbo.sp_InsertTempValue 101
6. Type GO enter

The following steps insert values 101 in a certain table

How can we do using MASTER..XP_CMDSHELL
Following are the steps I have tried

EXEC MASTER..XP_CMDSHELL 'SQLCMD'
EXEC MASTER..XP_CMDSHELL 'USE TOOLDB'
EXEC MASTER..XP_CMDSHELL 'GO'
EXEC MASTER..XP_CMDSHELL 'EXEC dbo.InsertTempValue 10'
EXEC MASTER..XP_CMDSHELL 'GO'

But this doesn't execute
Any other method to achieve this


I wouldn't. Just open a session and typw the SQL. xp_cmdshell is used in SQL to run command line statements that cannot be run in SQL. Why would you use SQL to send SQL to command line?

Thanks,

Jared
SQL Know-It-All

How to post data/code on a forum to get the best help - Jeff Moden
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search