I have SQL Server 2019 and have linked an SQLite database called [PSMDB_WIN_SVR] to it.
My objective is to retrieve data from the linked database for all records greater than or equal to a double variable parameter representing Julian Date/Time. The query runs fine in the native SQLite environment:
SELECT julianDateUTC,
frequency,
Vrms,
I1rms,
I2rms,
I3rms,
P1,
P2,
P3,
ProcessorTemp,
Quality
FROM tblRealTimeData
WHERE julianDateUTC >= :JD
ORDER BY julianDateUTC;
I have created the following dynamic stored procedure in SQL Server:
USE [pMonNetworkServer]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE getNewDataProc
@jd VARCHAR(50)
AS
DECLARE @sql VARCHAR(MAX)
SET @sql = 'SELECT * FROM OPENQUERY(PSMDB_WIN_SVR, ' +
'' + 'SELECT julianDateUTC,
frequency,
Vrms,
I1rms,
I2rms,
I3rms,
P1,
P2,
P3,
ProcessorTemp,
Quality
FROM tblRealTimeData
WHERE julianDateUTC >= ' + '' + @jd + '' +
' ORDER BY julianDateUTC;' + '' + ')'
EXEC (@sql)
GO
When I execute this using SSMS I get the following errors:
Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'SELECT'.Msg 102, Level 15, State 1, Line 15
Incorrect syntax near ')'.
Obviously my syntax is incorrect. The OPENQUERY
SQL string has to be set in quotes as a literal and I am using '' for this.
I have also surrounded the @jd
parameter in a single quote. I also tried without this, but I get the error whatever I do.
Can someone help with this please?