×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

1 SP on a different DB to used for multiple databases

1 SP on a different DB to used for multiple databases

1 SP on a different DB to used for multiple databases

(OP)
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.

RE: 1 SP on a different DB to used for multiple databases

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

RE: 1 SP on a different DB to used for multiple databases

(OP)
Would not a SYNONYM be even better? CREATE SYNONYM and then DROP it?

RE: 1 SP on a different DB to used for multiple databases

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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close