Results 1 to 8 of 8

Thread: Check a parameter value in SRPOC

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2004
    Location
    in the heart of the Mediterranean
    Posts
    1,143

    Check a parameter value in SRPOC

    Well,

    I am going through one of my SRPOCs and I have some doubt about one of its values. It goes like this :

    Code:
    Update tb_book
    
    SET stockbalance = stockbalance-@qty
    LimitAlert= Case
    
    When stockBalance-@qty<re_orderLimit
    
    THEN 1
    ELSE 0
    END
    Now my question is:

    Is @qty being deleted twice , in the Set... and in the When.... ?
    ------------------------------------------------------------------------
    If an answer to your question has been helpful, then please, Rate it!

  2. #2
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Check a parameter value in SRPOC

    I would think you would want this, so stockBalance is not decrement by qty twice

    Code:
    Update tb_book
    
    SET stockbalance = stockbalance-@qty
    LimitAlert= Case
    
    When stockBalance < re_orderLimit
    
    THEN 1
    ELSE 0
    END
    That is the very essence of human beings and our very unique capability to perform complex reasoning and actually use our perception to further our understanding of things. We like to solve problems. -Kleinma

    Does your code in post #46 look like my code in #45? No, it doesn't. Therefore, wrong is how it looks. - jmcilhinney

  3. #3
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Check a parameter value in SRPOC

    First - you are missing a comma - right. That syntax won't fly...

    But a field's value is "original" value everywhere you refer to it in a single UPDATE query. Just because it's one-line up in the query - that you are doing

    stockbalance = stockbalance-@qty

    The stockbalance is not reduced in the CASE statement - it's original value.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  4. #4
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Check a parameter value in SRPOC

    Here is the proof - your use is shown in this post

    Code:
    Set NoCount On
    
    Declare @Test Table (Value int, Alert int)
    
    Insert into @Test values (10,0)
    
    Declare @Qty int
    Set @Qty=5
    
    Update @Test Set Value=Value-@Qty, Alert=Case When Value<=0 Then 1 Else 0 End
    
    Select * From @Test
    
    Update @Test Set Value=Value-@Qty, Alert=Case When Value<=0 Then 1 Else 0 End
    
    Select * From @Test
    returns

    Code:
    Value       Alert       
    ----------- ----------- 
    5           0
    
    Value       Alert       
    ----------- ----------- 
    0           0

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  5. #5
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Check a parameter value in SRPOC

    And the proper way to do it

    Code:
    Set NoCount On
    
    Declare @Test Table (Value int, Alert int)
    
    Insert into @Test values (10,0)
    
    Declare @Qty int
    Set @Qty=5
    
    Update @Test Set Value=Value-@Qty, Alert=Case When Value-@Qty<=0 Then 1 Else 0 End
    
    Select * From @Test
    
    Update @Test Set Value=Value-@Qty, Alert=Case When Value-@Qty<=0 Then 1 Else 0 End
    
    Select * From @Test
    which returns

    Code:
    Value       Alert       
    ----------- ----------- 
    5           0
    
    Value       Alert       
    ----------- ----------- 
    0           1

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2004
    Location
    in the heart of the Mediterranean
    Posts
    1,143

    Re: Check a parameter value in SRPOC

    ok Sslamany,

    So apart from the comma which is my typo, the SPROC is okay and it is deducting only once. More convinced now.

    thanks.
    ------------------------------------------------------------------------
    If an answer to your question has been helpful, then please, Rate it!

  7. #7
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Check a parameter value in SRPOC

    Yes

    and Yes was to short a reply for the forum so I am forced to add this

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2004
    Location
    in the heart of the Mediterranean
    Posts
    1,143

    Re: Check a parameter value in SRPOC

    Thanks
    ------------------------------------------------------------------------
    If an answer to your question has been helpful, then please, Rate it!

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