使用varray变量运行插入查询时遇到问题
时间:2020-03-06 14:33:32 来源:igfitidea点击:
我在Oracle 10g企业上使用SQL * Plus 9.2. 我创建了一些脚本,这些脚本使用我通过命令提示符传递的参数进行基本插入。我应该能够在循环中运行一堆插入,这似乎合乎逻辑。所以我尝试了以下方法:
--begin DECLARE TYPE va_orgs IS TABLE OF nbr.lien_item.lien_item_name%type; org va_orgs := va_orgs('RTA','RTB','RTE','RTI','RTM','RTT'); BEGIN FOR i in org.FIRST .. org.LAST LOOP INSERT INTO nbr.lien_item (lien_item_sid, excel_row, include_in_calcs, indent, header_level, sort_order, unit, lien_item_status, lien_item_name) VALUES (nbr.lien_item_seq.nextval, 0, 'Y', 1, 0, 1, 'FTE', 'A', 'org(i)'); COMMIT; END LOOP; END; / --end
运行脚本时,我收到一条消息,说明PL / SQL已成功完成。我尝试调试并使用dbms_output
来显示org(i)
的值。一切看起来都很好。但是这些行永远不会输入到数据库中。一旦选择,新行就不存在了。关于循环和执行插入有一些技巧吗?
(我也尝试过使用" IS VARRAY(6)OF"代替" IS TABLE OF"。同样没有结果)
解决方案
在插入语句中,单引号中包含org(i)。我们不应该这样,我们可能会在表中插入org(i)一词作为值。所以你的插入语句应该是
INSERT INTO nbr.lien_item (lien_item_sid, excel_row, include_in_calcs, indent, header_level, sort_order, unit, lien_item_status, lien_item_name) VALUES (nbr.lien_item_seq.nextval, 0, 'Y', 1, 0, 1, 'FTE', 'A', org(i));