The basic syntax for Oracle stored procedures.

The basic syntax for writing an Oracle stored procedure is as follows:

  1. Create a stored procedure.
CREATE OR REPLACE PROCEDURE procedure_name (parameter1 datatype, parameter2 datatype, ...) 
IS 
-- 可选的局部变量声明
BEGIN
-- 逻辑代码
END;
/
  1. Storage procedure parameters can be input parameters, output parameters, or input/output parameters, and are declared within parentheses after the storage procedure name.
  2. The logic code of a stored procedure can include conditional statements, loop statements, exception handling, etc.
  3. The name of the stored procedure must be specified at the time of creation and created using the CREATE OR REPLACE PROCEDURE statement.
  4. Stored procedures can be defined using the BEGIN and END keywords to indicate the beginning and end.
  5. Stored procedures can define local variables using the DECLARE keyword.
  6. 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.

bannerAds