Results 1 to 5 of 5

Thread: Using Update in Then

  1. #1
    Join Date
    Nov 2005
    Posts
    1

    Using Update in Then

    Hello All

    I'm a new member of this group and desperately need the help of experts.

    I wish to execute an update statement in the THEN condition of the CASE in a select query i.e.

    select pp.productpurchaseid,pps.productpurchasesupportid, 'enddate' =
    CASE
    WHEN pps.enddate IS NULL THEN
    CASE
    WHEN DATEADD( month , 36, p.ReleaseDate) > DATEADD(month , 12, salesclosuredate)
    THEN (update productpurchasesupport set endate = DATEADD( month , 36, p.ReleaseDate)where pps.productpurchaseid = pp.productpurchaseid)
    ELSE (update productpurchasesupport set endate = DATEADD (month , 12, salesclosuredate)where pps.productpurchaseid = pp.productpurchaseid)
    END
    ELSE pps.enddate
    END
    from product p inner join productpurchase pp on ( p.productid = 1 and p.productId = pp.productId)
    inner join productpurchasesupport pps on (pp.productpurchaseId = pps.productpurchaseid )


    Pls help me if the update and select can be done like this in a single query or am i just goin wrong with some syntax.?????
    The query is working fine if there is no update statement.
    Thanks in advance

    Sameer
    Last edited by sameermast; 11-11-2005 at 02:08 AM.

  2. #2
    Join Date
    Sep 2002
    Posts
    5,938
    No, you can't run update in select statement.

  3. #3
    Join Date
    Aug 2005
    Posts
    6
    There is no need for a select statement if you are trying to update the table....or is that what you are trying to do?

    Create a Update query with all the Case statements and then do a select...if you wish to update the table

  4. #4
    Join Date
    Nov 2005
    Posts
    6
    --usage Is wrong
    --example:
    select
    A,
    B,
    C=case when c='a' then 1
    when c='b' then 2
    when c='c' then 3
    end
    from
    Table

  5. #5
    Join Date
    Feb 2003
    Posts
    1,048
    Code:
    Update pps
    Set pps.endate = IsNull(pps.enddate, Case 
    		When DATEADD(month, 36, p.ReleaseDate) > DATEADD(month, 12, salesclosuredate) Then DATEADD(month, 36, p.ReleaseDate)
    		Else DATEADD(month, 12, salesclosuredate)
    		End
    From product p 
    Inner Join productpurchase pp On p.productid = 1 and p.productId = pp.productId
    Inner Join productpurchasesupport pps On pp.productpurchaseId = pps.productpurchaseid
    
    Select pp.productpurchaseid, pps.productpurchasesupportid, pps.enddate
    From product p 
    Inner Join productpurchase pp On p.productid = 1 and p.productId = pp.productId
    Inner Join productpurchasesupport pps On pp.productpurchaseId = pps.productpurchaseid

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •