標籤:office number record 記錄 元素
4. PLSQL的記錄類型
4.1 建立記錄類型
記錄是一種複合的資料類型,一種有邏輯上相關聯的獨立元素組成的複合的資料類型
記錄只能儲存一行資料,行變數是記錄的一種特殊形式,有繼承的原結構(顯式遊標,表)
(1)直接存取記錄的元素,分別給每個欄位賦值
declare
type hrc_org_rec is record(hrc_org_id number,hrc_descr varchar2(20),org_short_name varchar2(30));
v_example_rec hrc_org_rec;
begin
v_example_rec.hrc_org_id:=1001;
v_example_rec.hrc_descr:=‘CEO/COO‘;
v_example_rec.org_short_name:=‘Office of CEO/COO ABC Inc.‘;
dbms_output.put_line(to_char(v_example_rec.hrc_org_id)||‘ ‘||v_example_rec.hrc_descr||‘ ‘||
v_example_rec.org_short_name);
exception when others then
null;
end;
(2)記錄之間的賦值
declare
type hrc_org_rec is record(hrc_org_id number,hrc_descr varchar2(20),org_short_name varchar2(30));
v_example_rec1 hrc_org_rec;
v_example_rec2 hrc_org_rec;
begin
v_example_rec1.hrc_org_id:=1001;
v_example_rec1.hrc_descr:=‘CEO/COO‘;
v_example_rec1.org_short_name:=‘Office of CEO/COO ABC Inc.‘;
v_example_rec2:=v_example_rec1;
dbms_output.put_line(to_char(v_example_rec2.hrc_org_id)||‘ ‘||v_example_rec2.hrc_descr||‘ ‘||
v_example_rec2.org_short_name);
exception when others then
null;
end;
將上面的練習使用記錄的方式實現:
declare
type rc is ref cursor;
v_rc rc;
v_deptno number;
type emp_dept_rec is record(v_dname varchar2(30),v_empno number,v_ename varchar2(30),v_job varchar2(30),v_hiredate date,v_sal number);
v_example_rec emp_dept_rec;
type emp_dept_rec1 is record(v_empno number,v_sal number,v_grade number);
v_example_rec1 emp_dept_rec1;
begin
open v_rc for select d.dname,e.empno,e.ename,e.job,e.hiredate,e.sal
from emp e,dept d
where e.deptno=d.deptno;
loop
fetch v_rc into v_example_rec; --必須要整一行都賦予記錄
exit when(v_rc%notfound);
dbms_output.put_line(v_example_rec.v_dname||‘ ‘||v_example_rec.v_empno||‘ ‘||v_example_rec.v_ename||‘ ‘||v_example_rec.v_job||‘ ‘||to_char(v_example_rec.v_hiredate)||‘ ‘||v_example_rec.v_sal);
end loop;
close v_rc;
dbms_output.put_line(‘------------------------------------‘);
open v_rc for select a.empno,a.sal,s.grade
from emp a,salgrade s
where a.sal between s.losal and s.hisal;
loop
fetch v_rc into v_example_rec1;
exit when(v_rc%notfound);
dbms_output.put_line(to_char(v_example_rec1.v_empno)||‘ ‘||rpad(to_char(v_example_rec1.v_sal),10,‘ ‘)||‘ ‘||to_char(v_example_rec1.v_grade));
end loop;
close v_rc;
exception when others then
null;
end;
###########################################################################################
4.2 oracle的序列對象
(1) 定義一個序列:
create sequence hrc_org_seq minvalue 1 maxvalue 999999 start with 100 increment by 1;
(2) 查詢驗證:
select * from user_objects where object_name=‘HRC_ORG_SEQ‘;
select * from user_sequences;
(3) 使用方法:
SQL> select hrc_org_seq.nextval from dual;
NEXTVAL
----------
100
SQL> select hrc_org_seq.nextval from dual;
NEXTVAL
----------
101
SQL> select hrc_org_seq.nextval from dual;
NEXTVAL
----------
102
經常用作主鍵的產生器,比如:
SQL> select ‘44522‘||to_char(sysdate,‘yyyymmdd‘)||‘BOSS‘||hrc_org_seq.nextval from dual;
這個主鍵永遠都不會重複
一般不用有業務含義的欄位作主鍵,設計系統的時候要求一旦主鍵產生就不能再被修改,只能刪除,也不能夠update
有業務含義的欄位經常會被修改,比如某個人手機號,認證號碼,屬性資訊可能會發生錯亂而導致資料的不一致。
(4)刪除一個序列對象
SQL> drop sequence hrc_org_seq;
Sequence dropped
###########################################################################################
4.3記錄類型的相關操作
(1) 通過select into 語句來給記錄賦值
declare
type hrc_org_rec is record(hrc_org_id number,hrc_descr varchar2(20),org_short_name varchar2(30));
v_example_rec hrc_org_rec;
begin
select hrc_org_seq.nextval,h.hrc_descr,o.org_short_name
into v_example_rec
from org_tab o,hrc_tab h
where o.hrc_code=h.hrc_code
and o.org_id=1002; --使用select into方式填充記錄,必須要確保查到的資料是一行
dbms_output.put_line(to_char(v_example_rec.hrc_org_id)||‘ ‘||v_example_rec.hrc_descr||‘ ‘||
v_example_rec.org_short_name);
exception when others then
null;
end;
(2)將6個org_id都通過記錄讀取並且輸出,修改上面的程式
declare
type hrc_org_rec is record(
hrc_org_id number,
hrc_descr varchar2(20),
org_short_name varchar2(30));
v_example_rec hrc_org_rec;
begin
for idx in (select org_id from org_tab) loop
select hrc_org_seq.nextval, h.hrc_descr, o.org_short_name
into v_example_rec
from org_tab o, hrc_tab h
where o.hrc_code = h.hrc_code
and o.org_id = idx.org_id; --使用select into方式填充記錄,必須要確保查到的資料是一行
dbms_output.put_line(to_char(v_example_rec.hrc_org_id) || ‘ ‘ ||
v_example_rec.hrc_descr || ‘ ‘ ||
v_example_rec.org_short_name);
end loop;
exception
when others then
null;
end;
注意:測試兩個記錄是否相等,不能 if record1 = record2 .... 錯誤的寫法,只能使用每個欄位的內容都去比較
declare
type hrc_org_rec is record(hrc_org_id number,hrc_descr varchar2(20),org_short_name varchar2(30));
v_example_rec1 hrc_org_rec;
v_example_rec2 hrc_org_rec;
begin
v_example_rec1.hrc_org_id:=1001;
v_example_rec1.hrc_descr:=‘CEO/COO‘;
v_example_rec1.org_short_name:=‘Office of CEO/COO ABC Inc.‘;
v_example_rec2:=v_example_rec1;
if(v_example_rec1.hrc_org_id=v_example_rec2.hrc_org_id and v_example_rec1.hrc_descr=v_example_rec2.hrc_descr and v_example_rec1.org_short_name = v_example_rec2.org_short_name) then
dbms_output.put_line(‘YES‘);
end if;
exception when others then
null;
end;
練習:修改之前的練習,嘗試用記錄實現接收取到的值
-------------------------------------------------
定義一個遊標變數類型,聲明遊標,分別開啟兩個查詢並輸出
1.員工的姓名、薪資、僱傭日期、部門名
2.員工的編號、薪水、薪水等級,關聯salgrade表
(3) 行變數的記錄:行變數是一種特殊的記錄,結構是基於表或者遊標的。
declare
cursor csr_hrc is select * from hrc_tab order by 1;
hrc_rec csr_hrc%rowtype;
hrc_rec1 hrc_tab%rowtype;
type hrc_org_rec is record(hrc_code number,hrc_descr varchar2(20));
hrc_rec2 hrc_org_rec;
begin
open csr_hrc;
loop
fetch csr_hrc into hrc_rec;
exit when(csr_hrc%notfound);
dbms_output.put_line(to_char(hrc_rec.hrc_code)||‘ ‘||hrc_rec.hrc_descr);
end loop;
close csr_hrc;
dbms_output.put_line(‘-----------------------------------‘);
open csr_hrc;
loop
fetch csr_hrc into hrc_rec1;
exit when(csr_hrc%notfound);
dbms_output.put_line(to_char(hrc_rec1.hrc_code)||‘ ‘||hrc_rec1.hrc_descr);
end loop;
close csr_hrc;
dbms_output.put_line(‘-----------------------------------‘);
open csr_hrc;
loop
fetch csr_hrc into hrc_rec2;
exit when(csr_hrc%notfound);
dbms_output.put_line(to_char(hrc_rec2.hrc_code)||‘ ‘||hrc_rec2.hrc_descr);
end loop;
close csr_hrc;
dbms_output.put_line(‘-----------------------------------‘);
open csr_hrc;
loop
fetch csr_hrc into hrc_rec; --fetch值給行變數
exit when(csr_hrc%notfound);
hrc_rec2:=hrc_rec; --將行變數賦值給記錄類型的變數,能夠賦值成功
dbms_output.put_line(to_char(hrc_rec2.hrc_code)||‘ ‘||hrc_rec2.hrc_descr);
end loop;
close csr_hrc;
exception when others then
null;
end;
(4)涉及到DML操作的記錄
A insert操作
declare
type hrc_org_rec is record(hrc_code number,hrc_descr varchar2(30));
v_example_rec hrc_org_rec;
begin
v_example_rec.hrc_code:=6;
v_example_rec.hrc_descr:=‘Wev analyze l‘;
insert into hrc_tab values v_example_rec; --使用記錄的方式插入一行資料
commit;
exception when others then
null;
end;
驗證:
select * from hrc_tab;
B update操作
declare
type hrc_org_rec is record(hrc_code number,hrc_descr varchar2(30));
v_example_rec hrc_org_rec;
begin
v_example_rec.hrc_code:=6;
v_example_rec.hrc_descr:=‘Wev analyze 2‘;
update hrc_tab set row=v_example_rec where hrc_code=v_example_rec.hrc_code; --使用記錄的方式更新一行資料
commit; --注意row關鍵字
exception when others then
null;
end;
驗證:
select * from hrc_tab;
C delete操作
declare
type hrc_org_rec is record(hrc_code number,hrc_descr varchar2(30));
v_example_rec hrc_org_rec;
begin
v_example_rec.hrc_code:=6;
delete from hrc_tab where hrc_code=v_example_rec.hrc_code; --使用記錄的方式刪除一行記錄
commit;
exception when others then
null;
end;
驗證:
select * from hrc_tab;
注意:不能用整行作為過濾條件
declare
type hrc_org_rec is record(hrc_code number,hrc_descr varchar2(30));
v_example_rec hrc_org_rec;
begin
v_example_rec.hrc_code:=6;
v_example_rec.hrc_descr:=‘Web anayled 1‘;
delete from hrc_tab where row=v_example_rec; --不能使用這種方式刪除
commit;
exception when others then
null;
end;
plsql記錄,對象序列