Works in SQLFiddle Not in SQL Server 2012 -
when run query in sql fiddle runs perfectly:
;with cte (select analysisvalue.analysisid, heatname, analysistime, sampletype, grade, productid, element, value dbo.analysisvalue inner join dbo.canalysis on dbo.analysisvalue.analysisid = dbo.canalysis.analysisid heatname = 'a7m0066' ) select * s_analysis s cross join (select top 1 analysistime cte order analysisid desc ) c s.heat_no = 'a7m0066' or (s.analysis_datetime between c.analysistime , dateadd(hh, 2, c.analysistime ))
however when run in sql server 2012, receive error leading semi-colon:
msg 170, level 15, state 1, line 1 line 1: incorrect syntax near ';'.
and error when without semicolon:
msg 156, level 15, state 1, line 1 incorrect syntax near keyword 'with'.
instead of cte, use #temp table , see if query valid:
select analysisvalue.analysisid, heatname, analysistime, sampletype, grade, productid, element, value #temptable dbo.analysisvalue inner join dbo.canalysis on dbo.analysisvalue.analysisid = dbo.canalysis.analysisid heatname = 'a7m0066'; select * s_analysis s cross join (select top 1 analysistime #temptable order analysisid desc ) c s.heat_no = 'a7m0066' or (s.analysis_datetime between c.analysistime , dateadd(hh, 2, c.analysistime ))