📜  PL SQL procedure(1)

📅  最后修改于: 2023-12-03 14:45:31.520000             🧑  作者: Mango

PL/SQL Procedure Introduction

PL/SQL (Procedural Language/Structured Query Language) is a procedural language used to develop applications in Oracle Database. PL/SQL allows the use of procedural constructs such as loops and conditional statements in SQL statements.

A PL/SQL procedure is a named block of code that performs a specific task. The procedure can be called by other procedures or programs to perform the specified task. Procedures can accept input arguments and return output values.

Creating a PL/SQL Procedure

To create a PL/SQL procedure, we first need to connect to an Oracle database instance:

CONNECT username/password@hostname:port/servicename

Next, we can create a procedure using the following syntax:

CREATE OR REPLACE PROCEDURE procedure_name
  (parameter1 IN data_type, parameter2 OUT data_type, ... )
AS
BEGIN
  -- block of code
END;
/

Let's take a closer look at each part of this syntax:

  • CREATE OR REPLACE - This phrase is used to create a new procedure or update an existing one if it already exists.
  • PROCEDURE - This keyword indicates that we are creating a new procedure.
  • procedure_name - This is the name of our new procedure.
  • parameter1 IN data_type - These are the input parameters for our procedure. We can have zero or more input parameters, and each one is specified with a name, a direction (IN or OUT), and a data type.
  • parameter2 OUT data_type -These are the output parameters for our procedure. We can have zero or more output parameters, and each one is specified with a name, a direction (IN or OUT), and a data type.
  • BEGIN - This keyword indicates the start of the procedure's code block.
  • END - This keyword indicates the end of the procedure's code block.
  • / - This symbol indicates the end of the procedure definition.
Example of a PL/SQL Procedure

Let's create a simple PL/SQL procedure that adds two numbers and returns the result:

CREATE OR REPLACE PROCEDURE add_numbers
  (a IN NUMBER, b IN NUMBER, result OUT NUMBER)
AS
BEGIN
  result := a + b;
END;
/

In this example, we have three parameters: a and b which are input parameters, and result which is an output parameter. Inside the procedure, we add a and b and store the result in the result parameter.

Calling a PL/SQL Procedure

To call our add_numbers procedure, we can use the following syntax:

DECLARE
  x NUMBER := 10;
  y NUMBER := 20;
  z NUMBER;
BEGIN
  add_numbers(x, y, z);
  DBMS_OUTPUT.PUT_LINE('The result is: ' || z);
END;
/

In this example, we declare two variables x and y and assign them the values 10 and 20 respectively. Then, we call the add_numbers procedure passing x and y as input parameters, and z as an output parameter. Finally, we use the DBMS_OUTPUT.PUT_LINE procedure to print the result.

Conclusion

PL/SQL procedures are powerful constructs that allow us to encapsulate code and reuse it across multiple programs or procedures. By learning how to create and call procedures in PL/SQL, we can improve our programming skills and develop more robust applications.