1

I am trying to Execute a stored procedure that requires to variables be passing into to it. One is a static, the other is a dynamic variable.

DECLARE @Filt DATETIME 
SET @Filt = (SELECT DISTINCT MAX(Date) FROM Data.db.Staging)
SELECT * INTO #tempData FROM OPENROWSET('SQLNCLI', 'Server=ISR14  \MSSQL2012;Trusted_Connection=yes;', 'EXEC GetData.db.Staging @Mode = ''Date'' @Filt ')

but that doesn't work, got the error back "Msg 8180, Level 16, State 1, Line 1 Statement(s) could not be prepared. Msg 102, Level 15, State 1, Line 1 Incorrect syntax near '@Filt'."

I'm guessing it is because Filt is dynamic statement. So I tried this

DECLARE @FilterData DATETIME
DECLARE @sql VARCHAR(200) 
SET @Filt = (SELECT DISTINCT MAX(AsOfDate) FROM Data.db.Staging)
SET @sql = 'EXEC GetData.db.Staging @Mode = ''Date'' @Filt =  '  + @Filt

SELECT * INTO #tempData FROM OPENROWSET('SQLNCLI', 'Server=ISR14\MSSQL2012;Trusted_Connection=yes;',
 @sql)

But I get the message back

"Msg 102, Level 15, State 1, Line 24 Incorrect syntax near '@sql'."

It seems that OPENROWSET can only accept strings. But I want to pass a variable that is dynamic.

3 Answers 3

2

You have to put the whole statement into a variable and run it, and convert @FilterData to a varchar to concatenate it.

You can't use variables with openquery/openrowset.

Try this and check the print output... if it works and looks ok, then EXEC(@sql2)

DECLARE @FilterData DATETIME
DECLARE @sql VARCHAR(200), @sql2 VARCHAR(500)
SET @FilterData = '2014-07-01'--(SELECT DISTINCT MAX(AsOfDate) FROM Data.db.Staging)
SET @sql = 'EXEC GetData.db.Staging @Mode = ''''Date'''', @Filt =  '''''  + CONVERT(VARCHAR(20),@FilterData ,120) + ''''''

SET @sql2 = 'SELECT * INTO #tempData FROM OPENROWSET(''SQLNCLI'', ''Server=ISR14\MSSQL2012;Trusted_Connection=yes;'',
 '''+@sql+''')'

print @sql2
--exec(@sql2)
Sign up to request clarification or add additional context in comments.

2 Comments

That definitely helped! However, I'm now getting this issue: Msg 102, Level 15, State 1, Line 2 Incorrect syntax near '2014'. Msg 105, Level 15, State 1, Line 2 Unclosed quotation mark after the character string ')'.
Updated, was missing a ,. I'm not seeing any un-closed quotation marks though. Did you get it sorted out?
1

You need to make the whole query dynamic, not sure if I got it nailed down, but something like:

DECLARE @Filt DATETIME 
       ,@sql VARCHAR(MAX)
SET @Filt = (SELECT MAX(Date) FROM Data.db.Staging)
SET @sql = 'SELECT * INTO #tempData FROM OPENROWSET(''SQLNCLI'', ''Server=ISR14  \MSSQL2012;Trusted_Connection=yes;'', ''EXEC GetData.db.Staging @Mode = ''''Date''' +@Filt+ ')'
EXEC (@sql)

2 Comments

That definitely helped! However, I'm now getting this issue: Msg 102, Level 15, State 1, Line 2 Incorrect syntax near '2014'. Msg 105, Level 15, State 1, Line 2 Unclosed quotation mark after the character string ')'. Where 2014 is a part of the MAX date found in @Filt
@user3259896 Can you show me a string that executes with a hard-coded value instead of the dynamic version? I'm just not sure how EXEC GetData.db.Staging @Mode = ''''Date''' +@Filt+ ') should get structured.
0

I got error Incorrect syntax near '' when executing @sql. In my case there was invalid character at the beginning of NVARCHAR. This helped me:

SET @sql = REPLACE(@sql, NCHAR(0xFEFF), '');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.