Document directory
- 1. What is PL/SQL?
- 2. Advantages of PL/SQL
- 3. Simple PL/SQL Classification
- 4. PL/SQL programming specifications
- 5. PL/SQL block (bloc) Introduction
- 6. instance 1: contains only PL/SQL blocks for execution.
PL/SQL programming
Objectives:
1. Understand PL/SQL concepts
2. Master PL/SQL programming technology, including programming process, functions, triggers, etc.
I. Basic PL/SQL Introduction What is 1.pl/ SQL?
PL/SQL (Procedural Language/SQL) is an extension of Oracle in the standard SQL language. PL/SQL not only allows embedding SQL languages, but also allows you to customize variables and constants. conditional statements and cyclic statements can be used to handle various errors with exceptions, this makes its functions more powerful.
2. Advantages of PL/SQL
1) Improve Application Performance
2) modular design concept [paging process, order process, transfer process, etc.]
3) reduce network transmission volume
4) Improve Security
Disadvantages of PL/SQL
1) Poor portability
3. Case studies 1. Create a simple table Create TableMytest(NameVarchar2 (30),PasswordVarchar (30)); 2. insert records to the table Create Or Replace ProcedureSp_pro1Is Begin -- Execution part Insert IntoMytest(Name,Password) Values ('Test','123'); End; If it is executed in sqlplus, add a slash to the last line/Indicates that the process is completed. How to call this process? ①ExecProcess name(Param list); ②CallProcess name(Param list); |
3. Simple PL/SQL Classification
4. PL/SQL programming specifications
1) annotations
① Single line comment :--
Select * from EMP where ename = 'Scott '; -- Query employee information
② Multi-line comment
/* This is comments */
2) variable naming rules
① Define the variable. We recommend that you use V _ as the prefix: v_sal
② Define constants. We recommend that you use C _ as the prefix: c_rate
③ Define the cursor. We recommend that you use _ cursor as the Suffix: emp_cursor.
④ Definition exception. We recommend that you use E _ as the prefix: e_error
5. PL/SQL block (bloc) Introduction
Blocks are the basic program units of PL/SQL. Compiling PL/SQL programs is actually compiling PL/SQL blocks. To complete relatively simple application functions, you may only need to write a simple block. However, to implement complex functions, you may need to nest other blocks in a PL/SQL block.
Block Structure PL/SQL consists of three parts: the definition part, the execution part, and the exception handling part. As follows: Declare /* Definition section-Defines variables, constants, cursors, exceptions, complex data types, and so on */ Begin /* Execution part-the PL/SQL statements to be executed and SQL statements */ Exception /* Exception Handling part -- handle various running errors */ End; Note: 1. The definition part starts with declare and is optional; 2. The execution part starts from begin and is required; 3. Exception Handling starts with exception, which is optional. |
6. instance 1: contains only PL/SQL blocks for execution.
SetServeroutputOn-- Enable output options Begin Dbms_output.Putline('Hello world.'); End; Note: dbms_output is a package provided by Oracle. This package contains some processes. put_line is a built-in process of this package. |
Instance 2: PL/SQL blocks that contain the definition part and execution part
Declare V_enameVarchar2 (5);-- Set string variables V_salNumber (7,2); Begin SelectEname, SalIntoV_ename, v_salFromEMPWhereEmpno= & No; -- & Indicates the variable to be received from the console. the pop-up box appears. Dbms_output.Putline('Employee name :'|V_ename|', Salary :'|V_sal); -- String Connection Symbol | End; |
Example 3: a PL/SQL block that includes the definition part and execution part and exception handling.
To avoid PL/SQL program running errors and improve PL/SQL robustness, it is necessary to handle possible errors:
① For example, in instance 2, if an employee number does not exist, the exception should be handled.
② Sometimes an error occurs and another logic is required for processing.
Note: Oracle predefines some exceptions. The no_data_found exception means that data cannot be found.