0

Possible Duplicate:
How to use variable for database name in t-sql

If I do Select * from Production.dbo.MatchTable it works.(where Production is database name)

Now I need to use it in a variable first as

Declare @dbName As varchar(20) = 'Test'

And then if I do Select * from @dbName.dbo.MatchTable I get an error

Msg 102, Level 15, State 1, Line 5 Incorrect syntax near '.'.

Then how can I use it.

The reason is that, at the time of testing we will change the db name. So changing the same in many places in many store procs will be an issue.

Also I cannot write a dynamic query.

So what may be the way of doing it? Synonyms won't work as it is not applicable for database name.

Thanks

Community
  • 1
  • 1
  • 1
    Why don't you have synonyms for the objects? Otherwise you will need to resign yourself to a global search and replace at the time of testing. You can't use a variable for a database name unless you are using dynamic SQL. – Aaron Bertrand Sep 20 '11 at 12:21
  • Use the `Data Source` property of the connection string to SQL Server. `Data Source` specifies the "current" database and you don't need to specify database in the queries to query the current database. – Mikael Eriksson Sep 20 '11 at 12:32
  • Sir, the probel is that, we are refering some tables that are in different db. – lightswitchlover Sep 20 '11 at 12:54

0 Answers0