3

Hi I want to fetch data from MS SQL DB of a Remote server using Linked Servers. I have linked the servers. I am fetching data as follows.

select * from [IP of server].[Database Name].[dbo].[Table Name]

But I want to get [IP address] from a variable and use it.

Ex: Declare @ServIP varchar(50)

set @ServIP = 'IP of server'

select * from [@ServIP].[Database Name].[dbo].[Table Name]

I'm getting error If I use variable.

How can I do it?

2
  • Why are you using the IP address?
    – ChrisBint
    Jul 19, 2012 at 15:02
  • I'm accessing Remote Server's data. I need to mention which Server.
    – Ethan
    Jul 19, 2012 at 15:25

1 Answer 1

0

Dynamic SQL:

declare @ServIP varchar(50) = 'IP of server'
declare @dsql varchar(1024)
set @dsql = 'select * from ' + quotename(@ServIP) + '.DbName.dbo.TableName'
exec (@dsql)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.