dbTalk Databases Forums  

Dynamic database selection?

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss Dynamic database selection? in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Ed Murphy
 
Posts: n/a

Default Dynamic database selection? - 06-14-2007 , 08:32 PM






I'm working on a script to convert data from one software package
to another. Greatly simplified, it looks something like

create procedure import_widget as
begin

insert into our_widget (foo, bar)
select baz, quux from their_db.dbo.their_widget

end
go

The problem is that the name of the source database varies from
one system to another, so I want to pass the database name as a
parameter. I think I could do the following, but is there a
better way to go about it?

create procedure import_widget (@db_name sysname) as
begin

exec 'create view their_widget as select * from '
+ @db_name + '.dbo.their_widget'

insert into our_widget (foo, bar)
select baz, quux from their_widget

drop view their_widget

end
go

Reply With Quote
  #2  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: Dynamic database selection? - 06-15-2007 , 04:10 PM






Ed Murphy (emurphy42 (AT) socal (DOT) rr.com) writes:
Quote:
The problem is that the name of the source database varies from
one system to another, so I want to pass the database name as a
parameter. I think I could do the following, but is there a
better way to go about it?
On SQL 2005 you could use synonyms:

CREATE SYNONYM mytable AS thatdatabase.dbo.hertable

When you move to a new database you only need to update the synonyms.


--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx


Reply With Quote
  #3  
Old   
Ed Murphy
 
Posts: n/a

Default Re: Dynamic database selection? - 06-15-2007 , 05:12 PM



Erland Sommarskog wrote:

Quote:
Ed Murphy (emurphy42 (AT) socal (DOT) rr.com) writes:
The problem is that the name of the source database varies from
one system to another, so I want to pass the database name as a
parameter. I think I could do the following, but is there a
better way to go about it?

On SQL 2005 you could use synonyms:

CREATE SYNONYM mytable AS thatdatabase.dbo.hertable

When you move to a new database you only need to update the synonyms.
Alas, this is SQL 2000 (or at least I expect it will be in a
significant number of cases).


Reply With Quote
  #4  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: Dynamic database selection? - 06-16-2007 , 04:23 AM



Ed Murphy (emurphy42 (AT) socal (DOT) rr.com) writes:
Quote:
Erland Sommarskog wrote:

Ed Murphy (emurphy42 (AT) socal (DOT) rr.com) writes:
The problem is that the name of the source database varies from
one system to another, so I want to pass the database name as a
parameter. I think I could do the following, but is there a
better way to go about it?

On SQL 2005 you could use synonyms:

CREATE SYNONYM mytable AS thatdatabase.dbo.hertable

When you move to a new database you only need to update the synonyms.

Alas, this is SQL 2000 (or at least I expect it will be in a
significant number of cases).
Then the best may be to have a stored procedure in the other database
to retreive that data. You still need to construct the procedure name
dynamically, but since EXEC accepts a variable for the procedure name,
you don't have to use dynamic SQL.

That is you can say:

SELECT @sp_name = @dbname + '..that_sp'
EXEC @sp_name


--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.