Usage of oracle custom type (type)

The emp table data is as follows

Define the object type

create or replace type typeof_userinfo_row as object(
  user_id varchar2(50),
  user_name varchar2(50)
)

Create a function with this type as the return value type

create or replace function FUN_TEST
return typeof_userinfo_row
is

  FunctionResult typeof_userinfo_row;

begin

  FunctionResult:=typeof_userinfo_row(null,null);
  
  --将单条数据值插入到自定义类型的变量中
  SELECT e.empno,e.ename  INTO FunctionResult.user_id,FunctionResult.user_name
  FROM emp e where e.empno = '7499';

  RETURN(FunctionResult);

end FUN_TEST;

Call the function and print the execution result

declare res typeof_userinfo_row;

begin
  res := FUN_TEST();
  
  dbms_output.put_line(res.user_id || ' ' || res.user_name);
  
end;

Results of the

Define table type

create or replace type typeof_userinfo_table is table of typeof_userinfo_row

Create a function with this type as the return value type

create or replace function FUN_TEST1
return typeof_userinfo_table
is

  FunctionResult typeof_userinfo_table;
begin
  --将多条记录的值同时插入到自定义类型的变量中
  SELECT typeof_userinfo_row(empno,ename) BULK COLLECT INTO FunctionResult FROM emp e;
    
  RETURN(FunctionResult);

end FUN_TEST1;

Call the function and print the execution result

declare 
res typeof_userinfo_table;
i NUMBER := 1;

begin
  res := FUN_TEST1();
  
  WHILE i <= res.LAST  LOOP
  	DBMS_OUTPUT.PUT_LINE(res(i).user_id || ' ' ||res(i).user_name);
	 
		i := i + 1;
	END LOOP;
 
end;

Results of the

Other usage reference articles: [Oracle] TYPE defined data type_oracle type type_Do_GH's Blog-CSDN Blog

Guess you like

Origin blog.csdn.net/liangmengbk/article/details/131352051