Search Inside Table Type of Records

柔情痞子 提交于 2019-12-31 06:18:43

问题


I have the below types

CREATE OR REPLACE TYPE "CLONE_PRODUCT_CHAR_RECORD" IS OBJECT  (
  CharacteristicID              NUMBER,
  NewValue              VARCHAR2(200 Char),
  NewValueName      VARCHAR2(200 Char)
);


 CREATE OR REPLACE TYPE "CLONE_PRODUCT_CHAR_TABLE" IS 
  TABLE OF CLONE_PRODUCT_CHAR_RECORD;

and the below Procedure

DECLARE 

characteristic CLONE_PRODUCT_CHAR_TABLE:=CLONE_PRODUCT_CHAR_TABLE(

      CLONE_PRODUCT_CHAR_RECORD(2,'ZIKA','ZIKA'),
      CLONE_PRODUCT_CHAR_RECORD(3,'MIGO','MIGO'),
      CLONE_PRODUCT_CHAR_RECORD(4,'ZAG','ZAG')

);

char_record CLONE_PRODUCT_CHAR_RECORD;

BEGIN


END;

and i need to select from characteristic record with CharacteristicID = 3 into the char_record variable


回答1:


declare 
characteristic CLONE_PRODUCT_CHAR_TABLE:=CLONE_PRODUCT_CHAR_TABLE(
      CLONE_PRODUCT_CHAR_RECORD(2,'ZIKA','ZIKA'),
      CLONE_PRODUCT_CHAR_RECORD(3,'MIGO','MIGO'),
      CLONE_PRODUCT_CHAR_RECORD(4,'ZAG','ZAG')
);
char_record CLONE_PRODUCT_CHAR_RECORD;
BEGIN
  for i in 1 .. characteristic.count loop
      if characteristic(i).characteristicID = 3 then
         char_record := characteristic(i);
         exit;
      end if;
  end loop;
  dbms_output.put_line(char_record.newValue);
  dbms_output.put_line(char_record.newValueName);
END;
/

Alternatively, you might want to try

declare 
characteristic CLONE_PRODUCT_CHAR_TABLE:=CLONE_PRODUCT_CHAR_TABLE(
      CLONE_PRODUCT_CHAR_RECORD(2,'ZIKA','ZIKA'),
      CLONE_PRODUCT_CHAR_RECORD(3,'MIGO','MIGO'),
      CLONE_PRODUCT_CHAR_RECORD(4,'ZAG','ZAG')
);
char_record CLONE_PRODUCT_CHAR_RECORD;
BEGIN

  select CLONE_PRODUCT_CHAR_RECORD(characteristicID, newvalue, newvaluename)
    into char_record from
   table(characteristic)
   where
      characteristicID = 3;

  dbms_output.put_line(char_record.newValue);
  dbms_output.put_line(char_record.newValueName);
END;
/


来源:https://stackoverflow.com/questions/24584646/search-inside-table-type-of-records

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!