天天看點

oracle系列(四)PL/SQL

過程,函數,觸發器是PL/SQL編寫的,存儲在oracle中的.

PL/SQL是非常強大的資料庫過程語言.

PL/SQL優點:

性能,子產品化,網絡傳輸量,安全性

缺點:移植性不好

簡單分類:

塊:過程,函數,觸發器,包

Demo:

create or replace procedure sp01 is

begin

insert into ice.persons values(10,'jack',18,'shenzhen');

commit;

end sp01;

編寫規範:

1.注釋

單行注釋 --

多行注釋

2.辨別符号的命名規範

1)變量,v_作為字首 v_sal (變量名不要跟表的字段名一樣)

2)常量,c_作為字首 c_rate

3)遊标,_cursor作為字尾 emp_cursor;

4)例外,e_作為字首,e_error

table:persons

ID NAME AGE CITY

1 10 tom 18 shenzhen

--SQL Window

--1.0最簡單的塊

begin

dbms_output.put_line('hello world!');

end;

--包含定義部分和執行部分的pl/sql塊

declare

v_ename varchar2(5); --定義字元串變量

v_city varchar2(50);

begin

select t.name,t.city into v_ename,v_city from PERSONS t where t.id = &person_id;

dbms_output.put_line('ID為10的Person的名字' || v_ename||',城市'||v_city);

--異常處理

exception

when no_data_found then

dbms_output.put_line('編号輸入有誤!');

end;

--2.0 過程

create procedure sp02(personId INTEGER, personName varchar2) is

begin

update persons p set p.name=personName where p.id=personId;

commit;

end;

--3.0 函數

create or replace function func_01(personId in varchar2) return number is

FunctionResult number;

begin

select p.age+18 into FunctionResult from ice.persons p where p.id=personId;

return(FunctionResult);

end func_01;

--調用

select func_01(10) from dual;

--4.0包

--4.1建立包規範

create or replace package pkg_01 is

-- Function and procedure implementations

function func_02(personId in varchar2) return number;

end pkg_01;

--4.2建立包體

create or replace package body pkg_01 is

-- Function and procedure implementations

function func_02(personId in varchar2) return number is

FunctionResult number;

begin

select p.age + 18

into FunctionResult

from ice.persons p

where p.id = personId;

return(FunctionResult);

end func_02;

end pkg_01;

--5.0定義并使用變量

--5.1标量

v_ename varchar2(10);

v_sal number(6,2);

v_sal2 number(6,2):=5.4;--注意指派運算符 :=

v_hiredate date;

v_valid boolean not null default false;

%type

v_ename persons.name%type;

--5.2記錄類型

declare

type person_record_type is record(

name persons.name%type,

age persons.age%type);

sp_record person_record_type;

begin

select p.name, p.age into sp_record from persons p where p.id = 10;

dbms_output.put_line(sp_record.name || '->' || sp_record.age);

end;

--5.3表類型:相當于進階語言中的數組

declare

type sp_table_type is table of persons.name%type index by binary_integer;

sp_table sp_table_type;

begin

select name into sp_table(0) from persons p where p.id = 10;--去掉where會報錯. 如果要去掉,使用參照變量

dbms_output.put_line('姓名' || sp_table(0));

end;

--5.4參照變量:遊标變量(ref cursor)

declare

type sp_person_cursor_type is ref cursor;

person_cursor sp_person_cursor_type;

v_name persons.name%type;

v_age persons.age%type;

begin

open person_cursor for

select name, age from persons p;

loop

fetch person_cursor

into v_name, v_age;

--判斷退出

exit when person_cursor%notfound;

dbms_output.put_line(v_name || '->' || v_age);

end loop;

end;

https://www.cnblogs.com/linjiqin/category/349944.html

轉載于:https://www.cnblogs.com/ICE_Inspire/p/9380226.html