Subject Re: [firebird-support] S.Proc
Author Alexandre Benson Smith
Em 10/9/2013 14:14, Revcor Stump escreveu:
Good Day,

Could someone please correct below s.proc. syntax.
Thank You

CREATE PROCEDURE BOMCOST (
  "ITEMNO" VARCHAR(20) )
RETURNS
(  "CHILDITEMNO"  VARCHAR(20),
  "QTY" NUMERIC(18, 2),
  "LASTPRICE" NUMERIC(18, 4),
  "AVGCOST" NUMERIC(18, 4))
AS
BEGIN
/*First i'd like to collect LastPurchasePrice & AverageCost of the items
/*from ITEMSMOVE View.
 FOR
SELECT SUM(NETPRICE * QTY)/SUM(QTY) FROM ITEMSMOVE
INTO : AVGCOST

/*Then i'd like get the last purchase price from the upper same View*/
SELECT UPRICE
  FROM ITEMSMOVE
 WHERE (ITEMNO=:"ITEMNO")  and
 TDATETIME=(SELECT MAX(TDATETIME) FROM ITEMSMOVE
 WHERE TNAME IN ('B','M', 'T' ))
  INTO : LASTPRICE

 Now i'd like to combain above LASTPRICE & AVGCOST with
 below ITEMS (Parts)
 
 SELECT B.CHILDITEMNO, B.QTY
  FROM BOM B
  WHERE B.ITEMNO= : ITEMNO
  INTO : CHILDITEMNO,: QTY
  DO
  SUSPEND;
END

The result should be like below
CHILDITEMNO...QTY....AVGCOST.....LASTPRICE
AA...........................2............1200...............1220
BB...........................4...............600................630
.......


If I got it right you should move the section where you get the LastPrice and AvgCost to the loop. Something like:

CREATE PROCEDURE BOMCOST (
  "ITEMNO" VARCHAR(20) )
RETURNS
(  "CHILDITEMNO"  VARCHAR(20),
  "QTY" NUMERIC(18, 2),
  "LASTPRICE" NUMERIC(18, 4),
  "AVGCOST" NUMERIC(18, 4))
AS
BEGIN
  for
     SELECT B.CHILDITEMNO, B.QTY
     FROM BOM B
     WHERE B.ITEMNO= : ITEMNO
     INTO :CHILDITEMNO, :QTY
  DO
  begin

   /*First i'd like to collect LastPurchasePrice & AverageCost of the items
   /*from ITEMSMOVE View.
   SELECT SUM(NETPRICE * QTY)/SUM(QTY) FROM ITEMSMOVE
/* I think you missed this where) */
   WHERE ITEMNO = :CHILDITEMNO
   INTO : AVGCOST;

   /*Then i'd like get the last purchase price from the upper same View*/
   SELECT UPRICE
     FROM ITEMSMOVE
     WHERE (ITEMNO=:ITEMNO)  and
     TDATETIME=(SELECT MAX(TDATETIME) FROM ITEMSMOVE
        WHERE TNAME IN ('B','M', 'T' ))
        INTO :LASTPRICE;

     SUSPEND;
   end
END