Oracle之create type与type使用整理

--create type 使用
create or replace type accountinfo as object(
id int, 
username VARCHAR2(100), 
money NUMBER(12,2)
);
create or replace type accountinfo_arr as table of accountinfo;

declare 
 acc accountinfo;
 acc_arr accountinfo_arr;
begin
  
  acc := accountinfo('1','tom',100);
  dbms_output.put_line(acc.id ||'--' || acc.username || '--' || acc.money);
  
  acc_arr := accountinfo_arr(accountinfo('2','jerry',200),accountinfo('3','jack',300));
  for k in acc_arr.first..acc_arr.last loop
     dbms_output.put_line(acc_arr(k).id ||'--' || acc_arr(k).username || '--' || acc_arr(k).money);
  end loop;
  
  insert into account (id,username,money)
  select id,username,money from table(acc_arr);
  commit;
  
  /*
  --!!!没有足够值
  select id,username,money into acc 
  from account where id = 2;
  
  --!!!没有足够值
  insert into account (id,username,money) values 
  (accountinfo('4','rose',400));
  commit;
  */
end;
--type使用
declare 
  type accountinfo is record
  (
    id int, 
    username VARCHAR2(100), 
    money NUMBER(12,2)
  );
  type accountinfo_arr is table of accountinfo; 
  
  acc accountinfo;
  acc_arr accountinfo_arr;
begin
  
 /*
 --此范围不存在‘accountinfo’函数
  acc := accountinfo('1','tom',100);
  dbms_output.put_line(acc.id ||'--' || acc.username || '--' || acc.money);
  
  --此范围不存在‘accountinfo’函数
  acc_arr := accountinfo_arr(accountinfo('2','jerry',200),accountinfo('3','jack',300));
  for k in acc_arr.first..acc_arr.last loop
     dbms_output.put_line(acc_arr(k).id ||'--' || acc_arr(k).username || '--' || acc_arr(k).money);
  end loop;
  */
  
  select id,username,money into acc 
  from account where id = 2;
  dbms_output.put_line(acc.id ||'--' || acc.username || '--' || acc.money);
  
  /*
  --!!!没有足够值
  acc.username := acc.username ||'234';
  insert into account (id,username,money) values 
  (acc);
  commit;
  */
end;
发布了132 篇原创文章 · 获赞 64 · 访问量 47万+

猜你喜欢

转载自blog.csdn.net/qiuzhi__ke/article/details/104073748