title: SQL OpenQuery incorrect syntax near string
body: <p>I am trying to run the following query in a stored procedure. When I try to run it with hardcoded values instead of variables it runs fine. But just when I try to enclose the whole query into a variable and execute it it gives me an error saying: <code>Incorrect syntax near 'FIFO'.</code> </p>
  
  <p>I tried replacing single quotes with double quotes but of no use. Please Help!</p>
  
  <pre><code>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 )&lt;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;
  </code></pre>