gpt4 book ai didi

postgresql - 使用 %ROWTYPE 在 postgres 函数中循环数据时出现问题

转载 作者:行者123 更新时间:2023-11-29 12:31:53 31 4
gpt4 key购买 nike

我正在尝试在 postgres 中创建一个函数,该函数从一个表中检索数据并将其输入到另一个表中。我正在使用 %ROWTYPE 类型来存储来自 select 语句的临时数据,然后使用插入语句遍历它,但没有成功!以下是我的代码:

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;

好吧,现在我只是想通过使用计数器来计算循环次数并返回该数字来查看函数是否正在循环,但到目前为止它还没有返回任何东西。我无法在网上或任何地方找到有关此类程序的太多信息,如果有人可以帮助或指导我朝着正确的方向前进,我将不胜感激!

谢谢

最佳答案

成功了...

如果其他人遇到同样的问题,这是供将来引用的代码!

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;

关于postgresql - 使用 %ROWTYPE 在 postgres 函数中循环数据时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/385985/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com