Henry Olive 5 Posted November 28, 2020 I wish everyone a healthy day i want to UPDATE a stock quantities with below S.Proc i dont get any err. msg. but this sql doesnt update correctly ALTER PROCEDURE "UPDATEITEMQTY" ( "ITMNO" VARCHAR(20)) AS declare variable TRS NUMERIC(18, 5); /* Qty Transfer from last year */ declare variable INCOME NUMERIC(18, 5); /* Stock IN */ declare variable OUTGO NUMERIC(18, 5); /* Stock OUT */ BEGIN SELECT SUM(IM.TRS)TRS, SUM(IM.INCOME)INCOME, SUM(IM.OUTGO)OUTGO FROM ITEMMOVEMENTS IM /* IM=a VIEW & every item movements are CORRECT */ JOIN WHOUSE WH ON WH.WHNO=IM.WHNO WHERE ITEMNO=:"ITMNO" and WH.WHTYPE <>'Rework' INTO :TRS,:INCOME,:OUTGO; UPDATE ITEMS SET /* IB2007 has Coalesce problem so i wrote below code */ TRS= Case When :TRS >0 then :TRS else 0 end, INCOME= Case When :INCOME >0 then :INCOME else 0 end, OUTGO= Case When :OUTGO >0 then :OUTGO else 0 end, STOCK= (:TRS + :INCOME) - :OUTGO WHERE ITEMNO=:ITMNO; END Can someone help me please ? Thank You Share this post Link to post
Vandrovnik 214 Posted November 28, 2020 May be instead of SELECT SUM(IM.TRS)TRS, SUM(IM.INCOME)INCOME, SUM(IM.OUTGO)OUTGO should be just SELECT SUM(IM.TRS), SUM(IM.INCOME), SUM(IM.OUTGO) ? The JOIN - I alway specify what kind of join I want, such as LEFT OUTER JOIN, because I am never sure, what is the default 🙂 Share this post Link to post
Attila Kovacs 629 Posted November 28, 2020 I don't know IB nor do I know the result of the IM JOIN WH but I don't like the "SUM(IM.TRS)TRS" I would write this like ISNULL( SUM( ISNULL(IM.TRS, 0) ), 0) AS TRS Share this post Link to post
Henry Olive 5 Posted November 28, 2020 Thank you so much Vandrovnik, Attila Share this post Link to post
Clément 148 Posted November 28, 2020 3 hours ago, Henry Olive said: I wish everyone a healthy day i want to UPDATE a stock quantities with below S.Proc i dont get any err. msg. but this sql doesnt update correctly ALTER PROCEDURE "UPDATEITEMQTY" ( "ITMNO" VARCHAR(20)) AS declare variable TRS NUMERIC(18, 5); /* Qty Transfer from last year */ declare variable INCOME NUMERIC(18, 5); /* Stock IN */ declare variable OUTGO NUMERIC(18, 5); /* Stock OUT */ BEGIN SELECT SUM(IM.TRS)TRS, SUM(IM.INCOME)INCOME, SUM(IM.OUTGO)OUTGO FROM ITEMMOVEMENTS IM /* IM=a VIEW & every item movements are CORRECT */ JOIN WHOUSE WH ON WH.WHNO=IM.WHNO WHERE ITEMNO=:"ITMNO" and WH.WHTYPE <>'Rework' INTO :TRS,:INCOME,:OUTGO; UPDATE ITEMS SET /* IB2007 has Coalesce problem so i wrote below code */ TRS= Case When :TRS >0 then :TRS else 0 end, INCOME= Case When :INCOME >0 then :INCOME else 0 end, OUTGO= Case When :OUTGO >0 then :OUTGO else 0 end, STOCK= (:TRS + :INCOME) - :OUTGO WHERE ITEMNO=:ITMNO; END Can someone help me please ? Thank You Hi, Since you already know the values of :TRS, :INCOME and :OUTGO you can check for nulls before entering your update. SELECT .... INTO :TRS,:INCOME,:OUTGO; IF TRS is NULL then TRS = 0; if INCOME IS NULL then INCOME = 0; if OUTGO IS NULL then OUTGO = 0; STOCK = TRS + INCOME - OUTGO; UPDATE ITEMS SET TRS= :TRS, INCOME = :INCOME, OUTGO= :OUTGO, STOCK= :STOCK WHERE ITEMNO=:ITMNO; END I haven't checked the syntax, but you get the idea. Share this post Link to post
Attila Kovacs 629 Posted November 28, 2020 4 minutes ago, Clément said: IF TRS is NULL then TRS = 0; if INCOME IS NULL then INCOME = 0; if OUTGO IS NULL then OUTGO = 0; STOCK = TRS + INCOME - OUTGO; UPDATE ITEMS SET TRS= :TRS, INCOME = :INCOME, OUTGO= :OUTGO, STOCK= :STOCK WHERE ITEMNO=:ITMNO; END Warning. This changes the SP fundamentally where negative numbers were ignored. Share this post Link to post