过程,函数,触发器是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
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|