Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 USE @database

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2005-08-10 : 07:58:22
Razvan writes "Hi.

I have a sp in which I need to do stuff (like select, insert, update) in 2 databases, so I need to change from time to time between them.

The first database is the database on which I run the sp, so I know its name, and I can use the normal sintax "USE database".

But the name of the second database is known only at runtime, so I have the param @dnName:
select @dbName = DataBase from Accounts where ID = @Account

So... how do I use "USE @dbName" ???

I tried to use a variable @str = 'USE ' + @dbName and then exec (@str) or exec sp_executesql @str, but this doesn't work.

Please help :)

Thanks,

Razvan"

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-08-10 : 08:03:42
Instead use @dbName, you can use

Exec('Select columns from '+@dbName..tableName)

But it is better to avoid passing Database name dynamically

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -