--------------------------存储过程练习-------------------------------------------
--存储过程基本语法结构,我在运行过程中,总会出现已在sql窗口建了某张表,但在命令窗口查询说不存在,
--主要问题在于用户切换导致的用户不同------
DECLARE
v_ename emp.ename%TYPE;
BEGIN
SELECT ename INTO v_ename FROM emp WHERE empno = &empno;
dbms_output.put_line('员工姓名:' || v_ename);
EXCEPTION
WHEN NO_DATA_FOUND
THEN dbms_output.put_line('查无此人!');
END;
/**
条件判断
IF boolean THEN executable END IF;
IF boolean THEN executable ELSE executable END IF;
IF boolean THEN executable ELSIF boolean THEN executable ELSE executable END IF;
*/
/**
循环 (LOOP WHILE FOR)
*/
-- LOOP
DECLARE
j NUMBER := 0;
BEGIN
j := 1;
LOOP
dbms_output.put_line(j);
EXIT WHEN j >= 7;
j := j + 1;
END LOOP;
END;
-- WHILE
DECLARE
j NUMBER := 0;
BEGIN
j := 1;
WHILE j <= 8
LOOP
dbms_output.put_line(j || '---');
j := j+1;
END LOOP;
END;
-- FOR
DECLARE
j NUMBER := 0;
BEGIN
FOR j IN 1..8
LOOP
dbms_output.put_line(j || '---');
END LOOP;
END;
/**
存储过程.PROCEDURE
参数形式三种 (in, outm in out)
*/
-- 示例一
CREATE OR REPLACE PROCEDURE PROC1(i IN NUMBER)
AS
a VARCHAR2(50);
BEGIN
a := '';
FOR j IN 1..i
LOOP
a := a || '*';
dbms_output.put_line(a);
END LOOP;
END;
-- 两种方式调用存储过程 1 通过exec执行 2 通过块执行
-- exec PROC1(4);
BEGIN
PROC1(7);
END;
----------------------运行------------------------------------------
SQL> CREATE OR REPLACE PROCEDURE PROC1(i IN NUMBER)
2 AS
3 a VARCHAR2(50);
4 BEGIN
5 a := '';
6 FOR j IN 1..i
7 LOOP
8 a := a || '*';
9 dbms_output.put_line(a);
10 END LOOP;
11 END;
12 /
Procedure created
SQL>
SQL> BEGIN
2 PROC1(7);
3 END;
4 /
*
**
***
****
*****
******
*******
PL/SQL procedure successfully completed
--示例二
CREATE OR REPLACE PROCEDURE PROC2(i OUT NUMBER)
AS
BEGIN
i := 100;
dbms_output.put_line(i);
END;
DECLARE
k NUMBER;
BEGIN
PROC2(K);
dbms_output.put_line(k);
END;
----------------------------运行--------------------------------
SQL> CREATE OR REPLACE PROCEDURE PROC2(i OUT NUMBER)
2 AS
3 BEGIN
4 i := 100;
5 dbms_output.put_line(i);
6 END;
7 /
Procedure created
SQL>
SQL> DECLARE
2 k NUMBER;
3 BEGIN
4 PROC2(K);
5 dbms_output.put_line(k);
6 END;
7 /
100
100
PL/SQL procedure successfully completed
-- 示例三
CREATE OR REPLACE PROCEDURE PROC3(p1 IN OUT NUMBER , p2 IN OUT NUMBER)
AS
v_temp NUMBER;
BEGIN
v_temp := p1;
p1 := p2;
p2 := v_temp;
END;
DECLARE
num1 NUMBER := 10;
num2 NUMBER := 20;
BEGIN
PROC3(num1, num2);
dbms_output.put_line(num1);
dbms_output.put_line(num2);
END;
------------------------------------------------------------------------------
SQL> CREATE OR REPLACE PROCEDURE PROC3(p1 IN OUT NUMBER , p2 IN OUT NUMBER)
2 AS
3 v_temp NUMBER;
4 BEGIN
5 v_temp := p1;
6 p1 := p2;
7 p2 := v_temp;
8 END;
9 /
Procedure created
SQL>
SQL> DECLARE
2 num1 NUMBER := 10;
3 num2 NUMBER := 20;
4 BEGIN
5 PROC3(num1, num2);
6 dbms_output.put_line(num1);
7 dbms_output.put_line(num2);
8 END;
9 /
20
10
PL/SQL procedure successfully completed
--函数--
/**
函数:(函数是可以返回值的命名的PL/SQL 子程序)
CREATE [OR REPLACE] FUNCTION [(PARAM...)]
RETURN IS|AS
[LOCAL DECLARATIONS]
BEGIN
EXECUTABLE STATEMENTS;
RETURN RESULT;
EXCEPTION
EXCEPTION HANDLERS;
END;
*/
-- 001 要求:创建一个函数,可以接受用户输入的学号,得到该学生的名次,输出名次。
drop table STUDENT;
CREATE TABLE STUDENT (STU_NO NUMBER(3), NAME VARCHAR2(10), SCORE NUMBER(3));
INSERT INTO STUDENT VALUES (1 , '小小', 99);
INSERT INTO STUDENT VALUES (2 , '小G', 80);
INSERT INTO STUDENT VALUES (3 , '诺诺', 98);
INSERT INTO STUDENT VALUES (4 , '小北', 79);
COMMIT;
SELECT * FROM STUDENT;
CREATE OR REPLACE FUNCTION FUNC1(SNO INT) RETURN INT
AS
v_score NUMBER;
v_mingci NUMBER;
BEGIN
SELECT SCORE INTO v_score FROM STUDENT WHERE STU_NO = SNO;
SELECT COUNT(*) INTO v_mingci FROM STUDENT WHERE SCORE > v_score;
v_mingci := v_mingci + 1;
RETURN v_mingci;
END;
SELECT FUNC1(3) FROM DUAL;
--------------------------运行---------------------------------------------
SQL> CREATE OR REPLACE FUNCTION FUNC1(SNO INT) RETURN INT
2 AS
3 v_score NUMBER;
4 v_mingci NUMBER;
5 BEGIN
6 SELECT SCORE INTO v_score FROM STUDENT WHERE STU_NO = SNO;
7 SELECT COUNT(*) INTO v_mingci FROM STUDENT WHERE SCORE > v_score;
8 v_mingci := v_mingci + 1;
9 RETURN v_mingci;
10 END;
11 /
Function created
SQL> SELECT FUNC1(3) FROM DUAL;
FUNC1(3)
----------
2