Solved

Need a SQL stored procedure to use tables from multiple databases

Posted on 2009-05-14
4
864 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 60

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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

729 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