Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production PL/SQL Release 11.2.0.1.0 - Production CORE 11.2.0.1.0 Production TNS for 64-bit Windows: Version 11.2.0.1.0 - Production NLSRTL Version 11.2.0.1.0 - Production
# This file is actually generated by netca. But if customers choose to # install "Software Only", this file wont exist and without the native # authentication, they will not be able to connect to the database on NT.
DECLARE V_RES NUMBER(8, 2) DEFAULT''; --自定义变量 V_NAME VARCHAR2(10); /* 标量类型的变量 */ V_NAME2 STUDENT.NAME%TYPE; -- 跟STUDENT表NAME字段同类型 V_ID NUMBER(4, 2); V_SEX CONSTANT CHAR(1) :='1'; -- CONSTANT /* 复合类型的变量 */ -- 1. 记录类型 TYPE TYPE_STUDENT_RECORD IS RECORD( -- a.定义 NAME STUDENT.NAME%TYPE, -- 注意:逗号 CLASS_ID STUDENT.CLASS_ID%TYPE ); V_STUDENT_RESULT TYPE_STUDENT_RECORD; -- b.声明 -- 1.2. %ROWTYPE V_STUDENT STUDENT%ROWTYPE; -- 2. PL/SQL索引表类型(关联数组) TYPE TYPE_TABLE_STUDENT ISTABLEOF STUDENT%ROWTYPE -- 定义 INDEX BY BINARY_INTEGER; -- 以数字为键值 TYPE TYPE_TABLE_NAME ISTABLEOF VARCHAR2(8) INDEX BY PLS_INTEGER; V_TABLE_STUDENT TYPE_TABLE_STUDENT; -- 声明 V_TABLE_NAME TYPE_TABLE_NAME; TYPE TYPE_TABLE_STUDENT_NAME ISTABLEOF STUDENT.NAME%TYPE INDEX BY VARCHAR2(20); -- 下标是string V_TABLE_STUDENT_NAME TYPE_TABLE_STUDENT_NAME; -- 3. VARRAY变长数组,适合较少数据使用 TYPE TYPE_VARRAY IS VARRAY(100) OF VARCHAR2(20); V_VARRAY TYPE_VARRAY := TYPE_VARRAY('0','2','1'); -- 声明时必须初始化
BEGIN V_RES :=100/3; DBMS_OUTPUT.PUT_LINE('print on screen.'|| V_RES); /* 将结果存到变量中 */ SELECT NAME INTO V_NAME FROM STUDENT WHERE ID='2'; --抛异常后是不再往下执行的 DBMS_OUTPUT.PUT_LINE('print on screen.'|| V_NAME);
SELECT NAME INTO V_NAME2 FROM STUDENT WHERE ID='1'; DBMS_OUTPUT.PUT_LINE('print on screen.'|| V_NAME2);
SELECT NAME, CLASS_ID INTO V_STUDENT_RESULT FROM STUDENT WHERE ID='3'; DBMS_OUTPUT.PUT_LINE('print on screen.'|| V_STUDENT_RESULT.NAME ||', '|| V_STUDENT_RESULT.CLASS_ID);
SELECT*INTO V_STUDENT FROM STUDENT WHERE ID='1'; DBMS_OUTPUT.PUT_LINE('print on screen.'|| V_STUDENT.NAME ||', '|| V_STUDENT.CLASS_ID);
SELECT*INTO V_TABLE_STUDENT(3) FROM STUDENT WHERE ID='4'; DBMS_OUTPUT.PUT_LINE('print on screen.'|| V_TABLE_STUDENT(3).NAME); -- 下标为3,空前两个都可以
SELECT NAME INTO V_TABLE_STUDENT_NAME('ID4') FROM STUDENT WHERE ID='4'; DBMS_OUTPUT.PUT_LINE('student name is : '|| V_TABLE_STUDENT_NAME('ID4')); V_VARRAY(1) :='HELLO'; V_VARRAY(3) :='WORLD'; DBMS_OUTPUT.PUT_LINE(V_VARRAY(1) ||', '|| V_VARRAY(2)); EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('data not found.'); WHEN TOO_MANY_ROWS THEN DBMS_OUTPUT.PUT_LINE('too many rows.'); END; / EXIT;