|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Monday, April 27, 2009 1:14 AM
Points: 47,
Visits: 104
|
|
Hi,
I have four clients, each with their own identical database. They all have the same Stored Procedure named sp_Test.
To simplify things, I want a new database with sp_Test, that is called by the clients.
So,I made a new database called COMMON, and put sp_test in there. Problem is, this Stored Procedure doesn't know on which database to perform its actions. A statement like USE DB_CLIENT_ONE gives an error ("USE database statement is not allowed in a procedure").
It's annoying that such a Stored Procedure can do the trick, for example:
SELECT * FROM DB_CLIENT_ONE.DBO.orders this works
USE DB_CLIENT_ONE SELECT * FROM orders this doesn't work
Is there a way to get it done, other than with the EXEC statement?
Thanks,
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Tuesday, August 10, 2010 5:07 AM
Points: 2,732,
Visits: 23,078
|
|
I don't advocate adding procedures to the Master database, but this will solve your issue. If you add a procedure to the Master database (and the dbo schema) and use a name starting with "sp_", it will be found when executing it from another database but run in the context of the database it is called from.
For example:
USE Master GO CREATE PROCEDURE dbo.sp_TestMe AS SELECT DB_Name() GO USE MyDatabase --Replace with your database name GO EXEC sp_TestMe
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Friday, November 27, 2009 8:11 AM
Points: 5,
Visits: 14
|
|
Thanks Michael, it looks like this will indeed solve the issue.
One question though: you say you "don't advocate adding procedures to the Master database". Can there be serious problems doing so? I never touched the master database before, so I'm not an expert in this area....
Thanks again,. Raymond
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Friday, February 15, 2013 6:29 PM
Points: 1,326,
Visits: 1,265
|
|
Hi, I think you can use dynamic SQL
alter proc Test_SP @dbName Sysname As
Declare @strSQL Nvarchar(200) set @strSQL ='Select * FROM ' + @dbName + '.dbo.orders' exec sp_executesql @strsql
Go
Regards, Ahmed
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Wednesday, May 22, 2013 4:49 AM
Points: 1,075,
Visits: 5,119
|
|
Instead of dynamic SQL, I recommend conditional execution like this:
alter proc Test_SP @dbName Sysname As if @dbName = 'Database1' begin select * from Database1.dbo.TableName end else if @dbName = 'Database2' begin select * from Database2.dbo.TableName end
Go
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Friday, February 15, 2013 6:29 PM
Points: 1,326,
Visits: 1,265
|
|
Sorry Suresh, But if you will have more than 4 clients, will you still using conditional execution. Good luck
Regards, Ahmed
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Wednesday, May 22, 2013 4:49 AM
Points: 1,075,
Visits: 5,119
|
|
Hi Ahmed,
When a new database is created for the new client, this stored procedure can be altered.
As dynamic sql is not precomplied, I think conditional execution is faster. However, I have not compared the performance of these 2 types of stored procedures.
Thanks.
Suresh
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Friday, February 15, 2013 6:29 PM
Points: 1,326,
Visits: 1,265
|
|
Yes, you are rigth on this point 'dynamic sql is not precomplied' Ahmed
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Tuesday, August 10, 2010 5:07 AM
Points: 2,732,
Visits: 23,078
|
|
You are not likely to run into errors from adding procedures to the master database. What you do have to watch out for is making sure you appropriately back up your system databases, be aware that if you transfer a database to another server that you may have broken some logic. You also have to watch out for a service pack or something that could potentially replace or remove your stored procedure.
I don't like adding objects to the system databases mostly because they are system databases and I like to leave them alone. For what you are doing, I probably would just put the procedure into the master database and ignore all of the dynamic SQL or conditionals. However, if you find yourself adding more than 10 or so procedures to the system databases, I would say you may have a design problem.
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Friday, November 27, 2009 8:11 AM
Points: 5,
Visits: 14
|
|
Thanks again Michael (and others)
My preference indeed is to skip the conditionals and the dynamics. On the other hand, the number of stored procedures would run into the 100's. I am building an ASP website, and all calls to the database are stored procedures (to make a conversion to asp.net later more simple).
On the ASP side, all customers have their own website. Every .asp file basically consists of two lines: #include customer settings #include common file So, with any number of customers, there's always just one file for me to update (the common file).
Obviously I would like to have the same type of setup for the database. It would be a drag to have to update the same Stored Procedure 50 times for each of the 50 customers.
Probably the best would be is, after a Stored Procedure has been created or modified in a test enviroment, run a script that creates/modifies all the counterparts in the customer databases.
Thanks, Raymond
|
|
|
|