Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

1 SP on a different DB to used for multiple databases

Status
Not open for further replies.

davism

MIS
Nov 9, 2002
140
0
0
US
We have some database that like span different states.

We have code for a stored procedure and that stored procedure resides in a Stage database on the same server so a linked server is not necessary.

Code:
CREATE PROCEDURE dbo.TestA
(@StateID CHAR(2))
AS
BEGIN 

   SET NOCOUNT ON


  UPDATE dbo.TableA

       SET MemTypeInd='Y'

  WHERE State=@State


END

As you see in this store procedure updates dbo.TableA. Yet, there could be let's say 14 states that have this. So basically there is 1 aspect of code which is in a stage database that needs to run for each. So I basically could not even use a dbo.TableA being that the SP is on a stage database.

What I would need is something like:

Code:
CREATE PROCEDURE dbo.TestA
(@StateID CHAR(2))
AS
BEGIN 

   SET NOCOUNT ON

  UPDATE StateFL.dbo.TableA

       SET MemTypeInd='Y'

  WHERE State=@State

END

Code:
CREATE PROCEDURE dbo.TestA
(@StateID CHAR(2))
AS
BEGIN 

   SET NOCOUNT ON

  UPDATE StateCO.dbo.TableA

       SET MemTypeInd='Y'

  WHERE State=@State

END

Or basically leave it as dbo.TableA but yet still reside on the stage DB. Or even like a @DBName.dbo.TableA where the @DBName would equate to like the StateFL or StateCO.

I know there is the sp_MSforeachdb but that would basically be like a dynamic SQL as well.

What other possible ways are there to achieve this without a dynamic sql as a dynamic sql does not take the best query path with the sql optimizer unless that has changed which I do not think it did.
 
You could try using partitioned views. I recommend you google it to get a good understanding. Basically, in a "Combined" database, you create a view like this...

Code:
Create View vwTableA
AS
Select * From StateFL.dbo.TableA where State = 'FL'

Union All

Select * From StateCO.dbo.TableA Where State = 'CO'

This should allow you to select the data from all your databases a combine in to 1 result set. You may also be able to update the data. It's been many years since I've tried doing something like this, but I do encourage you to spend some time looking in to it.

You should also google "SQL Server Federated database", which is a way of putting data on separate servers with the purpose of improving performance. These same techniques also work for combining data from multiple databases and/or sql instances.

-George
Microsoft SQL Server MVP
My Blogs
SQLCop
twitter
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Would not a SYNONYM be even better? CREATE SYNONYM and then DROP it?
 
It depends on how you end up using this. With the partitioned view method, all you need to do when you add a new state is to change the view. With other methods, you may find yourself updating more code.

-George
Microsoft SQL Server MVP
My Blogs
SQLCop
twitter
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top