---
title: "PL/SQL 语法"
author: "dmwdmc"
date: "2010-04-29T17:32:41Z"
tags: []
categories: ["数据库"]
slug: "pl-sql"
draft: false
lastmod: "2010-05-05T15:08:27Z"
id: 656393
url: "https://dmwdmc.iteye.com/blog/656393"
---

创建 VIEW

> CREATE VIEW VIEW_A AS SELECT a.id, a.age
>   FROM a 
> UNION ALL
> SELECT a_WSP.id, a_WSP.age
>   FORM a_WSP


PL SQL的参数列表：
in：表示该参数不能被过程赋值（只能位于等号右边），只能由调用者在调用时输入值。
out:表示该参数只能被过程赋值（只能位于等号左边），调用者不用传递参数值。
in out:表示该类型既能被过程赋值也能被调用者传值。
默认情况下为in.

PL SQL 有四大内容。
1.Trigger 
VIEW 不能删除内容，但是可以加Trigger,当它删除VIEW时候调用这个trigger,实现内部的删除。

> create or replace TRIGGER trigger_del_viewa
> instead of delete on VIEW_A 
> FOR EACH ROW
> DECLARE
>    var1 tableA.id%TYPE;
>    resultset tableA%ROWTYPE;
>    var2 LONG;
>    countA NUMBER;
>    invalid_Exp EXCEPTION;
> BEGIN
>     var1:=NULL;
>     resultset :=select......;
>     select count(*) into countA from tableA where tableA=resultset;
>     if countA=0 Then
>        raise invalid_Exp;
>     end if;
>     delete from tableA where tableA=resultset;
> EXCEPTION
>     WHEN invalid_Exp then
>           Raise_application_error(-20003,'invalide_exp');
> END trigger_del_viewa


自然还可以触发在任何行为上。

> create or replace trigger after_update_viewa AFTER INSERT OR UPDATE OR DELETE ON viewa
> begin
>  proc_a('dd');--可以调用其它的procedure.
> end; 

可以省略end 后面的名字。


2.Procedure 这里是删除一行数据

> create or replace procedure proc_del_tableA(oid IN tableB.oid%TYPE)
> AS
> countA number(1);
> var1 LONG;
> invalid_Exp;
> BEGIN
> select count(*) into countA from tableA where tableA.oid =oid;
> if countA=0 then
>    raise invalid_Exp;
> end if;
> delete from tableA where tableA.oid=oid;
> EXCEPTION
>    when invalid_Exp then 
>    raise_application_error(-200001,'invalid argument');
> END;


procedure的调用可以在pl/sql内部调用，也可以通过SQL>execute proc_del_tableA('oid123123');

3.function

> create or replace function fn_get_oid(oid in tableA.oid%type) return number
> as
> l_ret number(1);
> begin 
> l_ret:=0;
> if oid is not nll then
>    l_ret:=1;
> end if;
> return l_ret;
> end;



4.package 主要是为了加快访问速度。

> create or replace package packA as
>       procedure procA;
>       function fnB return varchar2;
> 
> end packA;