收集多行并在PL / SQL中存储值

时间:2018-04-10 14:31:34

标签: oracle plsql collections

我有以下查询:

DECLARE
  type INV_TASK_GLOBAL is varray(10) of NUMBER(20,0);
  type STUDY_CASE_GLOBAL is varray(10) of NUMBER(20,0);

BEGIN
  SELECT T_INVESTIGATIONTASK.ID, T_STUDYCASE.ID 
     into INV_TASK_GLOBAL, STUDY_CASE_GLOBAL 

  FROM T_VALIDATIONCARRIER 
      left join T_ESTIMATIONOBJECT on T_VALIDATIONCARRIER.IA_ESTIMATIONOBJECT_ID = T_ESTIMATIONOBJECT.ID
      left join T_INVESTIGATIONTASK on T_ESTIMATIONOBJECT.INVESTIGATIONTASK_ID = T_INVESTIGATIONTASK.ID
      left join T_STUDYCASE on T_INVESTIGATIONTASK.STUDYCASE_ID = T_STUDYCASE.ID

  WHERE T_VALIDATIONCARRIER.ESTIMATIONOBJECT_ID = 940;

  dbms_output.Put_line('INVESTIGATIONTASK_ID: ' || INV_TASK_GLOBAL);
  dbms_output.Put_line('STUDYCASE_ID: ' || STUDY_CASE_GLOBAL);
END;

编译器告诉我,精确提取中指定的数字小于返回的行数。事实是我希望返回这些行。具体来说:我想收集所有T_INVESTIGATIONTASK.IDT_STUDYCASE.IDWHERE子句捕获的每一行一个),将它们存储到INV_TASK_GLOBAL和在STUDY_CASE_GLOBAL中,然后显示返回的所有值(可能用逗号分隔) 我可能会在将来更改WHERE条件,但无论如何,我希望为这两个变量返回的最大元素数为10。
我知道我以错误的方式使用varray数据类型:我需要某种循环和列表/数组数据类型来存储INV_TASK_GLOBALSTUDY_CASE_GLOBAL变量中的所有返回值,然后打印阵列在屏幕上。你知道如何完成它吗?

1 个答案:

答案 0 :(得分:0)

经过一些测试后,以下代码解决了我的问题:

DECLARE
  type collection_id is table of NUMBER(20,0);

  INV_TASK_GLOBAL collection_id := collection_id(10);
  STUDY_CASE_GLOBAL collection_id := collection_id(10);

BEGIN
  SELECT T_INVESTIGATIONTASK.ID, T_STUDYCASE.ID 
    BULK COLLECT into INV_TASK_GLOBAL, STUDY_CASE_GLOBAL 
  FROM T_VALIDATIONCARRIER 
     left join T_ESTIMATIONOBJECT on T_VALIDATIONCARRIER.IA_ESTIMATIONOBJECT_ID = T_ESTIMATIONOBJECT.ID
     left join T_INVESTIGATIONTASK on T_ESTIMATIONOBJECT.INVESTIGATIONTASK_ID = T_INVESTIGATIONTASK.ID
     left join T_STUDYCASE on T_INVESTIGATIONTASK.STUDYCASE_ID = T_STUDYCASE.ID
  WHERE T_VALIDATIONCARRIER.ESTIMATIONOBJECT_ID = 940;

  FOR indx IN 1 .. INV_TASK_GLOBAL.COUNT
  LOOP
     dbms_output.Put_line('INVESTIGATIONTASK_ID: ' || INV_TASK_GLOBAL(indx));
  END LOOP;

  FOR indx IN 1 .. STUDY_CASE_GLOBAL.COUNT
  LOOP
      dbms_output.Put_line('STUDY_CASE_ID: ' || STUDY_CASE_GLOBAL(indx));
  END LOOP;
END;