如何使用隐式光标在PLSQL中使用插入查询进行循环

发布于 2025-02-10 17:22:41 字数 1371 浏览 3 评论 0原文

我有一个现有的表,我想在给定第一张表的每一行中的另一个表中添加一些条目。 我正在写我的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

丢了幸福的猪 2025-02-17 17:22:41

您无需在此处使用动态SQL-您知道要插入/选择的所有列和表,因此您可以直接在SQL语句中直接使用PL/SQL变量。

另外,当您在PL/SQL中编写SQL时,出于性能原因(以及易于阅读,维护和调试),您应该认为基于设定。

完全有可能在单个插入语句中完成所有插入,您可以将其放入过程中。

BEGIN
  INSERT INTO ntable (tar_guid, pc_nae, pcv)
  WITH results AS (SELECT t.cola_guid,
                          t.hapc_guid,
                          t.tar_guid,
                          CASE WHEN EXISTS (SELECT NULL FROM p WHERE p.cola_guid = t.cola_guid) THEN 'val1' ELSE 'val2' END val
                   FROM   taba t)
  SELECT tar_guid,
         'abcd' pc_nae,
         val pcv
  FROM   results
  UNION ALL
  SELECT tar_guid,
         'RA_hapc_guid' pc_nae
         hapc_guid pcv
  FROM   results
  UNION ALL
  SELECT p.tar_guid,
         p.pc_nae,
         p.pcv
  FROM   results r
         inner JOIN p ON r.cola_guid = p.cola_guid
  WHERE  p.pc_nae = 'propVal';
END;
/

别忘了您需要根据需要提交/回滚!

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.

BEGIN
  INSERT INTO ntable (tar_guid, pc_nae, pcv)
  WITH results AS (SELECT t.cola_guid,
                          t.hapc_guid,
                          t.tar_guid,
                          CASE WHEN EXISTS (SELECT NULL FROM p WHERE p.cola_guid = t.cola_guid) THEN 'val1' ELSE 'val2' END val
                   FROM   taba t)
  SELECT tar_guid,
         'abcd' pc_nae,
         val pcv
  FROM   results
  UNION ALL
  SELECT tar_guid,
         'RA_hapc_guid' pc_nae
         hapc_guid pcv
  FROM   results
  UNION ALL
  SELECT p.tar_guid,
         p.pc_nae,
         p.pcv
  FROM   results r
         inner JOIN p ON r.cola_guid = p.cola_guid
  WHERE  p.pc_nae = 'propVal';
END;
/

Don't forget you'll need to commit/rollback as required!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文