OCM读书笔记(2)

1. % type 用法,提取% type所在字段的类型


declare
    myid dept.deptno % type;
    myname dept.dname % type;
begin
    select deptno, dname into myid, myname from dept where deptno = 10;
    dbms_output.put_line(myid);
    dbms_output.put_line(myname);
end;2. % rowtype 用法,提取% rowtype所在字段的类型


declare
    type type_dept is table of dept % rowtype index by binary_integer;
    tb type_dept;
begin
    tb(1).deptno:='001';
    tb(2).deptno:='002';
    dbms_output.put_line(tb.COUNT);
end;
/3. TYPE用法,相当于结构体


declare
    lv_order_date DATE:=sysdate;
    lv_last_tr varchar2(5) default '001';
    lv_last varchar2(10) not null:='us';
    TYPE type_test is record(
        myid dept.deptno % type,
        myname dept.dname % type
    );
    rec type_test;
begin
    dbms_output.put_line(lv_last);
    select deptno, dname into rec from dept where deptno=10;
    dbms_output.put_line(rec.myid);
    dbms_output.put_line(rec.myname);
end;
/4. 游标的使用


declare
    g_id number(2) := 20;
    find_not char(1) := 'N';
    cursor cur is select * from dept;
    TYPE type_dept is record(
        myid dept.deptno % type,
        myname dept.dname % type,
        myaddr dept.loc % type
    );
    rect type_dept;
begin
    open cur;
    loop
        fetch cur into rect;
        exit when cur%     NOTFOUND;
        if rect.myid = g_id then
            dbms_output.put_line('Find it!!');
            dbms_output.put_line('DEPT NO: ' || rect.myid);
            dbms_output.put_line('DNAME: ' || rect.myname);
            dbms_output.put_line('LOC: ' || rect.myaddr);
        end if;
    end loop;
    close cur;
   
    if find_not = 'N' then
        dbms_output.put_line('No Record');
    end if;
end;
/5. for循环

begin
    for i in 1..5 loop
        dbms_output.put_line(i);
    end loop;
end;
/6. loop循环


declare
    v number := 1;
begin
    loop
        exit when v > 5;
        dbms_output.put_line(v);
        v := v+1;
    end loop;
end;
/ 7. while循环


declare
    v number := 1;
begin
    while v <= 5 loop
        dbms_output.put_line(v);
        v := v+1;
    end loop;
end;
/

原文地址:https://www.cnblogs.com/thlzhf/p/3357884.html