The basic syntax for Oracle stored procedures.
The basic syntax for writing an Oracle stored procedure is as follows:
- Create a stored procedure.
CREATE OR REPLACE PROCEDURE procedure_name (parameter1 datatype, parameter2 datatype, ...)
IS
-- 可选的局部变量声明
BEGIN
-- 逻辑代码
END;
/
- Storage procedure parameters can be input parameters, output parameters, or input/output parameters, and are declared within parentheses after the storage procedure name.
- The logic code of a stored procedure can include conditional statements, loop statements, exception handling, etc.
- The name of the stored procedure must be specified at the time of creation and created using the CREATE OR REPLACE PROCEDURE statement.
- Stored procedures can be defined using the BEGIN and END keywords to indicate the beginning and end.
- Stored procedures can define local variables using the DECLARE keyword.
- Stored procedures can utilize both SQL statements and PL/SQL statements.
For example, here is an example of a simple stored procedure:
CREATE OR REPLACE PROCEDURE calculate_sum (num1 NUMBER, num2 NUMBER, sum OUT NUMBER)
IS
BEGIN
sum := num1 + num2;
DBMS_OUTPUT.PUT_LINE('The sum of ' || num1 || ' and ' || num2 || ' is ' || sum);
END;
/
This stored procedure takes two input parameters, num1 and num2, calculates their sum, stores the result in the output parameter sum, and finally uses the DBMS_OUTPUT.PUT_LINE statement to output the result to the console.