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)
 Database name as Variable in Join Query for SQL Server 2005

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2007-02-06 : 09:00:03
Punit writes "Hi
I have a very simple query like

Select *
from table1
inner join table2

Now My problem is that table2 is from a different database. I want to make it a variable(global variabl or keep it in some table) in my application so that I dont have to change its fully qualified name if my datbse name changes. In our application

Table2 is used at many sps and database name changes frequently. So it requires us to change the database name prefixed to table name at many places. Basically i want to get rid of it and make the change at one place."

nr
SQLTeam MVY

12543 Posts

Posted - 2007-02-06 : 10:01:47
exec ('Select *
from table1
inner join ' + @databasename + '..table2')

if the user has permission o the database/table.



==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page
   

- Advertisement -