如何使用隐式光标在PLSQL中使用插入查询进行循环
我有一个现有的表,我想在给定第一张表的每一行中的另一个表中添加一些条目。 我正在写我的plsql命令,为:
BEGIN
FOR record in (select cola_guid, hapc_guid, tar_guid from tabA) LOOP
select count(*) INTO v_record_exists
from p where
p.cola_guid = record.cola_guid;
IF v_record_exists = 0 THEN
execute immediate 'insert into NTABLE (tar_guid, PC_NAE, PCV) values (record.tar_guid, ' || '''abcd''' || ', ' || '''val1''' || ')';
ELSE
execute immediate 'insert into NTABLE (tar_guid, PC_NAE, PCV) values (record.tar_guid, ' || '''abcd''' || ', ' || '''val2''' || ')';
END IF;
execute immediate 'insert into NTABLE (tar_guid, PC_NAE, PCV) values (record.tar_guid, ' || '''RA_hapc_guid''' || ', record.hapc_guid)';
execute immediate 'insert into NTABLE (tar_guid, PC_NAE, PCV) select record.tar_guid, PC_NAE, PCV from p where record.cola_guid = p.cola_guid and PC_NAE = ' || '''propVal''' || ' ';
END LOOP;
END;
现在我遇到了错误:
ORA-00984:列在这里不允许
列:
立即执行'插入ntable(tar_guid,pc_nae,pcv)值(record.tar_guid,'||'''''''''''''''''||'||',||''''''''''''''''''''''''''''''''''''''''''''''''''''''')) ';
我是PLSQL World的新手,但我确实尝试了分类和谷歌搜索,但无法解决。请指导并提供帮助。
I have an existing table and I want to add some entry in another table for each row of the given first table.
I am writing my PLSQL command as:
BEGIN
FOR record in (select cola_guid, hapc_guid, tar_guid from tabA) LOOP
select count(*) INTO v_record_exists
from p where
p.cola_guid = record.cola_guid;
IF v_record_exists = 0 THEN
execute immediate 'insert into NTABLE (tar_guid, PC_NAE, PCV) values (record.tar_guid, ' || '''abcd''' || ', ' || '''val1''' || ')';
ELSE
execute immediate 'insert into NTABLE (tar_guid, PC_NAE, PCV) values (record.tar_guid, ' || '''abcd''' || ', ' || '''val2''' || ')';
END IF;
execute immediate 'insert into NTABLE (tar_guid, PC_NAE, PCV) values (record.tar_guid, ' || '''RA_hapc_guid''' || ', record.hapc_guid)';
execute immediate 'insert into NTABLE (tar_guid, PC_NAE, PCV) select record.tar_guid, PC_NAE, PCV from p where record.cola_guid = p.cola_guid and PC_NAE = ' || '''propVal''' || ' ';
END LOOP;
END;
Now I am getting error:
ORA-00984: column not allowed here
in line:
execute immediate 'insert into NTABLE (tar_guid, PC_NAE, PCV) values (record.tar_guid, ' || '''abcd''' || ', ' || '''val1''' || ')';
I am new to PLSQL world but I really tried triaging and googling but wasn't able to resolve. Please guide and help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无需在此处使用动态SQL-您知道要插入/选择的所有列和表,因此您可以直接在SQL语句中直接使用PL/SQL变量。
另外,当您在PL/SQL中编写SQL时,出于性能原因(以及易于阅读,维护和调试),您应该认为基于设定。
完全有可能在单个插入语句中完成所有插入,您可以将其放入过程中。
别忘了您需要根据需要提交/回滚!
There is no need for you to use dynamic sql here - you know all the columns and tables you're inserting/selecting from, so you can simply use the PL/SQL variables directly in the SQL statement.
Also, when you're writing SQL inside PL/SQL, for performance reasons (as well as easy to read, maintain and debug) you should think set based.
It's entirely possible to do all your inserts in a single insert statement, which you can put inside a procedure.
Don't forget you'll need to commit/rollback as required!