Results 1 to 3 of 3

Thread: [sql2005] update using case statement

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    617

    [sql2005] update using case statement

    Hi there, need extra head once more.

    Im updating the dlvrystatus of purchase item against certain condition. The value will be determine through CASE statement. Note that the 2nd and 3rd condition work.

    In the code I want to return PENDING for purchase item without deliveries(tblmaterialreceiptdetails).

    Code:
    update tbllpodetails  set dlvrystatus=(
    	case	
    		--assign Pending for po without deliveries
    		when  is null(select sum(b.qty)
    			from	tblmaterialreceiptdetails b
    			where b.fk_lpodetailid=tbllpodetails.pk_lpodetailid)
    			then 'Pending'
    
    	 --assign Received for lpo without deliveries
    		when tbllpodetails.qty=(select sum(b.qty)
    			from	tblmaterialreceiptdetails b
    			where b.fk_lpodetailid=tbllpodetails.pk_lpodetailid) 
    			then 'Received'
    	else 
    		'Partial'
    	end
    )
    Error:
    Msg 156, Level 15, State 1, Line 11
    Incorrect syntax near the keyword 'is'.
    Msg 156, Level 15, State 1, Line 14
    Incorrect syntax near the keyword 'then'.
    Msg 156, Level 15, State 1, Line 20
    Incorrect syntax near the keyword 'then'.

    Thanks.

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: [sql2005] update using case statement

    I'ts possible... just need to get the syntax correct.... based on what you have, see if this works:

    Code:
    UPDATE tbllpodetails 
    SET dlvrystatus =
            CASE
                  WHEN Q.totalQty IS NULL THEN 'Pending'
                  WHEN Q.totalQty = tbllpodetails.qty THEN 'Received'
                  ELSE 'Partial'
            END
    
    FROM tbllpodetails 
    LEFT JOIN (
                    select a.pk_lpodetailid, sum(b.qty) at otalQty
                    from	tbllpodetails a
                    inner join tblmaterialreceiptdetails b
                      on b.fk_lpodetailid=a.pk_lpodetailid
    ) Q
    On tbllpodetails.pk_lpodetailid = Q.pk_lpodetailid
    See if that fits what you need.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    617

    Re: [sql2005] update using case statement

    I received this error

    Column 'tbllpodetails.pk_lpodetailid' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

    and I add the grouping

    Code:
    LEFT JOIN (
                    select a.pk_lpodetailid, sum(b.qty) as totalQty
                    from	tbllpodetails a
                    inner join tblmaterialreceiptdetails b
                    on b.fk_lpodetailid=a.pk_lpodetailid
    				group by pk_lpodetailid
    ) Q
    But i did not get the correct result. The status Received flags lpo item with partial deliveries. Im close to it though.


    Thanks for dropping by.

Posting Permissions

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



Click Here to Expand Forum to Full Width