基本格式:
例:
create or replace procedure validateUser(USERNAME in VARCHAR2, USERPASSWORD in VARCHAR2, USERTYPE OUT VARCHAR2, VALID OUT CHAR) //头声明,参数以“参数名 in/out 类型”格式声明
is //过程开始
v_temp number; //变量声明,格式为“变量名 类型”,“;”结尾
begin //过程体开始
select count(1) INTO v_temp from MANAGER //一种赋值的方式是用select into的形式
where ma_name=USERNAME and ma_password=USERPASSWORD; //写完一个语句用“;”结尾
if(v_temp=0) then //这里是“if-then-else-end if”的格式
usertype:='not legal user'; //直接赋值
valid:='0';
else
select ma_type into usertype from manager
where ma_name=USERNAME and ma_password=USERPASSWORD;
valid:='1';
end if;
end ; //过程体结束
为适应select * 的情况,在变量声明时,可以使用variable_name tbl_name%rowtype;这样的形式,意思相似于variable_name为由tbl_name中各个column名为属性构成的struct.
%的奇特用途一:
在Oracle Tutorial中有这样一段话:
Instead of specifying a data type, one can also refer to the data type of a table column (so-called anchored declaration). For example, EMP.Empno%TYPE refers to the data type of the column Empno in the relation EMP. Instead of a single variable, a record can be declared that can store a complete tuple from a given table (or query result). For example, the data type DEPT%ROWTYPE specifies a record suitable to store all attribute values of a complete row from the table DEPT. Such records are typically used in combination with a cursor. A field in a record can be accessed using <record name>.<column name>, for example, DEPT.Deptno.
简单说来就是在声明变量时,不只可以用Oracle的类型名定义,还可以用表中的字段相应类型定义。比如:
v_name tblName.colName%Type; 这一句的意思就是将v_name定义成表tblName中colName列的类型;而它后面的%Type是说明这种用法的固定写法。
另外一种是在变量声明时,可以使用:
variable_name tbl_name%rowtype;
这样的形式,意思相似于variable_name是一个由tbl_name中各个column名为属性构成的struct. (比如为适应select * into variable_name的情况)。其中%rowtype是这种用法的固定写法。而要调用这个struct的属性,则直接以variable_name.col_name就可以了(这个与一般编程语言一样)。
有关于Exception:
Oracle Procedure里的Excpetion部分很像Java里的catch块,即实现了出现异常后的处理。例如:
exception
when NO_DATA_FOUND then //是when而不是if
o_dataExist:=0;
格式“when – then”表示在某种异常的情况下应该进行的操作。 When后面的异常情况见表:
Oracle Exception Name |
Oracle Error |
Explanation |
DUP_VAL_ON_INDEX |
ORA-00001 |
You tried to execute an INSERT or UPDATE statement that has created a duplicate value in a field restricted by a unique index. |
TIMEOUT_ON_RESOURCE |
ORA-00051 |
You were waiting for a resource and you timed out. |
TRANSACTION_BACKED_OUT |
ORA-00061 |
The remote portion of a transaction has rolled back. |
INVALID_CURSOR |
ORA-01001 |
You tried to reference a cursor that does not yet exist. This may have happened because you've executed a FETCH cursor or CLOSE cursor before OPENing the cursor. |
NOT_LOGGED_ON |
ORA-01012 |
You tried to execute a call to Oracle before logging in. |
LOGIN_DENIED |
ORA-01017 |
You tried to log into Oracle with an invalid username/password combination. |
NO_DATA_FOUND |
ORA-01403 |
You tried one of the following: 1. You executed a SELECT INTO statement and no rows were returned. 2. You referenced an uninitialized row in a table. 3. You read past the end of file with the UTL_FILE package. |
TOO_MANY_ROWS |
ORA-01422 |
You tried to execute a SELECT INTO statement and more than one row was returned. |
ZERO_DIVIDE |
ORA-01476 |
You tried to divide a number by zero. |
INVALID_NUMBER |
ORA-01722 |
You tried to execute an SQL statement that tried to convert a string to a number, but it was unsuccessful. |
STORAGE_ERROR |
ORA-06500 |
You ran out of memory or memory was corrupted. |
PROGRAM_ERROR |
ORA-06501 |
This is a generic "Contact Oracle support" message because an internal problem was encountered. |
VALUE_ERROR |
ORA-06502 |
You tried to perform an operation and there was an error on a conversion, truncation, or invalid constraining of numeric or character data. |
CURSOR_ALREADY_OPEN |
ORA-06511 |
You tried to open a cursor that is already open. |
除了上面所示的,还有其他的Exception可以用when others来捕获。