tags:

views:

59

answers:

2

How to execute one Stored Procedure inside other Stored Procedure?

NOTE: Both Stored Procedure's reside in different servers

+1  A: 

The easiest, if you can on your system is (they must be linked - click the link for information on linking Servers):

EXEC [RemoteServer].DatabaseName.ObjectOwnerName.StoredProcedureName

Have a look here for more information .

Also, for getting away from hard-coding server names, look at Paul's answer here involving Synonyms.

Kyle Rozendo
The brackets around the remote server name are only necessary if the name contains invalid identifier characters, just like with any other name in SQL. I see this a lot where I work, because we have a lot of servers with hyphens in the name, which require the brackets, so people end up using them for all server names, which is unnecessary ugliness, IMO.
P Daddy
@Kyle: See my answer for a way to abstract the server name out of the stored procedure. That way you can move the code between environments and not have to modify the reference itself.
Paul Hadfield
Also, the second to last part is only the database owner (dbo) if the stored procedure in question belongs to the database owner. In most designs, it does, but a more correct specification would be ObjectOwnerName.
P Daddy
@Paul - Quite right, I should have mentioned Synonyms as we've been bitten there before. @P Daddy - Altered in accordance of your second comment. This is just a simple named structure to follow.
Kyle Rozendo
+1  A: 

Further to @Kyles answer, you can add a synonym to remove the need for the longer / linked structure. This abstracts the server being referenced. So you could move the SP from UAT to LIVE environment and the code wouldn't have to change. The synonym with in the two environments would be set up on each server to correctly reference the other server in that environment.

Paul Hadfield
Synonyms should be a best practice, hard coding a 4 part name is just asking for trouble. +1
@Daytona250: True, but it's knowing that functionality is available that's always the problem. It was only a new DBA coming into my previous role that highlighted it to the team. Prior to that we thought hard coded linking was the only option.
Paul Hadfield