views:

603

answers:

1

Hi all...

I am trying to create a function in postgres that retrieves data from one table and inputs into another. I am using the %ROWTYPE type to store the temporary data from the select statement and then iterate through it with an insert statement but have been unsuccessful! The following is my code :

CREATE OR REPLACE FUNCTION rm_stock_take_add (icompany character varying, idate character varying) 
  RETURNS character varying AS

$BODY$

DECLARE

    loc_result    CHAR(50);

    -- Declaring a counter to increment for the index
    counter INTEGER;

    -- Declare a variable to hold rows from the stock table.
    row_data rm_stock%ROWTYPE;


BEGIN

     -- Iterate through the results of a query.
    FOR row_data IN 
    SELECT *
    FROM rm_stock 
    --WHERE company = icompany 
    ORDER BY company, rm_sto_code, rm_col_code  

        LOOP

      counter := counter + 1;
/*       
          INSERT INTO rm_stock_take
          ( 
           "stock_ind", "company", "rm_stock_code", "rm_col_code", "rm_dye_lot_num", "rm_take_date", "rm_quantity_theo"
          )
          VALUES 
          ( 
           counter, icompany, row_data.rm_sto_code, row_data.rm_col_code, row_data.rm_dye_lot_num, idate,
               row_data.rm_sto_on_hand_excl
          );

*/   
       END LOOP;



   RETURN counter :: character varying;

END;
$BODY$
 LANGUAGE 'plpgsql'VOLATILE;
ALTER FUNCTION rm_stock_take_add(icompany character varying, idate character varying) OWNER TO postgres;

Okay, so at the moment i am just trying to see if the function is looping by using the counter to count the amount of loops and return that number, but it hasn't returned anything as of yet. I haven't been able to find much information online or anywhere regarding this sort of procedure, and if anyone can help or guide me into the right direction it will be greatly appreciated!

Thanks

+1  A: 

Got it working...

here is the code for future reference if anyone else bumps into the same problem!

CREATE OR REPLACE FUNCTION rm_stock_take_add(icompany character varying, idate character varying)
  RETURNS character varying AS
$BODY$


DECLARE
    loc_result CHAR(50);
    counter INTEGER = 1;
    row_data RECORD;

BEGIN
    FOR row_data IN SELECT rm_sto_code, rm_col_code, rm_dye_lot_num, rm_sto_on_hand_excl
    FROM rm_stock 
    WHERE company = icompany 
    ORDER BY company, rm_sto_code, rm_col_code  

    LOOP         
          INSERT INTO rm_stock_take
          ( 
               "stock_ind", "company", "rm_stock_code", "rm_col_code", "rm_dye_lot_num", "rm_take_date", "rm_quantity_theo"
          )
          VALUES 
          ( 
               counter, icompany, row_data.rm_sto_code, row_data.rm_col_code, row_data.rm_dye_lot_num, idate,
               row_data.rm_sto_on_hand_excl
          );   
          counter := counter + 1;

    END LOOP;

    loc_result := 'success';  
    RETURN loc_result ;
END;
$BODY$
  LANGUAGE 'plpgsql' VOLATILE;
ALTER FUNCTION rm_stock_take_add(icompany character varying, idate character varying) OWNER TO postgres;