Home Forums SQL Server 2008 T-SQL (SS2K8) How to Insert/Upadte data based on exist in mathcin table, in single query RE: How to Insert/Upadte data based on exist in mathcin table, in single query

  • Please Find the individual statements

    --Update Statement

    UPDATE a

    set Points=

    case when (a.lastGamedate is null and b.fee>0) then b.fee

    when (a.lastGamedate is not null and dateadd(DAY,49,lastgamedate) >= getdate() and a.Points < b.Fee ) then b.Fee

    end,

    LastGameDate = case when (a.lastGamedate is null and b.fee>0) then DATEADD (D, -1 * DatePart (WEEKDAY , GETDATE()) + 7, getdate()) end

    from StudentNew a inner join StudentOffers b on a.Sno =b.Sno and a.Sname =b.Sname

    ---Insert Statement

    Insert into StudentNew

    Select b.Sno ,b.Sname , case when (b.Fee>0) then DATEADD (D, -1 * DatePart (WEEKDAY , GETDATE()) + 7, getdate()) else null end ,b.DoB , case when ( b.Fee >0) then b.Fee else null end

    from StudentNew a right join StudentOffers b on a.Sno =b.Sno and a.Sname =b.Sname

    where a.Sno is null and a.Sname is null

    this works but please assit me how can i make this into single statement as effective one

    also if you observe somehow i am not feeling these statements the way i wrote is not good looking (even though it is functioning well) i mean for example rather than going the whole table read eveytime can i include where clause in update statement so that it is efficient

    please kindly help me

    thanks in advance