Msg 156, Levl 15, State 1, Line 1 Incorrect syntax near keyword 'top'

时间:2017-03-30 18:51:33

标签: sql-server

This section of my code is failing. I cannot figure out why I am receiving this message. Can anyone please look it over and see if you see anything that looks wrong?

Thanks so much!!!

    declare @ReportOrder int
    declare @numrows int
    declare @Section varchar(30)
    declare @Dept_Op varchar(5)
    declare @Dept varchar(30)
    declare @SDept_Op varchar(5)
    declare @SDept varchar(30)
    declare @Class_Op varchar(5)
    declare @Class varchar(30)
    declare @Over_POS Int
    declare @year int
    declare @week int
    declare @Perc_To_Total float
    declare @Perc_Remaining float
    declare @LS smalldatetime
    declare @LYCARD_2NC smalldatetime

    declare @sql nvarchar(4000)

    select @year = datepart(yy,getdate())
    select @year = @year - 1
    select @week = staging.dbo.fn_Calendar_Week(getdate())
    select @LS = staging.dbo.fn_Last_Saturday(getdate())
    select @lyCARD_2NC = dateadd(yy,-1,@LS)

    Truncate table ReportData.dbo.rpt_Holiday_Top_Selling_Report_2NC
    Truncate table Staging.dbo.tmp_Top10_2NC

    declare cur cursor for select ReportOrder, numrows, Section, Dept_Op,              Dept, SDept_Op, SDept, Class_Op, Class, Over_POS
    from Reference.dbo.Holiday_Top_Selling_Config_new
    order by ReportOrder

    open cur
    fetch next from cur into @ReportOrder, @numrows, @Section, @Dept_Op,     @Dept, @SDept_Op, @SDept, @Class_Op, @Class, @Over_POS

    while @@Fetch_Status = 0
    begin


    select @sql = 'insert into    Reportdata.dbo.rpt_Holiday_Top_Selling_Report_2NC '
    select @sql = @sql + 'select top ' + cast(@numrows as char(2)) + ' '
    select @sql = @sql + cast(@ReportOrder as char(3)) + ' as PrintOrder,'
    select @sql = @sql + '''' + @Section + '''' + ' as Section,'
    select @sql = @sql + ' row_Number() over (order by t1.wtd_dollars desc) as Rank, '
    select @sql = @sql + ' t1.ISBN as TY_ISBN, '
    select @sql = @sql + ' t1.title as TY_Title, '
    select @sql = @sql + ' t1.Author as TY_Author, '
    select @sql = @sql + ' t1.Dept as TY_Dept_Num, '
    select @sql = @sql + ' t1.SDept as TY_SDept_Num, '
    select @sql = @sql + ' t1.Class as TY_Class_Num, '
    select @sql = @sql + ' t1.display_min, '
    select @sql = @sql + ' t1.Class_Name as TY_Class, '
    select @sql = @sql + ' t1.wtd_Dollars as TY_SLS$, '
    select @sql = @sql + ' t1.wtd_Units as TY_SLSU, '
    select @sql = @sql + ' 0 as Discount, '
    select @sql = @sql + ' t1.OnHand_2NC as OnHand, '
    select @sql = @sql + ' isnull(t1.Qty_OnOrder,0) as OnOrder, '
    select @sql = @sql + ' 0 as Proj, '
    select @sql = @sql + ' NULL as NNTC, '
    select @sql = @sql + ' NULL as LY_ISBN, '
    select @sql = @sql + ' NULL as LY_Title, '
    select @sql = @sql + ' NULL as LY_Author, '
    select @sql = @sql + ' NULL as LY_Dept_Num, '
    select @sql = @sql + ' NULL as LY_SDept_Num, '
    select @sql = @sql + ' NULL as LY_Class_Num, '
    select @sql = @sql + ' NULL as LY_On_Hand, '
    select @sql = @sql + ' NULL as LY_Class, '
    select @sql = @sql + ' NULL as LY_SLS$, '
    select @sql = @sql + ' NULL as LY_SLSU, '
    select @sql = @sql + ' NULL as Forecast_Thru, '
    select @sql = @sql + ' NULL as Week_42, '
    select @sql = @sql + ' NULL as Week_43, '
    select @sql = @sql + ' NULL as Week_44, '
    select @sql = @sql + ' NULL as Week_45, '
    select @sql = @sql + ' NULL as Week_46, '
    select @sql = @sql + ' NULL as Week_47, '
    select @sql = @sql + ' NULL as Week_48, '
    select @sql = @sql + ' NULL as Week_49 '
    select @sql = @sql + ' from dssdata.dbo.CARD_2NC t1'
    select @sql = @sql + ' where t1.dept ' + @Dept_Op + ' ' + @Dept 
    select @sql = @sql + ' and t1.SDept ' + @SDept_Op + ' ' + @SDept
    select @sql = @sql + ' and t1.Class ' + @Class_Op + ' ' + @Class

    if @Over_POS > 0
        select @sql = @sql + ' and t1.Retail > ' + cast(@Over_POS as char(3)) + ' '
        select @sql = @sql + ' order by wtd_Dollars desc '

    EXEC sp_executesql @sql

    select @sql = 'insert into Staging.dbo.tmp_Top10_2NC'
    select @sql = @sql + 'select top ' + cast(@numrows as char(3)) + ' '
    select @sql = @sql + 'row_Number() over (order by t1.LYWeek1dollars desc) as LYRank, '
    select @sql = @sql + 't1.ISBN as ISBN, '
    select @sql = @sql + 't1.Title as Title, '
    select @sql = @sql + 't1.Author, '
    select @sql = @sql + 't1.Dept, '
    select @sql = @sql + 't1.SDept, '
    select @sql = @sql + 't1.Class, '
    select @sql = @sql + 't1.OnHand_2NC, '
    select @sql = @sql + 't1.Class_Name, '
    select @sql = @sql + 't1.LYWeek1Dollars as Sls$, '
    select @sql = @sql + 't1.LYWeek1Units as SlsU '
    select @sql = @sql + 'from  dssdata.dbo.CARD_2NC t1 '
    select @sql = @sql + ' where t1.dept ' + @Dept_Op + ' ' + @Dept 
    select @sql = @sql + ' and t1.SDept ' + @SDept_Op + ' ' + @SDept
    select @sql = @sql + ' and t1.Class ' + @Class_Op + ' ' + @Class

    if @Over_POS > 0
        select @sql = @sql + ' and t1.retail > ' + cast(@Over_POS as char(3)) + ' '
        select @sql = @sql + ' order by LYWeek1Dollars desc '

    EXEC sp_executesql @sql

    select @sql = 'update ReportData.dbo.rpt_Holiday_Top_Selling_Report_2NC '
    select @sql = @sql + 'Set   LY_Title = Title, '
    select @sql = @sql + 'LY_ISBN = ISBN, '
    select @sql = @sql + 'NNTC = (Proj + (TY_SLSU * 3)) - (ReportData.dbo.rpt_Holiday_Top_Selling_Report_2NC.onHand + OnOrder), '
    select @sql = @sql + 'LY_Author = Author, '
    select @sql = @sql + 'LY_Dept_Num = Dept_Num, '
    select @sql = @sql + 'LY_SDept_Num = SDept_Num, '
    select @sql = @sql + 'LY_Class_Num = Class_Num, '
    select @sql = @sql + 'LY_OnHand = Staging.dbo.tmp_Top10_2NC.OnHand, '
    select @sql = @sql + 'LY_Class = Class, '
    select @sql = @sql + 'LY_SLS$ = SLS$, '
    select @sql = @sql + 'LY_SLSU = SLSU '
    select @sql = @sql + 'from  Staging.dbo.tmp_Top10_2NC'
    select @sql = @sql + 'where Staging.dbo.tmp_Top10_2NC.LYRank = Rank '
    select @sql = @sql + 'and Section = ' + '''' + @Section + ''''


    EXEC sp_executesql @sql

    truncate table Staging.dbo.tmp_Top10_NEW

    fetch next from cur into @ReportOrder, @numrows, @Section, @Dept_Op,      @Dept, @SDept_Op, @SDept, @Class_Op, @Class, @Over_POS
   end 

   close cur
   deallocate cur

1 个答案:

答案 0 :(得分:3)

结尾处缺少空格
  

选择@sql ='插入Staging.dbo.tmp_Top10_2NC'

select @sql = 'insert into Staging.dbo.tmp_Top10_2NC '
select @sql = @sql + 'select top ' + cast(@numrows as char(3)) + ' '