附加到Oracle用户定义的集合类型

时间:2018-04-10 11:18:19

标签: oracle plsql oracle11g oracle10g

我想保留对象引用变量上的所有插入行。但是找不到我能做到的事情。任何人都可以检查我的脚本并更正它。

OBJECT_ID   OBJECT_TYPE

11          COLLEGE

当我执行脚本时。它将最终输出显示为

OBJECT_ID   OBJECT_TYPE

10          STUDENT
11          COLLEGE

我需要输出

Employee newEmp = new Employee();
newEmp.fname = textBox1.Text;
newEmp.sname = textBox2.Text;
newEmp.id = textBox3.Text;
newEmp.sal = textBox4.Text;

我如何保留所有插入的记录

2 个答案:

答案 0 :(得分:2)

您可以在PL / SQL中完成所有操作,并且不需要上下文切换到SQL范围:

DECLARE
   -- Initialise the collection
   v_OBJECT_TYPE_REL_IDS T_OBJECT_TYPE_REL_IDS := T_OBJECT_TYPE_REL_IDS();
BEGIN
   -- Extend the collection by the default amount (1 element)
   v_OBJECT_TYPE_REL_IDS.EXTEND;
   -- Set the first element to be your student type
   v_OBJECT_TYPE_REL_IDS(1) := GT_OBJECT_TYPE_REL_IDS (10, 'STUDENT');

   -- Extend the collection by 1 element
   v_OBJECT_TYPE_REL_IDS.EXTEND(1);
   -- Set the last element to be your college type
   v_OBJECT_TYPE_REL_IDS(v_OBJECT_TYPE_REL_IDS.COUNT) := GT_OBJECT_TYPE_REL_IDS (11, 'COLLEGE');

   OPEN :curs FOR SELECT * FROM TABLE (v_OBJECT_TYPE_REL_IDS);
END;
/

您也可以在构造函数中执行此操作:

DECLARE
   -- Initialise the collection
   v_OBJECT_TYPE_REL_IDS T_OBJECT_TYPE_REL_IDS := T_OBJECT_TYPE_REL_IDS(
       GT_OBJECT_TYPE_REL_IDS (10, 'STUDENT'),
       GT_OBJECT_TYPE_REL_IDS (11, 'COLLEGE')
     );
BEGIN
   OPEN :curs FOR SELECT * FROM TABLE (v_OBJECT_TYPE_REL_IDS);
END;
/

或者,如果您想使用SQL,那么如果使用UNION ALL直接填充游标行,则不需要集合类型:

BEGIN
   OPEN :curs FOR
     SELECT GT_OBJECT_TYPE_REL_IDS (10, 'STUDENT') FROM DUAL UNION ALL
     SELECT GT_OBJECT_TYPE_REL_IDS (11, 'COLLEGE') FROM DUAL;
END;
/

或者,使用集合:

BEGIN
   OPEN :curs FOR
     SELECT *
     FROM TABLE(
       T_OBJECT_TYPE_REL_IDS(
         GT_OBJECT_TYPE_REL_IDS (10, 'STUDENT'),
         GT_OBJECT_TYPE_REL_IDS (11, 'COLLEGE')
       )
     );
END;
/

然后,使用前两个版本中的任何一个,您可以完全避免使用游标和PL / SQL。即:

SELECT *
FROM TABLE(
  T_OBJECT_TYPE_REL_IDS(
    GT_OBJECT_TYPE_REL_IDS (10, 'STUDENT'),
    GT_OBJECT_TYPE_REL_IDS (11, 'COLLEGE')
  )
);

或者merge two collections

DECLARE
  v_OBJECT_TYPE_REL_IDS T_OBJECT_TYPE_REL_IDS;
BEGIN
  --Explicit Constructor
  v_OBJECT_TYPE_REL_IDS := T_OBJECT_TYPE_REL_IDS( GT_OBJECT_TYPE_REL_IDS (10, 'STUDENT') );

  -- Merge in SQL using MUTLISET UNION ALL
  SELECT v_OBJECT_TYPE_REL_IDS
         MULTISET UNION ALL
         T_OBJECT_TYPE_REL_IDS( GT_OBJECT_TYPE_REL_IDS (11, 'COLLEGE') )
  INTO v_OBJECT_TYPE_REL_IDS
  FROM   DUAL;

  OPEN :curs FOR SELECT * FROM TABLE (v_OBJECT_TYPE_REL_IDS);
END;
/

答案 1 :(得分:1)

你用第二行覆盖第一行。使用select UNION ALLSELECT * BULK COLLECT INTO v_OBJECT_TYPE_REL_IDS FROM ( SELECT GT_OBJECT_TYPE_REL_IDS(10, 'STUDENT') FROM DUAL UNION ALL SELECT GT_OBJECT_TYPE_REL_IDS(11, 'COLLEGE') FROM DUAL ); 一起加载。

PIPELINED FUNCTION

此外,如果您将其转换为CURSOR,则不需要SELECT * FROM TABLE(fn_pipelinedfunc);变量。您只需运行For i = 1 to N IF numberOfRunningThreads < k // run foo() on another thread ELSE // run foo()

即可