Solved

Need a SQL stored procedure to use tables from multiple databases

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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
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.

726 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