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)
 Substitute database object with a variable

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2001-11-01 : 09:06:11
Todd Peterson writes "I'm on a project where I need to use a variable for the database object, but I've found this can't be done. If I can't, then I'll have to put my script in EACH database. Can someone show me how to do this using this simple select statement using the Northwind database?

This works.....
DECLARE @db_name varchar(50)
SET @db_name = 'Northwind'
SELECT address,city
FROM NorthWind..Customers

This doesn't....
DECLARE @db_name varchar(50)
SET @db_name = 'Northwind'
SELECT address,city
FROM @db_name..Customers

Thank you!!"
   

- Advertisement -