Solved

Need a SQL stored procedure to use tables from multiple databases

Posted on 2009-05-14
4
838 Views
Last Modified: 2012-05-07
I have a stored procedure that I would like to pass in a database name as a parm and have that database be used for all the tables. Other then scripting each and every SQL statement does anyone know of a way to do this? The USE statement would be ideal but unfortunately you can't use it in a stored procedure.
0
Comment
Question by:Bob Hoffman
  • 2
4 Comments
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 24385507
You can try with dynamic SQL:

DECLARE @DbName NVARCHAR(100), @Sql NVARCHAR(MAX)
SET @DbName = 'MyDatabase';

SET @Sql = 'SELECT ColumnName FROM ' + @DbName + '.dbo.Your_Table_Name'

EXEC(@Sql)
0
 
LVL 8

Author Comment

by:Bob Hoffman
ID: 24385599
That's what I meant by scripting the SQL statements. Normally I would do it that way but there are several DECLARE statements which would cause me problems. Thanks
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 24385775
I think this will workout for you (I never tried it )
Create the stored procedure with 'sp_' in the master database and make this sp system sp
If you are using sql2005 , use the below

USE MASTER
EXECUTE sp_ms_marksystemobject 'urSpName'  
 
and call it from ur databases
0
 
LVL 8

Accepted Solution

by:
Bob Hoffman earned 0 total points
ID: 24458215
Found my own solution, Here is what I did:

--you can run multiple statements just seperate them with a semi colon

CREATE PROCEDURE [dbo].[run_statement]
            @database varchar(50),
            @exesql varchar(4000)
                       
AS

DECLARE @sql varchar(4000)

--Set database context, execute statement(s)
SET @sql = @database + '..sp_executesql @statement = N''USE ' + @database + '; ' + replace(@exesql,'''','''''') + ';'''
EXEC (@sql
 
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now