Solved

SQL stored procedure

Posted on 2011-09-28
5
215 Views
Last Modified: 2012-08-13
Is there a way to make the name of the database used in a stored procedure a variable.  Can I have 1 stored procedure that runs in different databases based upon the name of the  database name I pass in as a parameter.  

"a USE database statement is not allowed in a procedure, function or trigger."  = error from SQL
0
Comment
Question by:dastaub
[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
5 Comments
 
LVL 17

Expert Comment

by:sonawanekiran
ID: 36716806
select * from your_database_name..your_table_name
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 36716807
If you pass in the db name as a parameter, then you could try something like this in your code:

IF @db_name = 'db1' SELECT * FROM db1.dbo.MyTable
IF @db_name = 'db2' SELECT * FROM db2.dbo.MyTable
IF @db_name = 'db3' SELECT * FROM db3.dbo.MyTable
-- et cetera

Open in new window


If you do not want to specify the databases up front, you will have to use dynamic SQL.
0
 
LVL 25

Accepted Solution

by:
Lee Savidge earned 500 total points
ID: 36716836
Yes, but the simplest thing to use is dynamic SQL.
create procedure myProc (@nvchDB nvarchar(100))
as
begin
    exec ('select * from ' + @nvchDB + '.dbo.myTable')
end
go

Open in new window

0
 

Author Comment

by:dastaub
ID: 36716988
exec ('use ' + @nvchDB )

i tried this, but it appears to be ignored by the stored procedure.  if this worked, I would only have to change 1 line of code
0
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 36717457
You can't do that.
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Display SQL maintenance plan SQL Code 3 47
SQL Job Failed 6 31
SQL Simple Query Taking a Very Long Time 11 37
SQL Server replace between tags 6 29
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
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…

751 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