本文主要介紹的是Oracle游標提取相關的數據的實際操作方案首先我們要從Oracle游標中得到一行相關的數據我個人提議你使用FETCH命令當每一次提取數據後Oracle游標都指向結果集的下一行語法如下
FETCH cursor_name INTO variable[variable]
對於SELECT定義的Oracle游標的每一列FETCH變量列表都應該有一個變量與之相對應變量的類型也要相同
例
SET SERVERIUTPUT ON
DECLARE
v_ename EMPENAME%TYPE;
v_salary EMPSALARY%TYPE;
CURSOR c_emp IS SELECT enamesalary FROM emp;
BEGIN
OPEN c_emp;
FETCH c_emp INTO v_enamev_salary;
DBMS_OUTPUTPUT_LINE(Salary of Employee|| v_ename
||is|| v_salary);
FETCH c_emp INTO v_enamev_salary;
DBMS_OUTPUTPUT_LINE(Salary of Employee|| v_ename
||is|| v_salary);
FETCH c_emp INTO v_enamev_salary;
DBMS_OUTPUTPUT_LINE(Salary of Employee|| v_ename
||is|| v_salary);
CLOSE c_emp;
END
這段代碼無疑是非常麻煩的如果有多行返回結果可以使用循環並用Oracle游標屬性為結束循環的條件以這種方式提取數據程序的可讀性和簡潔性都大為提高下面我們使用循環重新寫上面的程序
SET SERVERIUTPUT ON
DECLARE
v_ename EMPENAME%TYPE;
v_salary EMPSALARY%TYPE;
CURSOR c_emp IS SELECT enamesalary FROM emp;
BEGIN
OPEN c_emp;
LOOP
FETCH c_emp INTO v_enamev_salary;
EXIT WHEN c_emp%NOTFOUND;
DBMS_OUTPUTPUT_LINE(Salary of Employee|| v_ename
||is|| v_salary);
END
以上的相關內容就是對Oracle游標提取數據的介紹望你能有所收獲
From:http://tw.wingwit.com/Article/program/Oracle/201311/18889.html