Dynamic database selection?

Dynamic database selection?

am 15.06.2007 03:32:38 von Ed Murphy

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

Re: Dynamic database selection?

am 15.06.2007 23:10:49 von Erland Sommarskog

Ed Murphy (emurphy42@socal.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.


--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downlo ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books .mspx

Re: Dynamic database selection?

am 16.06.2007 00:12:14 von Ed Murphy

Erland Sommarskog wrote:

> Ed Murphy (emurphy42@socal.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).

Re: Dynamic database selection?

am 16.06.2007 11:23:49 von Erland Sommarskog

Ed Murphy (emurphy42@socal.rr.com) writes:
> Erland Sommarskog wrote:
>
>> Ed Murphy (emurphy42@socal.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@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downlo ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books .mspx