SQL Clone
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-Enthusiastic
SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)

Group: General Forum Members
Points: 186 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
Hall of Fame
Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)

Group: General Forum Members
Points: 3142 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
Old Hand
Old Hand (392 reputation)Old Hand (392 reputation)Old Hand (392 reputation)Old Hand (392 reputation)Old Hand (392 reputation)Old Hand (392 reputation)Old Hand (392 reputation)Old Hand (392 reputation)

Group: General Forum Members
Points: 392 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
Jared Karney
Jared Karney
SSChampion
SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)

Group: General Forum Members
Points: 13754 Visits: 3697
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
PFE - Microsoft
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