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 2005 Forums
 Transact-SQL (2005)
 pass db name as variable

Author  Topic 

rudba
Constraint Violating Yak Guru

415 Posts

Posted - 2010-10-13 : 15:40:08
I tried to pass database name by declared variable, getting the invalid object name error.

Anybody know how to pass db name as varibale


DECLARE @DbName varchar(50)
SET @DbName='MyDbName'
Print @DbName
select * from [@Dbname].dbo.tblName

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2010-10-13 : 15:42:18
Use dynamic SQL.



CODO ERGO SUM
Go to Top of Page

rudba
Constraint Violating Yak Guru

415 Posts

Posted - 2010-10-13 : 15:45:04
i get dynamic value on @DbName, i used the CURSOR so it always get dynamic value.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-10-17 : 03:11:14
exec ('select * from '+[@Dbname]+'.dbo.tblName')

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -