Avatar of Dale Fye
Dale Fye
Flag for United States of America

asked on 

Create dynamic SQL string including OpenQuery method and a criteria which includes dates.

SQL Server 2008R2

I have a linked server which I have only been able to query using the OpenQuery syntax:
SELECT TOP 10 * FROM OPENQUERY (OR_Azure_Server, 'SELECT * FROM xxxxx_VHS.HISTORICALVALUES')

Open in new window

I'm working on a stored procedure which will run every 15 minutes which will extract data for the previous 15 minutes and then run through a series of transformations before storing the data in a data warehouse (of sorts).

I'm trying to construct a dynamic SQL string that I can use in an openquery SELECT statement, but I cannot get the syntax correct.  My current code looks like but will eventually include an INTO clause in the SQL statement to create a temporary table from this recordset.

declare @FromDT datetime
declare @ThruDT datetime
declare @SQL nvarchar(max)
declare @dynSQL nvarchar(max)

set @FromDT = getdate()
--Convert the current time to the previous quarter hour value 2019-01-30 09:53:00 => 2019-01-30 09:45:00
Set @FromDT = CAST(Cast(Cast(CAST(@FromDT as float) * 24 * 4 as int) as float)/24 / 4 as datetime)
Set @ThruDT = DATEADD(MINUTE, 15, @FromDT )

SELECT @FromDT as [FROM] , @ThruDT as Thru
Set @SQL = 'SELECT * FROM xxxxx_VHS.HISTORICALVALUES ' 
         + 'WHERE [RecordTime] > = ' + CHAR(39) + CHAR(39) + convert(varchar(20), @FromDT, 120) + CHAR(39) + CHAR(39)
         + '  AND [RecordTime] < ' + CHAR(39) + CHAR(39) +  convert(varchar(20), @ThruDT, 120) + CHAR(39)+ CHAR(39)
select @SQL 

SET @DynSQL = 'SELECT * FROM OPENQUERY (OR_Azure_Server, ' + char(39) + @sql + CHAR(39) + ')'
SELECT @dynSQL 

Open in new window


When I run this, and copy the results of @dynSQL into the query design window, I get:
SELECT * FROM OPENQUERY (OR_Azure_Server, 
'SELECT * FROM xxxxx_VHS.HISTORICALVALUES 
WHERE [RecordTime] > = ''2019-01-30 07:15:00''  AND [RecordTime] < ''2019-01-30 07:30:00''')

Open in new window

and when I attempt to run that, I get the following error message:
OLE DB provider "MSDASQL" for linked server "OR_Azure_Server" returned message "[CygNet][ODBC EOP driver][OpenAccess SDK SQL Engine]Syntax error in SQL statement. syntax error line 1 at or after token <RecordTime>.[10179]".

Msg 7321, Level 16, State 2, Line 1
An error occurred while preparing the query "SELECT * FROM xxxxx_VHS.HISTORICALVALUES WHERE [RecordTime] > = '2019-01-30 07:15:00'  AND [RecordTime] < '2019-01-30 07:30:00'" for execution against OLE DB provider "MSDASQL" for linked server "OR_Azure_Server".

When I remove the last single quote that line, it looks like the following, and returns the following error message:
SELECT * FROM OPENQUERY (OR_Azure_Server, 
'SELECT * FROM xxxxx_VHS.HISTORICALVALUES 
WHERE [RecordTime] > = ''2019-01-30 07:15:00''  AND [RecordTime] < ''2019-01-30 07:30:00'')

Open in new window

Msg 105, Level 15, State 1, Line 1
Unclosed quotation mark after the character string 'SELECT * FROM xxxxx_VHS.HISTORICALVALUES WHERE [RecordTime] > = '2019-01-30 07:15:00'  AND [RecordTime] < '2019-01-30 07:30:00')
'.
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near 'SELECT * FROM xxxxx_VHS.HISTORICALVALUES WHERE [RecordTime] > = '2019-01-30 07:15:00'  AND [RecordTime] < '2019-01-30 07:30:00'.

I'm certain this has to do with the embedded quotes necessary for the date criteria but I cannot figure out what I'm doing wrong.  Would appreciate any recommendations.
Microsoft SQL ServerSQL

Avatar of undefined
Last Comment
Dale Fye

8/22/2022 - Mon