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)
 Select Query

Author  Topic 

Srain
Starting Member

23 Posts

Posted - 2009-01-31 : 23:01:04
Hi

i have a simple select query which is hardcoded many a times in a SP as the SP fetching data from multiple servers.

select * from [server1].[database1].[dbo].[table1]

Now the requirement is -
To put the [server1].[database1].[dbo].[table1] in a variable & use the variable in the above select query.

Please suggest.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-01-31 : 23:06:12
You can do that via dynamic SQL, but that is highly not recommended for performance reasons.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2009-01-31 : 23:16:35
Remember Your Server Should be Linked to Other Server or instance . Also It is not recommendable as Tara said:

DECLARE @LINKEDSERVER VARCHAR(300)
SET @LINKEDSERVER = 'SERVERNAME.DATABASENAME.SCHEMA.TABLENAME'
EXEC('SELECT * FROM ' + @LINKEDSERVER)
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-02-01 : 01:10:23
or else you need to use OPENROWSET along with dynamic sql.
Go to Top of Page
   

- Advertisement -