dynamic sql stored proc

  • hello,

    can anyone pls help me build the sp for this problem. I need to update the end_date column with the next start date from the next row of data. Im attaching  sample data to make it easier to understand

    Before Update

    idsvc_idstagestart_dateend_date
    1215/10/2004null
    2225/11/2004null
    3235/12/2004null
    4245/13/2004null
    5315/14/2004null
    6325/15/2004null
    7335/16/2004null
    8415/17/2004null
    9425/18/2004null
    10515/19/2004null
    11525/20/2004null

     

    data should look like this after update procedure

    idsvc_idstagestart_dateend_date
    1215/10/20045/11/2004
    2225/11/20045/12/2004
    3235/12/20045/13/2004
    4245/13/2004null
    5315/14/20045/15/2004
    6325/15/20045/16/2004
    7335/16/2004null
    8415/17/20045/18/2004
    9425/18/2004null
    10515/19/20045/20/2004
    11525/20/2004null

     

    id column is incremental

    Thanks

  • Well, if the stage field is always in sequence then this should do it:

    UPDATE  T1
    SET    T1.end_date = T2.start_date
    FROM  Table1 T1
    LEFT OUTER JOIN Table1 T2
    ON    T1.svc_id = T2.svc_id
    AND   T1.stage + 1 = T2.stage


    ----------------------------------------
    Pascal Dobrautz
    www.sqlassi.net

  • You can try (this works, if stage is not in sequence)

     

    update tempdata  set end_date=(select start_date from tempdata t2

    where t2.svc_id=tempdata.svc_id and t2.stage=(select min(stage) from tempdata t3 where t3.stage > tempdata.stage)

    )

     

    --- if stage is in sequence, you can try:

    update tempdata  set end_date=(select start_date from tempdata t2

    where t2.svc_id=tempdata.svc_id and t2.stage=tempdata.stage+1)

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

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