Greetings,I have a Linked Server that I can query using a passed parameter like this ( I am setting the variable just for testing) :DECLARE @URN varchar(15)DECLARE @MyString VARCHAR(MAX) SET @URN = '1234567890' SET @MyString = 'SELECT URN ,SEQNO ,MNEMONIC FROM OrderList WHERE URN = ''' + @URN + ''' ' SET @MyString = N'SELECT * FROM OPENQUERY(LINKSRV,''' + REPLACE(@MyString, '''', '''''') + ''')' EXEC (@MyString)
This query will return something like this: URN SEQNUM MNEMONIC 1234567890 1 ALIBABA1234567890 3 SHAZAAM1234567890 2 KABOOM
Now I would normally do something like this:DECLARE @MyString VARCHAR(MAX)DECLARE @workTB TABLE ( URN VARCHAR(MAX) ,SEQNO VARCHAR(MAX) ,Mnemonic VARCHAR(MAX) ) INSERT INTO @workTB ( URN ,SEQNO ,Mnemonic )SELECT * FROM OPENQUERY(LINKSRV,'SELECT URN ,SEQNO ,MNEMONIC FROM OrderList WHERE URN = '1234567890'SELECT * FROM @WorkTB
But my problem is, This last approach is not dynamic and I need to utilize the passed parameter. But OPENQUERY does not just simply accept a parameter.So due to my inexperience, I'm stuck. I need to query the Linked Server with a query that contains a passed parameter and then insert those results into a separate table. But I'm unsure of how to place the dynamically built query into the INSERT statement.I appreciate any guidance.