Executing Stored Procedure on Linked Servers

  • ***Oops wrong forum; mods please close I will recreate on T-SQL forum***

    I need to execute a storedproc which accepts parameters on a linked server.

    I'm struggling with the syntax though.

    I have two scenarios:

    Scenarion 1 - sp with no parameters

    INSERT INTO tableX

    SELECT * FROM OPENQUERY ( @sourceLinkedServer, 'EXEC linkedDB.dbo.spX')

    The above works fine.

    Now scenario 2: storedproc needs two INT parameters:

    DECLARE @startMonth INT

    DECLARE @endMonth INT

    SET @startMonth = 200901

    SET @endMonth = 200902

    INSERT INTO tableY

    SELECT * FROM OPENQUERY ( @sourceLinkedServer, 'EXEC linkedDB.dbo.spY ' + @startMonth + ', ' + @endMonth)

    The bit that confuses me is the query needs brackets ...

    OPENQUERY ( linked_server ,'query' )

    Any help is appreciated.

    G

  • You can build your SQL String into a variable and use the variable instead of doing the concatentation in OPENQUERY.

    I don't think you are gaining anything by using OPENQUERY for the call over just using 4 part naming or a a SYNONYM. I'd use 4 part naming Server.Database.Schema.Procedure Param1, Param2

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

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