Link servers and using a variables

  • I have a link server setup with the following set to true: Collation Compatible, DataAccess, RPC, RPC Out

    The issue is that when running the query below

    SELECT name, address, street FROM table1 WHERE name = Linda

    it resolves the query on the remote server

    but when using a variable the WHERE clause is not resolved on the remote server so the entire table is moved to the local server

    Is there a way or a place to configure sql server to do all the work on the remote server and not on local server when using a variable

     

    Thanks for all your help

     

  • Use dynamic SQL, so that the variable's value is passed to the linked server:

    Declare @SQL varchar(4000)

    Select @SQL = 'SELECT * FROM [Link].[DB}.[Owner].[TableName] Where SomeColumn = ''' + LocalVariable + ''' '

    Exec(@SQL)

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply