暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Oracle 动态FORALL中作为绑定变量的PL SQL类型

askTom 2018-03-13
132

问题描述

SQL版本:
Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production 0
PL/SQL Release 12.1.0.2.0 - Production 0
CORE 12.1.0.2.0 Production 0
TNS for Linux: Version 12.1.0.2.0 - Production 0
NLSRTL Version 12.1.0.2.0 - Production 0
复制


查询:

我可以使用PL sql类型作为绑定变量在FORALL中立即执行吗?请看下面的代码 ..让我知道是否可行,如果不可行,为什么?

CREATE OR REPLACE PROCEDURE sp_test
AS

   TYPE typ_abc IS TABLE OF abc%rowtype INDEX BY BINARY_INTEGER;
   v_typ_abc                   typ_abc;
BEGIN
   --Code
   
         FORALL i IN v_typ_abc.FIRST .. v_typ_abc.LAST  SAVE EXCEPTIONS
            EXECUTE IMMEDIATE 'INSERT INTO ' || p_table_owner || '.' || p_table_name || ' /*+ PARALLEL 1 */ PARTITION ( ' || p_partition_name || ' ) '
                                             || ' ( ' || v_column_names_list ||' ) VALUES :1' USING v_typ_abc(i);
                                             
   --Code                                            
END;
/
复制


在编译相同的我得到一个PLS-00457的错误。

专家解答

12c取消了可以传递给SQL的PL/SQL数据类型的许多限制。前提是您在包规范中声明类型。

可悲的是,您使用的是仅PL/SQL的 % rowtype。而这仍然是限制之一:

If the PL/SQL-only data type is an associative array, it cannot be used within a non-query DML statement (INSERT, UPDATE, DELETE, MERGE) nor in a subquery.

https://docs.oracle.com/database/121/LNPLS/release_changes.htm#GUID-57E439FB-B196-46CB-857C-0ADAB32D9EA0

你可以通过分别绑定每个属性来解决这个问题:

create table t (
  x int
);

create or replace procedure p  as
  type tp is table of t%rowtype index by binary_integer;
  recs tp;
begin
  recs(1).x := 1;
  recs(2).x := 2;
  
  forall i in 1 .. recs.count 
    execute immediate 'insert into t values (:v)' using recs(i).x;
end p;
/

exec p();
select * from t;

X   
  1 
  2
复制


或者,更好的是,使用静态SQL!

create or replace procedure p  as
  type tp is table of t%rowtype index by binary_integer;
  recs tp;
begin
  recs(1).x := 1;
  recs(2).x := 2;
  
  forall i in 1 .. recs.count 
    insert into t values recs(i);
end p;
/
sho err

exec p();
select * from t;

X   
  1 
  2 
  1 
  2 
复制

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论