zoukankan      html  css  js  c++  java
  • PLSQL_结构、存储过程、函数

    /**************************************002 PLSQL结构、存储过程、函数 *****************************************/
    /**
    [DECLARE declarations]
    BEGIN
    executable statements
    [EXCEPTION handlers]
    END;
    */
    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)
    */

    -- 001 in example
    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;

    -- 002 out example

    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;

    -- 003 in out example

    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;

    /**
    函数:(函数是可以返回值的命名的PL/SQL 子程序)
    CREATE [OR REPLACE] FUNCTION <FUNCTION NAME> [(PARAM...)]
    RETURN <DATATYPE> IS|AS
    [LOCAL DECLARATIONS]
    BEGIN
    EXECUTABLE STATEMENTS;
    RETURN RESULT;
    EXCEPTION
    EXCEPTION HANDLERS;
    END;
    */

    -- 001 要求:创建一个函数,可以接受用户输入的学号,得到该学生的名次,输出名次。
    /**
    -- 实验准备
    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;

  • 相关阅读:
    JAVA地址通过百度地图API转化为经纬度
    JS验证手机号
    JAVA获取六位随机数
    JS判断是否为“YYYYMMDD”式的日期
    JAVA 通过url下载图片保存到本地
    JAVA汉字转拼音
    JS判断是否是苹果系统(ios)和安卓系统(Android)客户端
    JAVA获取请求链接中所有参数(GET请求)
    如何高效学习 Kubernetes 知识图谱?
    当云原生遇到混合云:如何实现“求变”与“求稳”的平衡
  • 原文地址:https://www.cnblogs.com/bymcyc/p/8888783.html
Copyright © 2011-2022 走看看