Oracle Procedure (PL/SQL) 实践归纳

基本格式:

例:

create or replace procedure validateUser(USERNAME in VARCHAR2, USERPASSWORD in VARCHAR2, USERTYPE OUT VARCHAR2, VALID OUT CHAR) //头声明,参数以“参数名 in/out 类型”格式声明<o:p></o:p>

 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 ;
//过程体结束<o:p></o:p>

<o:p> </o:p>

<o:p> </o:p>

           

为适应select * 的情况,在变量声明时,可以使用variable_name tbl_name%rowtype;这样的形式,意思相似于variable_name为由tbl_name中各个column名为属性构成的struct.

<o:p> </o:p>

<o:p> </o:p>

%的奇特用途一:

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.<o:p></o:p>

简单说来就是在声明变量时,不只可以用Oracle的类型名定义,还可以用表中的字段相应类型定义。比如:<o:p></o:p>

v_name tblName.colName%Type; 这一句的意思就是将v_name定义成表tblNamecolName列的类型;而它后面的%Type是说明这种用法的固定写法。<o:p></o:p>

另外一种是在变量声明时,可以使用:

variable_name tbl_name%rowtype;<o:p></o:p>

这样的形式,意思相似于variable_name是一个由tbl_name中各个column名为属性构成的struct. (比如为适应select *  into variable_name的情况)。其中%rowtype是这种用法的固定写法。而要调用这个struct的属性,则直接以variable_name.col_name就可以了(这个与一般编程语言一样)。

<o:p> </o:p>

<o:p> </o:p>

<o:p> </o:p>

<o:p> </o:p>

有关于Exception

Oracle Procedure里的Excpetion部分很像Java里的catch块,即实现了出现异常后的处理。例如:

exception
when NO_DATA_FOUND then
//when而不是if
o_dataExist:=
0;<o:p></o:p>

格式“when – then”表示在某种异常的情况下应该进行的操作。 When后面的异常情况见表:

Oracle Exception Name<o:p></o:p>

Oracle Error<o:p></o:p>

Explanation<o:p></o:p>

DUP_VAL_ON_INDEX<o:p></o:p>

ORA-00001<o:p></o:p>

You tried to execute an INSERT or UPDATE statement that has created a duplicate value in a field restricted by a unique index.<o:p></o:p>

TIMEOUT_ON_RESOURCE<o:p></o:p>

ORA-00051<o:p></o:p>

You were waiting for a resource and you timed out.<o:p></o:p>

TRANSACTION_BACKED_OUT<o:p></o:p>

ORA-00061<o:p></o:p>

The remote portion of a transaction has rolled back.<o:p></o:p>

INVALID_CURSOR<o:p></o:p>

ORA-01001<o:p></o:p>

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.<o:p></o:p>

NOT_LOGGED_ON<o:p></o:p>

ORA-01012<o:p></o:p>

You tried to execute a call to Oracle before logging in.<o:p></o:p>

LOGIN_DENIED<o:p></o:p>

ORA-01017<o:p></o:p>

You tried to log into Oracle with an invalid username/password combination.<o:p></o:p>

NO_DATA_FOUND<o:p></o:p>

ORA-01403<o:p></o:p>

You tried one of the following: <o:p></o:p>

1.       You executed a SELECT INTO statement and no rows were returned. <o:p></o:p>

2.       You referenced an uninitialized row in a table. <o:p></o:p>

3.       You read past the end of file with the UTL_FILE package. <o:p></o:p>

TOO_MANY_ROWS<o:p></o:p>

ORA-01422<o:p></o:p>

You tried to execute a SELECT INTO statement and more than one row was returned.<o:p></o:p>

ZERO_DIVIDE<o:p></o:p>

ORA-01476<o:p></o:p>

You tried to divide a number by zero.<o:p></o:p>

INVALID_NUMBER<o:p></o:p>

ORA-01722<o:p></o:p>

You tried to execute an SQL statement that tried to convert a string to a number, but it was unsuccessful.<o:p></o:p>

STORAGE_ERROR<o:p></o:p>

ORA-06500<o:p></o:p>

You ran out of memory or memory was corrupted.<o:p></o:p>

PROGRAM_ERROR<o:p></o:p>

ORA-06501<o:p></o:p>

This is a generic "Contact Oracle support" message because an internal problem was encountered.<o:p></o:p>

VALUE_ERROR<o:p></o:p>

ORA-06502<o:p></o:p>

You tried to perform an operation and there was an error on a conversion, truncation, or invalid constraining of numeric or character data.<o:p></o:p>

CURSOR_ALREADY_OPEN<o:p></o:p>

ORA-06511<o:p></o:p>

You tried to open a cursor that is already open.<o:p></o:p>

除了上面所示的,还有其他的Exception可以用when others来捕获。

<o:p> </o:p>

<o:p> </o:p>

<o:p> </o:p>

 

你可能感兴趣的:(oracle,sql,编程)