简体   繁体   中英

Pl/Sql Oracle Function in a procedure

I need to make a procedure that needs to update the situation of a student, but I need to use a function that returns 'A' for the approved students, and 'R' for the not approved. After this I need to update the field "Situation" with an 'A' or 'R' that is returned from the function. I already have the function, but I don't have any idea how to make the procedure. Here goes the code of the function:

CREATE OR REPLACE FUNCTION check_grade(grade     IN NUMBER,
                                       frequency IN NUMBER) RETURN VARCHAR2 AS
   RESULT VARCHAR2(1) DEFAULT '';
BEGIN
   IF (grade >= 6) AND (frequency >= 0.75) THEN
      resultado := 'A';
   ELSE
      resultado := 'R';
   END IF;

   RETURN RESULT;
END;

See if this help.

create or replace procedure pr_update(v_grade in number, v_frequency in number) is
update tab_name set Situation=check_grade(v_grade,v_frequency);
end;

I think you are over complicating the things. You could do it in pure SQL rather than using PL/SQL function and procedure.

You could use CASE expression, for example:

Test 1

SQL> SELECT
  2  CASE
  3      WHEN &grade   >=6
  4      AND &frequency>=0.75
  5      THEN 'A'
  6      ELSE 'R'
  7    END
  8    FROM DUAL;
Enter value for grade:   10
old   3:     WHEN &grade   >=6
new   3:     WHEN   10   >=6
Enter value for frequency: 1
old   4:     AND &frequency>=0.75
new   4:     AND 1>=0.75

C
-
A

Test 2

SQL> SELECT
  2  CASE
  3      WHEN &grade   >=6
  4      AND &frequency>=0.75
  5      THEN 'A'
  6      ELSE 'R'
  7    END
  8    FROM DUAL;
Enter value for grade:   1
old   3:     WHEN &grade   >=6
new   3:     WHEN   1   >=6
Enter value for frequency: 0.5
old   4:     AND &frequency>=0.75
new   4:     AND 0.5>=0.75

C
-
R

So, using the same logic in your UPDATE statement:

UPDATE table_name
SET situation = (
  CASE
    WHEN &grade   >=6
    AND &frequency>=0.75
    THEN 'A'
    ELSE 'R'
  END)
WHERE situation IS NOT NULL;

Update OP only wants to do it in a procedure:

create or replace procedure pr_update
 is
begin
update table_name 
   set Situation = check_grade(grade, frequency);
end;
/

In the above procedure, put actual table_name in the update statement. grade and frequency are considered as the column names of the table.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM