Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Stored Procedure & 'other' database Expand / Collapse
Author
Message
Posted Tuesday, November 20, 2007 8:52 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC 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,



Post #424172
Posted Tuesday, November 20, 2007 1:32 PM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

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
Post #424313
Posted Wednesday, November 21, 2007 2:06 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum 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
Post #424461
Posted Wednesday, November 21, 2007 2:35 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Tuesday, August 12, 2014 9:52 AM
Points: 1,326, Visits: 1,288
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
Post #424474
Posted Wednesday, November 21, 2007 2:51 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Tuesday, August 5, 2014 2:55 AM
Points: 1,101, Visits: 5,280
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

Post #424486
Posted Wednesday, November 21, 2007 2:55 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Tuesday, August 12, 2014 9:52 AM
Points: 1,326, Visits: 1,288
Sorry Suresh,
But if you will have more than 4 clients, will you still using conditional execution.
Good luck

Regards,
Ahmed

Post #424489
Posted Wednesday, November 21, 2007 3:11 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Tuesday, August 5, 2014 2:55 AM
Points: 1,101, Visits: 5,280
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
Post #424496
Posted Wednesday, November 21, 2007 3:20 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Tuesday, August 12, 2014 9:52 AM
Points: 1,326, Visits: 1,288
Yes, you are rigth on this point 'dynamic sql is not precomplied'
Ahmed
Post #424497
Posted Wednesday, November 21, 2007 5:04 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

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.
Post #424535
Posted Wednesday, November 21, 2007 5:59 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum 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

Post #424551
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse