Solved

Need a SQL stored procedure to use tables from multiple databases

Posted on 2009-05-14
4
849 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

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.
I have a large data set and a SSIS package. How can I load this file in multi threading?
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…

790 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