Jump to content
Sign in to follow this  
Henry Olive

SQL Update

Recommended Posts

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

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

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
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
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×