passing parm to openquery

  • Is it possible to write a stored procedure that accepts a parameter such as a Datetime and then passes that parm to an OpenQuery statement that is accessing data via a Linked Server?

  • The only way I've found is to build a dynamic string and pass it through EXEC or sp_executesql (and this gets nasty on the single quotes for string parameters). For instance:

    CREATE PROC usp_TestOpenQuery

    @CustID nchar(5)

    AS

    DECLARE @SQL nvarchar(200)

    SET @SQL = 'SELECT * FROM OPENQUERY(DEVPORT1,

    ''SELECT * FROM Northwind.dbo.Orders WHERE CustomerID = '''''

    + @CustID + ''''''')'

    EXEC sp_executesql @SQL

    GO

    K. Brian Kelley

    bkelley@sqlservercentral.com

    http://www.sqlservercentral.com/columnists/bkelley/

    K. Brian Kelley
    @kbriankelley

  • You could just build the sql and then do a replace for ' with ''. I think Brian has the best idea.

    Steve Jones

    steve@dkranch.net

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

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