Error with Linked Server Dynamic Query | SQL Server Performance Forums

SQL Server Performance Forum – Threads Archive

Error with Linked Server Dynamic Query

Hi All<br /><br />What I am missing here<br /><pre id="code"><font face="courier" size="2" id="code"><br />Declare @d_StartDate DateTime<br />Declare @d_EndDate DateTime<br />Declare @dt_start varchar(<img src=’/community/emoticons/emotion-11.gif’ alt=’8)’ /><br />Declare @dt_end varchar(<img src=’/community/emoticons/emotion-11.gif’ alt=’8)’ /><br />DECLARE @openquery nvarchar(4000)<br />set @dt_start = convert(varchar (<img src=’/community/emoticons/emotion-11.gif’ alt=’8)’ />,@d_StartDate,112)<br />set @dt_end = convert(varchar (<img src=’/community/emoticons/emotion-11.gif’ alt=’8)’ />,@d_EndDate,112)<br />SELECT @openquery = ‘SELECT * FROM HRD WHERE LastUpdatedstate between ‘+ @dt_start + ‘ and ‘+ @dt_end<br />print @openquery<br />print @dt_start<br />print @dt_end<br />SELECT * FROM OPENQUERY(MYSQL_DBCON, ‘ + @openquery + ‘)<br /><br /><b>Error</b><br />Server: Msg 7357, Level 16, State 2, Line 13<br />Could not process object ‘ + @openquery + ‘. The OLE DB provider ‘MSDASQL’ indicates that the object has no columns.<br />OLE DB error trace [Non-interface error: OLE DB provider unable to process object, since the object has no columnsProviderName=’MSDASQL’, Query= + @openquery + ‘].<br /></font id="code"></pre id="code"><br /><br />Thanks in Advance<br />
Is this for SQL 2000 too? If you search for KBAs thenhttp://support.microsoft.com/default.aspx?scid=kb;en-us;270119 is best one to go. Satya SKJ
Microsoft SQL Server MVP
Writer, Contributing Editor & Moderator
http://www.SQL-Server-Performance.Com
This posting is provided AS IS with no rights for the sake of knowledge sharing. Knowledge is of two kinds. We know a subject ourselves or we know where we can find information on it.
Look at the second parameter here – which is the literal query statement: OPENQUERY(MYSQL_DBCON, ‘ + @openquery + ‘) Imagine you have your QA window up, you’re connected to the remote server, and you run that exact query statement – + @openquery + – what results do you expect to see? The problem with OPENQUERY is that you can only spell out the full query – you cannot use a variable, and you cannot concatenate variables into the statement. You have to concatenate the complete statement into a string, including OPENQUERY, and execute that as a whole. Also note that you have to put single quotes around dates that you spell out as criteria.
I have sort it out and it is like this…<br /><br />Declare @d_StartDate DateTime<br />Declare @d_EndDate DateTime<br />Declare @dt_start varchar(<img src=’/community/emoticons/emotion-11.gif’ alt=’8)’ /><br />Declare @dt_end varchar(<img src=’/community/emoticons/emotion-11.gif’ alt=’8)’ /><br />DECLARE @openquery nvarchar(4000)<br />DECLARE @runquery nvarchar(4000)<br />set @dt_start = convert(varchar (<img src=’/community/emoticons/emotion-11.gif’ alt=’8)’ />,@d_StartDate,112)<br />set @dt_end = convert(varchar (<img src=’/community/emoticons/emotion-11.gif’ alt=’8)’ />,@d_EndDate,112)<br />SET @openquery = ””+’SELECT * FROM HRD WHERE LastUpdatedstate between ‘ + @dt_start + ‘ and ‘ + @dt_end+””<br />SET @runquery = ‘SELECT * from openquery(MYSVR,’ + @openquery + ‘)'<br />EXEC(@runquery)<br /><br />Thanks again <br />sonny<br /><br /><br /><br /><br />
When you dont pass object names as parameters, why do you do it in dynamic sql? Madhivanan Failing to plan is Planning to fail
]]>