PL/SQL 编程(二)
1?? ?For循環(huán)
?? ?語法:begin
?? ??? ??? ?for i in reverse 1..10 loop
?? ??? ??? ?insert into users values(i,’奧巴馬‘)。
?? ??? ??? ?end loop;
?? ??? ?? end;
?? ?注意:循環(huán)變量 i 是隱含添加的。所以無法看到
?? ?
2?? ?goto語句
?? ?goto 語句用于跳轉(zhuǎn)到特定的位置去運(yùn)行語句。因?yàn)間oto語句會(huì)降低程序的可讀性,所以普通情況下
?? ?不建議使用goto語句
?? ?
3?? ?null語句
?? ?null語句不會(huì)運(yùn)行不論什么操作,可是能夠添加程序的可讀性
?? ?
4?? ?創(chuàng)建返回值是一個(gè)結(jié)果集的存儲(chǔ)過程
?? ?(1) 創(chuàng)建一個(gè)包:
?? ??? ?SQL> create or replace package testpackage as
?? ??? ??? ? type test test_cursor is ref cursor;
?? ??? ??? ? end testpackage;
?? ??? ??? ?
?? ?(2) 創(chuàng)建存儲(chǔ)過程
?? ??? ?SQL> create or replace procedure sp_procedure1
?? ??? ??? ? (stuNo in number, param_cursor out testpackage.test_cursor) is
?? ??? ??? ? begin
?? ??? ??? ??? ?open param_cursor for select * from emp where sutno=stuNo;
?? ??? ??? ? end;
?? ?
5?? ?分頁
?? ?(1) sql語句
?? ???? select * from
?? ??? ?(select *,rownum NO from
?? ??? ?(select * from emp) where rownum <=20) where rownum >=10;
?? ??? ?
?? ?(2)?? ?創(chuàng)建一個(gè)包
?? ??? ?create or replace package testpackage2 as
?? ??? ??? ? type test test_cursor is ref cursor;
?? ??? ??? ? end testpackage2;
?? ??? ??? ?
?? ?(3)?? ?創(chuàng)建存儲(chǔ)過程
?? ??? ?SQL> create or replace procedure procedureName2
?? ??? ??? ?(tableName in varchar2,?? ??? ??? ??? ??? ?-- 表名
?? ??? ??? ?pageSize in number,?? ??? ??? ??? ??? ??? ?-- 每頁顯示的記錄數(shù)
?? ??? ??? ?pageNow in number,?? ??? ??? ??? ??? ??? ?-- 當(dāng)前是第幾頁
?? ??? ??? ?pageCount out number,?? ??? ??? ??? ??? ?-- 總頁數(shù)
?? ??? ??? ?p_cursor out testpackage2.test_cursor) is
?? ??? ??? ?v_sql varchar2(1000);
?? ??? ??? ?v_beginNum number := (pageNow -1)* pageSize + 1;
?? ??? ??? ?v_endNum number := pageNow * pageSize;
?? ??? ??? ?begin
?? ??? ??? ??? ?v_sql := 'select * from (select *,rownum NO from (select * from '|| tableName ||')
?? ??? ??? ??? ?where rownum <= '|| v_endNum ||') where rownum >= '|| v_beginNum;
?? ??? ??? ??? ?open p_cursor for v_sql;
?? ??? ??? ??? ?-- 創(chuàng)建一個(gè)sql語句
?? ??? ??? ??? ?v_sql := 'select count(*) from ' || tableName;
?? ??? ??? ??? ?-- 運(yùn)行sql語句,將結(jié)果保存
?? ??? ??? ??? ?execute immediate v_sql into rows。
?? ??? ??? ??? ?if mod(rows,pageSize) = 0
?? ??? ??? ??? ?then pageCount := rows / pageSize;
?? ??? ??? ??? ?else
?? ??? ??? ??? ?pageCount := rows / pageSize + 1;
?? ??? ??? ??? ?end if;
?? ??? ??? ??? ?-- 關(guān)閉游標(biāo)
?? ??? ??? ??? ?close p_cursor;
?? ??? ??? ?end;
?? ?
6?? ?異常處理
?? ?(1) 提前定義異常
?? ?(2) 非提前定義異常
?? ?(3) 自己定義異常
?? ?
?? ?例1
?? ?SQL> declare v_name emp.ename%type;
?? ??? ? begin
?? ??? ? select ename into v_name from emp where empno = &no;
?? ??? ? dbms_output.put_line('名字:' || v_name);
?? ??? ? exception
?? ??? ??? ?when no_data_found
?? ??? ??? ?then dbms_output.put_line('編號(hào)沒有!
');
?? ??? ? end;
?? ??? ?
?? ?提前定義異常
?? ?a case_not_found
?? ?? 在編寫case 語句時(shí)。假設(shè)在when子句中沒有包括必須的條件分支(沒有符合條件的)。就會(huì)觸發(fā)case_not_found異常
??? b cursor_already_open
?? ?? 當(dāng)又一次打開已經(jīng)打開的游標(biāo)時(shí)觸發(fā)此異常
?? ?c dup_val_on_index
?? ?? 在唯一索引所相應(yīng)的列上插入反復(fù)值時(shí)觸發(fā)此異常
?? ?d invalid_cursor
?? ?? 當(dāng)試圖在不合法的游標(biāo)上進(jìn)行操作時(shí)觸發(fā)此異常?? ?
?? ?e invalid_number
?? ?? 當(dāng)輸入的數(shù)字無效時(shí)觸發(fā)此異常
?? ?f too_many_rows
?? ?? 當(dāng)返回值不止是一條記錄時(shí)觸發(fā)此異常
?? ?g zero_divide
?? ?? 當(dāng)進(jìn)行 x/0,即除數(shù)為零的操作時(shí)觸發(fā)此異常
?? ?h value_error
?? ?? 當(dāng)進(jìn)行賦值操作時(shí),假設(shè)變量的長(zhǎng)度不足以存儲(chǔ)實(shí)際數(shù)據(jù)時(shí)觸發(fā)此異常
?? ?i login——denide
?? ?? 當(dāng)用戶非法登錄時(shí)會(huì)觸發(fā)此異常
?? ?j not_logged_on
?? ?? 假設(shè)用戶沒有登錄就運(yùn)行DML操作,就會(huì)觸發(fā)此異常
?? ?k storage_error
?? ?? 假設(shè)超出了內(nèi)存空間,就會(huì)觸發(fā)此異常
?? ?l timeout_on_resource
?? ?? 當(dāng)Oracle等待資源時(shí)。假設(shè)發(fā)生超時(shí)情況,就會(huì)觸發(fā)此異常
??? 自己定義異常
?? ?SQL> create or replace procedure procedureName2(sp_empNo number) is
?? ??? ? MyExpception Exceptiom;?? ??? ?-- 自己定義一個(gè)異常
?? ??? ? begin
?? ??? ? update emp set sal = sal * 1.2 where empno = &no;
?? ??? ? if sql%notfound then
?? ??? ??? ?raise MyExpception;?? ??? ??? ?-- 觸發(fā)自己定義異常
?? ??? ? end if;
?? ??? ? exception
?? ??? ??? ?when no_data_found
?? ??? ??? ?then dbms_output.put_line('沒有更新數(shù)據(jù)!
');
?? ??? ? end;
?? ?
?? ?
?? ?
?? ?
?? ?
???
轉(zhuǎn)載于:https://www.cnblogs.com/mqxnongmin/p/10670815.html
總結(jié)
以上是生活随笔為你收集整理的PL/SQL 编程(二)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: GULP案例1:计算声子色散曲线和态密度
- 下一篇: chrome java虚拟机_JATT: