Click here to Skip to main content
15,886,258 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

how to execute stored procedure in multiple databases in sql server

thanks
Posted
Comments
André Kraak 11-Oct-11 9:21am    
Do you have the same stored procedure in multiple database, which you want to execute?

or

Do you have a stored procedure that needs to access multiple databases?
K N R 11-Oct-11 9:58am    
hi andre,

Yes, i am access to multiple databases

Solution 1 will work for multiple stored procedures in multiple databases.

For a stored procedure to access a different database the database should be linked. Then to access the tables in that database use the database in the name.
SQL
SELECT myColumn1 FROM [OtherServer\OtherInstance].[database].[schema].[table]
 
Share this answer
 
You can do the following :
SQL
exec database1.dbo.usp_name1
exec database2.dbo.usp_name2
exec database3.dbo.usp_name3
-- continue like this as many times as you need
 
Share this answer
 
Comments
K N R 14-Oct-11 0:33am    
hey mehdi,

I have only one stored procedure but it run in different databases.
faizan saand 21-Apr-14 4:36am    
if the other database has security login, how to call such sp from the other database?
Mehdi Gholam 21-Apr-14 5:05am    
Then you have to have access first.
faizan saand 21-Apr-14 7:55am    
Then how to access with permission
Mehdi Gholam 21-Apr-14 7:59am    
Contact your DBA and ask for permission.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900