T-SQL problem with cursor syntex

  • I hoping for some help on a the below code.

    I am creating a running total(by day within a month) from a table I have created. But I am having a problem with the statement after the if @monthend = 'Y'. The line " @sumtotal = @total". Looks simple enough but this is the second time I have done cursors sql so any help I can get I will appricate your input. The error is easy enough but I just dont see it. "Line 20: Incorrect syntax near '@sumtotal'." If anyone has a better way I am willing to try it.

    The table is defined as follows

    [OrderOf] [char] (2)

    [TermDate] [datetime] NOT NULL

    [SUMTotal] [numeric](18, 0) NULL

    [MonthEndInd] [char] (1) NULL

    -- host variables

    DECLARE @termdt datetime, @total numeric(18,0),@monthend char(1), @sumtotal numeric(18,0), @savetermdt datetime

    -- declare cursor

    declare c_tempdt cursor local

    forward_only

    for

    select TermDate, SUMtotal, MonthEndInd from CompletedDisconnects_TermDate_CountTEST order by termdate

    -- open the cursor

    open c_tempdt

    -- get the data

    fetch next from c_tempdt

    into @termdt, @total, @monthend

    -- initialize the varibles

    set @saveTermdt=' '

    set @sumtotal=0

    -- start the loop

    while @@fetch_status = 0

    begin

    begin

    if @monthend = 'Y'

    @sumtotal = @total <--error line

    else

    @sumtotal = @sumtotal @total

    end

    update CompletedDisconnects_TermDate_CountTEST set SUMTotal = @sumtotal where termdate = @termdt

    fetch next from c_tempdt

    into @termdt, @total, @monthend

    end

    -- close the cursor

    close c_tempdt

    -- dump the cursor

    deallocate c_tempdt

    Edited by - ghughes on 09/05/2003 11:09:14 AM

  • OK it was stupid

    fixing the lines

    begin

    if @monthend = 'Y'

    set @sumtotal = @total

    else

    set @sumtotal = @sumtotal + @total

    update CompletedDisconnects_TermDate_CountTEST set SUMTotal = @sumtotal where termdate = @termdt

    end

    Alittle thing like SET can SET you whole day off!

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply