Linked ServerName as variable

  • Hi,

    How can I pass the linked servername as variable in the stored porcedure.?

    I have multiple linked servers having identical databases and I have to use this SP to fetch data from them.

    the statement in SP is as below :

    [font="Courier New"]select invoicedate,invoiceno,invoicenet from [SVR1].TestPOS.dbo.invoice_header[/font]

    how can I replace [SVR1] with a variable..?

    thanks in advance

    regards,

    Thomas George

  • You have to use dynamic sql for this one.

    e.g.

    DECLARE @SQLString nvarchar(500),

    @srv VARCHAR(25)

    SET @srv = 'MyServerName'

    SET @SQLString = N'

    SELECT

    invoicedate,

    invoiceno,

    invoicenet

    FROM [' + @srv + '].TestPOS.dbo.invoice_header

    '

    EXECUTE sp_executesql @SQLString

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

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