Article directory
Review of past issues
- [Oracle] One of the Oracle series – Oracle data type
- [Oracle] Oracle Series 2 – Oracle Data Dictionary
- [Oracle] Oracle series three – Oracle character set
- [Oracle] Oracle Series 4 – User Management
- [Oracle] Oracle Series 5 – Oracle Table Space
- [Oracle] Oracle Series Six – Oracle Table Partitioning
- [Oracle] Oracle Series 7 – Table Creation and Management
- [Oracle] Oracle Series 8 – SQL Query
- [Oracle] Oracle Series 9 – Oracle Common Functions
- [Oracle] Oracle Series 10 – Oracle Regular Expressions
- [Oracle] Oracle Series 11 – PL/SQL
- [Oracle] Oracle series no. 12 – views, records, synonyms, sequences
- [Oracle] Oracle Series Thirteen – Cursor
- [Oracle] Oracle Series Fourteen – Trigger
Preface
1. Basic concepts
(1) Definition
Oracle stored procedures are a set of SQL statements to complete specific functions, with input and output parameters. They are compiled and stored in the database. The user can execute them by specifying the name of the stored procedure and giving parameters (such as with parameters).
Using stored procedures has the following advantages:
Improved performance: Since stored procedures are executed on the database server, network traffic and data transfer time can be reduced. In addition, since it is compiled once and executed multiple times, it also improves the responsiveness of the application.
Protect data: Operating the database through stored procedures can prevent security issues such as SQL injection attacks and misuse.
Simplify code: Encapsulating common tasks into a single block of code and naming them stored procedures can simplify repetitive code in your application.
Maintenance convenience: If you need to change some business logic or query conditions, you only need to update a stored procedure.
(2) The difference between stored procedures and functions
Functions can only return one value, while stored procedures can return multiple values.
Functions are typically used as expressions, while stored procedures are typically called to accomplish some task.
Functions cannot modify database state, but stored procedures can.
(3) The difference between stored procedures and triggers
Triggers cannot be called explicitly, but stored procedures can.
The execution of triggers is implicit, while the execution of stored procedures is explicit.
Triggers can only be defined at the table level, while stored procedures can be defined at the database level.
2. Create and use stored procedures
(1) Create a stored procedure
CREATE OR REPLACE PROCEDURE my_procedure
AS
BEGIN
-- 执行 SQL 语句或其他任务
END;
CREATE OR REPLACE means that if the stored procedure already exists, replace it with new code.
The AS keyword indicates the beginning of the definition of the body of the stored procedure.
Stored procedures must end with END.
(2) Call the stored procedure
Use the EXECUTE or CALL command to call the stored procedure.
e.g
EXECUTE my_procedure;
or:
CALL my_procedure();
If the stored procedure requires input parameters, you can specify the parameter values within parentheses.
e.g
EXECUTE my_procedure('John', 'Doe');
(3) Stored procedure input parameters
Oracle stored procedures can receive input parameters that allow you to pass data to the program at run time. To declare an input parameter, use the IN keyword, followed by the parameter name and data type. The parameter name should be clear and easy to understand, and the data type must match the value passed.
e.g
CREATE OR REPLACE PROCEDURE my_procedure (p_name VARCHAR2, p_age NUMBER)
AS
BEGIN
-- 执行 SQL 查询或其他任务
END;
(4) Output parameters of the stored procedure
Oracle stored procedures can also return one or more results, which are called output parameters. To declare an output parameter, use the OUT keyword, followed by the parameter name and data type. Output parameters must be assigned values in the stored procedure body, and the stored procedure call must contain corresponding variables to receive the output values.
e.g
CREATE OR REPLACE PROCEDURE my_procedure (p_name VARCHAR2, p_age NUMBER, p_result OUT VARCHAR2)
AS
BEGIN
-- 执行 SQL 查询或其他任务,并将结果存储在 p_result 变量中。
END;
3. Best practices for stored procedures
eg Output the employee's name and salary based on the employee's ID
CREATE OR REPLACE PROCEDURE get_employee_info (p_emp_id IN NUMBER, p_name OUT VARCHAR2, p_salary OUT NUMBER)
AS
BEGIN
SELECT employee_name, salary INTO p_name, p_salary FROM employees WHERE employee_id = p_emp_id;
EXCEPTION
WHEN no_data_found THEN
p_name := 'Unknown';
p_salary := 0;
END;
eg Use stored procedures to generate 1 million records:
CREATE OR REPLACE Procedure FILL_TS
As
nbid1 Number(10,0);
Begin
For nbid1 In 1..10000000 Loop
Insert Into ts_insert(nbid,mc) Values(nbid1,nbid1);
End Loop;
Commit;
End;