我试图在存储过程中运行以下查询。当我尝试使用硬编码值而不是变量运行它时它运行正常。但是当我尝试将整个查询括起来并执行它时,它会给我一个错误说:Incorrect syntax near 'FIFO'.
我尝试用双引号替换单引号但没有用。请帮助!
DECLARE @TSQL nvarchar(max); Declare @STATION_CODE varchar(max); Declare @STATION_CODE_PREV varchar(max);
SET @STATION_CODE='1600020'; SET @STATION_CODE_PREV='1600007'; SET @TSQL='SELECT * FROM OPENQUERY (VFDB,''SELECT COUNT(TABLE_7.FIFO) AS FIFO_COUNT,TABLE_7.FIFO,TABLE_7.Day_Hour ,TABLE_7.Station_Code,''FIFO'' AS Element FROM ( select CAST(CASE WHEN TABLE_6 .BCD_PRV2_EXIT=TABLE_5 .BCD_PRV2 THEN ''1'' ELSE ''0'' END AS INT) FIFO, case when datepart(hour,TABLE_6.CREATION_DATE_E3 )<10
then
''0''
+Right (Cast(datepart(hour,TABLE_6.CREATION_DATE_E3 + 100) As Varchar) ,2)+'':00 - ''+''0''
+Right(Cast(datepart(hour,TABLE_6.CREATION_DATE_E3 + 100) As Varchar) ,2)+'':59''
else
Right (Cast(datepart(hour,TABLE_6.CREATION_DATE_E3 + 100) As Varchar) ,2)+'':00 - ''
+Right(Cast(datepart(hour,TABLE_6.CREATION_DATE_E3 + 100) As Varchar) ,2)+'':59''
end As Day_Hour ,TABLE_6.Station_Code As Station_Code from
( select T1_ENTRY.RANK1_ENTRY AS T5_R1, T1_ENTRY.BCD_PRV1,T1_ENTRY.CREATION_DATE_E1,T2_ENTRY.RANK2_ENTRY , T2_ENTRY.BCD_PRV2 ,T2_ENTRY.CREATION_DATE_E2 from
(
select MFG_UNIT_NUM as BCD_PRV1,CREATION_DATE AS CREATION_DATE_E1 ,ROW_NUMBER()over(order by Creation_Date) as RANK1_ENTRY
FROM dbo.DAW_FN_STATS_TLDTLRAW(115, ''REFRIGERATOR'', NULL, '''''+ @STATION_CODE +''''', NULL,Dateadd(HOUR, Datediff(HOUR,0,GETDATE()),0),Dateadd(hh,1, Dateadd(HOUR, Datediff(HOUR,0,GETDATE()),0)))RAWDTL
) T1_ENTRY LEFT JOIN
(
select MFG_UNIT_NUM as BCD_PRV2,CREATION_DATE AS CREATION_DATE_E2,ROW_NUMBER()over(order by Creation_Date) as RANK2_ENTRY
FROM dbo.DAW_FN_STATS_TLDTLRAW(115, ''REFRIGERATOR'', NULL, '''''+ @STATION_CODE_PREV +''''', NULL,Dateadd(HOUR, Datediff(HOUR,0,GETDATE()),0),Dateadd(hh,1, Dateadd(HOUR, Datediff(HOUR,0,GETDATE()),0)))RAWDTL
) T2_ENTRY ON T1_ENTRY.RANK1_ENTRY=T2_ENTRY.RANK2_ENTRY-1
) TABLE_5
LEFT JOIN
( select T3_EXIT.RANK1_EXIT AS T6_R1, T3_EXIT.BCD_PRV1_EXIT,T3_EXIT.CREATION_DATE_E3,T4_EXIT.RANK2_EXIT , T4_EXIT.BCD_PRV2_EXIT ,T4_EXIT.CREATION_DATE_E4,T3_EXIT.Station_Code from
(
select MFG_UNIT_NUM as BCD_PRV1_EXIT,CREATION_DATE AS CREATION_DATE_E3 ,ROW_NUMBER()over(order by Creation_Date) as RANK1_EXIT,STATION_CODE as STATION_CODE
FROM dbo.DAW_FN_STATS_TLDTLRAW(115,''REFRIGERATOR'', NULL, '''+@STATION_CODE+''',NULL,Dateadd(HOUR, Datediff(HOUR,0,GETDATE()),0),Dateadd(hh,1, Dateadd(HOUR, Datediff(HOUR,0,GETDATE()),0)))RAWDTL
) T3_EXIT LEFT JOIN
(
select MFG_UNIT_NUM as BCD_PRV2_EXIT,CREATION_DATE AS CREATION_DATE_E4,ROW_NUMBER()over(order by Creation_Date) as RANK2_EXIT,STATION_CODE as STATION_CODE
FROM dbo.DAW_FN_STATS_TLDTLRAW(115,''REFRIGERATOR'', NULL,'''+@STATION_CODE_PREV+''', NULL,Dateadd(HOUR, Datediff(HOUR,0,GETDATE()),0),Dateadd(hh,1, Dateadd(HOUR, Datediff(HOUR,0,GETDATE()),0)))RAWDTL
) T4_EXIT ON T3_EXIT.RANK1_EXIT=T4_EXIT.RANK2_EXIT-1 ) TABLE_6 ON TABLE_5.BCD_PRV2=TABLE_6.BCD_PRV2_EXIT )TABLE_7 GROUP BY TABLE_7.FIFO,TABLE_7.Day_Hour ,Table_7. STATION_CODE'')';
EXEC sp_executesql @TSQL;
答案 0 :(得分:1)
您已经得到了答案,所以这只是一个提示,您将来如何轻松解决此类问题。
declare @sql_string varchar(100)
set @sql_string = 'SELECT * FROM OPENQUERY (VFDB,''SELECT * FROM foo WHERE bar = ''''i am a string'''';'')'
select @sql_string --> This here will let you see how your string looks like
答案 1 :(得分:0)
假设你有一些带字符串的动态SQL:
'SELECT * FROM foo WHERE bar = ''i am a string'';'
-- ^^ ^^
-- || ||
-- notice the double quotes
现在让我们在动态SQL(你的情况)中使用OPENQUERY
:
'SELECT * FROM OPENQUERY (VFDB,''SELECT * FROM foo WHERE bar = ''''i am a string'''';'')'
-- ^^ ^^^^
-- || ||||
-- double quotes escaped double quotes => 4 quotes
您在字符串中嵌套了查询,因此每次添加嵌套级别时,都必须将引号加倍以避免关闭上层嵌套级别。
强烈劝阻,因为调试真的很痛苦,正如你猜测的那样,没有提到乱七八糟的变量连接是什么(但你已经使用了5个引号,所以你已经知道了:))。
我尝试修复引号(无法测试它,所以也许我在某些时候搞砸了):
DECLARE @TSQL nvarchar(max); Declare @STATION_CODE varchar(max); Declare @STATION_CODE_PREV varchar(max);
SET @STATION_CODE='1600020'; SET @STATION_CODE_PREV='1600007'; SET @TSQL='SELECT * FROM OPENQUERY (VFDB,''''SELECT COUNT(TABLE_7.FIFO) AS FIFO_COUNT,TABLE_7.FIFO,TABLE_7.Day_Hour ,TABLE_7.Station_Code,''''FIFO'''' AS Element FROM ( select CAST(CASE WHEN TABLE_6 .BCD_PRV2_EXIT=TABLE_5 .BCD_PRV2 THEN ''''1'''' ELSE ''''0'''' END AS INT) FIFO, case when datepart(hour,TABLE_6.CREATION_DATE_E3 )<10
then
''''0''''
+Right (Cast(datepart(hour,TABLE_6.CREATION_DATE_E3 + 100) As Varchar) ,2)+'''':00 - ''''+''''0''''
+Right(Cast(datepart(hour,TABLE_6.CREATION_DATE_E3 + 100) As Varchar) ,2)+'''':59''''
else
Right (Cast(datepart(hour,TABLE_6.CREATION_DATE_E3 + 100) As Varchar) ,2)+'''':00 - ''''
+Right(Cast(datepart(hour,TABLE_6.CREATION_DATE_E3 + 100) As Varchar) ,2)+'''':59''''
end As Day_Hour ,TABLE_6.Station_Code As Station_Code from
( select T1_ENTRY.RANK1_ENTRY AS T5_R1, T1_ENTRY.BCD_PRV1,T1_ENTRY.CREATION_DATE_E1,T2_ENTRY.RANK2_ENTRY , T2_ENTRY.BCD_PRV2 ,T2_ENTRY.CREATION_DATE_E2 from
(
select MFG_UNIT_NUM as BCD_PRV1,CREATION_DATE AS CREATION_DATE_E1 ,ROW_NUMBER()over(order by Creation_Date) as RANK1_ENTRY
FROM dbo.DAW_FN_STATS_TLDTLRAW(115, ''''REFRIGERATOR'''', NULL, '''''+ @STATION_CODE +''''', NULL,Dateadd(HOUR, Datediff(HOUR,0,GETDATE()),0),Dateadd(hh,1, Dateadd(HOUR, Datediff(HOUR,0,GETDATE()),0)))RAWDTL
) T1_ENTRY LEFT JOIN
(
select MFG_UNIT_NUM as BCD_PRV2,CREATION_DATE AS CREATION_DATE_E2,ROW_NUMBER()over(order by Creation_Date) as RANK2_ENTRY
FROM dbo.DAW_FN_STATS_TLDTLRAW(115, ''''REFRIGERATOR'''', NULL, '''''+ @STATION_CODE_PREV +''''', NULL,Dateadd(HOUR, Datediff(HOUR,0,GETDATE()),0),Dateadd(hh,1, Dateadd(HOUR, Datediff(HOUR,0,GETDATE()),0)))RAWDTL
) T2_ENTRY ON T1_ENTRY.RANK1_ENTRY=T2_ENTRY.RANK2_ENTRY-1
) TABLE_5
LEFT JOIN
( select T3_EXIT.RANK1_EXIT AS T6_R1, T3_EXIT.BCD_PRV1_EXIT,T3_EXIT.CREATION_DATE_E3,T4_EXIT.RANK2_EXIT , T4_EXIT.BCD_PRV2_EXIT ,T4_EXIT.CREATION_DATE_E4,T3_EXIT.Station_Code from
(
select MFG_UNIT_NUM as BCD_PRV1_EXIT,CREATION_DATE AS CREATION_DATE_E3 ,ROW_NUMBER()over(order by Creation_Date) as RANK1_EXIT,STATION_CODE as STATION_CODE
FROM dbo.DAW_FN_STATS_TLDTLRAW(115,''''REFRIGERATOR'''', NULL, '''''+@STATION_CODE+''''',NULL,Dateadd(HOUR, Datediff(HOUR,0,GETDATE()),0),Dateadd(hh,1, Dateadd(HOUR, Datediff(HOUR,0,GETDATE()),0)))RAWDTL
) T3_EXIT LEFT JOIN
(
select MFG_UNIT_NUM as BCD_PRV2_EXIT,CREATION_DATE AS CREATION_DATE_E4,ROW_NUMBER()over(order by Creation_Date) as RANK2_EXIT,STATION_CODE as STATION_CODE
FROM dbo.DAW_FN_STATS_TLDTLRAW(115,''''REFRIGERATOR'''', NULL,'''''+@STATION_CODE_PREV+''''', NULL,Dateadd(HOUR, Datediff(HOUR,0,GETDATE()),0),Dateadd(hh,1, Dateadd(HOUR, Datediff(HOUR,0,GETDATE()),0)))RAWDTL
) T4_EXIT ON T3_EXIT.RANK1_EXIT=T4_EXIT.RANK2_EXIT-1 ) TABLE_6 ON TABLE_5.BCD_PRV2=TABLE_6.BCD_PRV2_EXIT )TABLE_7 GROUP BY TABLE_7.FIFO,TABLE_7.Day_Hour ,Table_7. STATION_CODE'')';
EXEC sp_executesql @TSQL;