将查询作为变量执行时出错

时间:2015-12-04 22:54:12

标签: sql-server

我正在尝试将查询作为变量@text执行。我的代码在下面 -

  declare @text as varchar(8000)
 set @text='DECLARE @MIN_DAY INTEGER,              
  @MAX_DAY INTEGER,               
  @YEARMONTH VARCHAR(6),             
  @SQL VARCHAR(8000),                
  @I INT,                    
  @ROWS INT,                 
  @SOURCECOUNT INT        

  SELECT * 
  into #S
  from openquery(dwprod,''select extract(month from to_date(time_partition,''YYYYMMDD'')) as month ,count(*) as sourcecount    
  from dw.fact_sales_actuals  a          
  join dw.dim_legacy_division b on         
  a.legacy_division_id=b.legacy_division_id    
  where time_partition >=   (select min(new_time_id)    
  from dw.dim_time  
  where EXTRACT(month FROM to_date(new_time_id,''YYYYMMDD''))>=EXTRACT(month FROM add_months(sysdate,-2))
  and EXTRACT(year FROM to_date(new_time_id,''YYYYMMDD''))>=EXTRACT(year FROM add_months(sysdate,-2)))

  AND b.ACTIVE_IND=''Y''  
  AND b.LOCATION_TYPE_CD=''D''    
  AND b.US_AREA<>''SEG''  
  AND B.LEGACY_DIVISION_ID<>''6374534''
  group by  extract(month from to_date(time_partition,''YYYYMMDD''))'')  

   select ROW_NUMBER() OVER (order by month asc) as Row,s.*
   into #SOURCECOUNT
   from #S s

SELECT  @MIN_DAY=MIN(TIME_PARTITION),  
         @MAX_DAY=MAX(TIME_PARTITION)   
          FROM DW_EXTRACT..DW_FACT_SALES_ACTUALS
          where TIME_PARTITION>= (select min(new_time_id)    
       from dw_extract.dbo.DIM_TIME_2
       where FISCAL_YEAR_MONTH=dw_extract.dbo.GetFiscalMonth(GETDATE())-2)   

  DECLARE @MONTHS TABLE      
  (MONTH_ID INTEGER IDENTITY(1,1),     
  FISCAL_YEAR_MONTH VARCHAR(20),      
  MIN_DATE INTEGER,      
  MAX_DATE INTEGER)           
   INSERT INTO @MONTHS (FISCAL_YEAR_MONTH, MIN_DATE, MAX_DATE)      
   SELECT  ''FSA''+CAST(FISCAL_YEAR_MONTH AS VARCHAR) AS FISCAL_YEAR_MONTH ,            
   MIN(NEW_TIME_ID) MIN_DATE,            
   MAX(NEW_TIME_ID) MAX_DATE           
   FROM DW_EXTRACT..DIM_TIME_2     
   WHERE NEW_TIME_ID BETWEEN @MIN_DAY AND @MAX_DAY      
   GROUP BY FISCAL_YEAR_MONTH              

   SET @ROWS=@@ROWCOUNT      
   SET @I=1        
   WHILE @I<=@ROWS          
   BEGIN 
   set @SOURCECOUNT=(select SOURCECOUNT from #SOURCECOUNT where ROW=@I)
   select @sql=''INSERT INTO   dqt.dbo.rk_DailyLoadValidation
   (TableName, SubCategory, SubCategoryName, SCServer, Oracle, ValidationDate)      
   SELECT ''+FISCAL_YEAR_MONTH+'',''+FISCAL_YEAR_MONTH+'',''+FISCAL_YEAR_MONTH+'',SCServer,ORACLE,GETDATE()      
   FROM (SELECT count(*)  SCServer,''+CONVERT(VARCHAR,@SOURCECOUNT)+'' AS ORACLE         
   FROM dw_extract.dbo.dw_fsa_''+RIGHT(FISCAL_YEAR_MONTH,6)+'' A with (nolock)               
   JOIN DW_EXTRACT.DBO.dim_legacy_division b        
   ON  a.legacy_division_id=b.legacy_division_id        
   WHERE  time_partition between ''+convert(varchar,min_date)+''       
   AND ''+convert(varchar,max_date)+''                    
   AND b.ACTIVE_IND=''Y''           
    AND b.LOCATION_TYPE_CD=''D''                
    AND b.US_AREA<>''SEG'')     A    ''       
    FROM @MONTHS    
    where month_id=@I    
    exec (@sql)     
     set @i=@i+1     
      end'

      exec (@text)

我收到以下错误 -

Msg 102, Level 15, State 1, Line 11
Incorrect syntax near 'YYYYMMDD'.
Msg 102, Level 15, State 1, Line 64
Incorrect syntax near 'Y'.

运行查询(直接)非常有效。很明显,我没有使用正确的格式将查询存储在变量中。任何人都可以指出我做错了吗?

1 个答案:

答案 0 :(得分:0)

from openquery(dwprod,''select extract(month from to_date(time_partition,''YYYYMMDD'')) as month ,count(*) as sourcecount    

似乎打算包含一个从select extract ...开始的语句,并在其中包含另一个引用的日期格式YYYYMMDD

但是,我认为<{1}}之前的'' 实际上是在终止select语句。