PL/SQL stored procedures

--The difference between custom functions and stored procedures:

1. Stored procedures are powerful and can perform a series of operations such as modifications, such as UPDATE / INSERT / DELETE. Custom functions are not allowed. 2.
Stored procedures can return zero or more records, while custom functions A value must be returned, and only one value can be returned.
3. The return value of a stored procedure cannot be directly referenced, while the return value of a custom function can be directly referenced.
4. A stored procedure is executed independently using the BEGIN END statement, and Custom functions, called in query statements (DQL) and operations (DML).

 --Syntax of stored procedure:
CREATE OR REPLACE PROCEDURE 存储过程名 (参数1 in 参数类型1,参数2 in 参数类型2.....) -- 入参 是 IN ,出参 是 out
IS /*AS*/
声明变量或者游标
BEGIN
  
-- 执行体/逻辑体

END;

--The stored procedure does not need to set parameters 

Case: Make a backup table EMP_TEST777 for EMP
-- Create a stored procedure and call the stored procedure, and achieve the following effects:
-- Pass in an employee number through the stored procedure, determine the employee's corresponding salary based on the employee number, and then provide him with a bonus
SAL <1000 COMM+300
1000-2000 COMM+200
>2000 COMM +10000

CREATE TABLE EMP_TEST777 AS SELECT * FROM EMP;

CREATE OR REPLACE PROCEDURE SP_UPCOMM(P_EMPNO NUMBER) 

IS
  V_SAL NUMBER;

BEGIN
  -- 查询薪资放入到 变量中 
  SELECT SAL INTO V_SAL FROM EMP WHERE EMPNO = P_EMPNO;

  IF V_SAL < 1000 THEN
    UPDATE EMP_TEST777 SET COMM = NVL(COMM, 0) + 300 WHERE EMPNO = P_EMPNO;
  ELSIF V_SAL BETWEEN 1000 AND 2000 THEN
    UPDATE EMP_TEST777 SET COMM = NVL(COMM, 0) + 200 WHERE EMPNO = P_EMPNO;
  ELSE
    UPDATE EMP_TEST777
       SET COMM = NVL(COMM, 0) + 10000
     WHERE EMPNO = P_EMPNO;
  END IF;
END SP_UPCOMM;
/

--Call stored procedure

BEGIN
SP_UPCOMM(7369);
END;

SELECT * FROM EMP_TEST777;

Guess you like

Origin blog.csdn.net/weixin_57024726/article/details/133291776