linked server variable

  • I'm trying to pass in a variable

    to a store procedure. Based on the variable the s/p will look at the linked server

    DECLARE @my_LinkedServer

    SELECT COUNT(brands)

    FROM @my_LinkedServer.Linkedserverlocal.dbo.brands

    I get the error Incorrect syntax near '.'.

    However if I hard code the @my_LinkedServer it works fine

  • You can use dynamic sql to accomplish this:

    declare @my_LinkedServer varchar(100)

    declare @sql varchar(255)

    set @my_LinkedServer = 'Server1'

    set @sql = 'select count(brands) from [' + @my_LinkedServer + '].[Linkedserverlocal].[dbo].[brands]'

    print @sql

    --exec (@sql)

    - Jeff

  • Excellent!

    Worked great Thanks

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

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