PL SQL塊的結構

2022-08-11 05:24:13 字數 3423 閱讀 6465

-- 1. 宣告變數時,必須要指定型別

-- 2. 變數名需要先賦值,後使用

-- 3. 變數名沒有預設值(表現是空,無意義)

--pl/sql塊的結構

declare

v_name varchar2(20) := '雨女無瓜';

begin

dbms_output.put_line(v_name||'的第一條pl/sql語句');

end;

--與使用者互動輸入引數

declare

v_name varchar2(20):= '&input_name';

begin

dbms_output.put_line(v_name||'的第一條pl/sql語句');

end;

declare

v_name1 varchar2(32);

v_name2 varchar2(32) := 'abc';

v_num1 number := 13;

v_num2 number;

begin

dbms_output.put_line(v_name1);

dbms_output.put_line(v_name1 || v_name2);

dbms_output.put_line(v_num1);

dbms_output.put_line(v_num1 + v_num2);

end;

--constant 定義常量值,定義後無法修改

declare

v_number1 number := 13;

v_number2 constant number := 3.14;

begin

dbms_output.put_line(v_number1 * v_number2);

end;

--變數與指定的列的型別一致 採用%type

declare

v_num1 employees.salary%type := 13.234;

v_num2 employees.manager_id%type := 2;

begin

dbms_output.put_line(v_num1 / v_num2);

end;

--dml結果裝載入plsql變數

declare

v_salary employees.salary%type;

begin

select salary into v_salary from employees where employee_id=198;

dbms_output.put_line('v_salary:'||v_salary);

end;

--%rowtype表示資料型別是一行資料

declare

v_emp employees%rowtype;

begin

select * into v_emp from employees where employee_id = 100;

dbms_output.put_line('部門編號:'||v_emp.department_id ||

' 員工姓名:'|| v_emp.first_name);

end;

--通過使用者輸入的員工號,查詢一行記錄

declare

v_empid employees.employee_id%type;

v_emp employees%rowtype;

begin

select * into v_emp from employees where employee_id='&input_empid';

dbms_output.put_line(v_emp.first_name);

exception

when no_data_found then

dbms_output.put_line('該員工未找到');

end;

--流程控制

if 《布林表示式》 then

pl/sql 和 sql語句

else

其它語句

end if;

--if例項

declare

v_temp number :='&input_temp';

begin

if v_temp>38 then

dbms_output.put_line('隔離');

else

dbms_output.put_line('觀察');

end if;

end;

--case 條件判斷

case 條件表示式

when 條件表示式結果1 then

語句段1

when 條件表示式結果2 then

語句段2

......

when 條件表示式結果n then

語句段n

[else 條件表示式結果]

end;

--case例項

declare

--定義屬性

v_score number :='&input_score';

--定義結果

v_result varchar2(32);

begin

--case 條件判斷

v_result :=

case

when v_score between 90 and 100 then '優秀'

when v_score between 80 and 90 then '良好'

when v_score between 70 and 80 then '一般'

when v_score between 60 and 70 then '及格'

when v_score between 0 and 60 then '不及格'

else '資料異常'

end;

--列印結果

dbms_output.put_line(v_result);

end;

--陣列的輸出採用for迴圈方式,列印出來

declare

-- 定義乙個陣列型別

type type_arr is array(5) of varchar2(32);

-- 制定乙個變數為陣列的型別

v_type_arr type_arr;

begin

--變數賦值

v_type_arr :=type_arr('1','22','333','4444','55555');

--使用for迴圈陣列

for i in 1 .. v_type_arr.count loop

dbms_output.put_line(v_type_arr(i));

end loop;

end;

PL SQL複習之塊結構

pl sql 由三個部分組成 定義部分,執行部分,例外處理部分 a.只包含執行部分的pl sql塊 set serveroutput on begin dbms output.put line helllo world end 輸出 hello world 注意點1 dbms output報輸出資料...

PL SQL程式設計 Exception塊

一.預定義的異常處理 錯誤號 異常錯誤資訊名稱 說明 ora 0001 dup val on index 違反了唯一性限制 ora 0051 timeout on resource 在等待資源時發生超時 ora 0061 transaction backed out 由於發生死鎖事務被撤消 ora ...

PLSQL程式設計1 PLSQL塊的基本格式

plsql是過程化語言,能通過if,while,loop等實現流程控制,完成複雜的業務邏輯,plsql是對標準的sql語句的擴充套件。plsql塊的標準結構如下 deaclare 申明變數 begin exception 異常處理 執行這個plsql塊的結果如下 plsql塊中的識別符號有以下幾個要...