Complete notes on oracle PLSQL

Source: Internet
Author: User
PLSQL anonymous block declare -- Declaration part, optional begin -- execution part, required exception -- exception, optional end; -- required Stored Procedure createprocedure name (can be written to parameters to be received) is -- declares some beginexceptionend; uses the in name (parameter) end; to use the stored procedure function create

PL/SQL anonymous block declare -- Declaration part, optional begin -- execution part, required exception -- exception, optional end; -- the required stored procedure create procedure name (which can be written to the parameter to be received) is -- declares part of begin exception end; uses the begin name (parameter) end; to use the stored procedure function create

PL/SQL
Anonymous Block
Declare -- Declaration part, optional
Begin -- execution part, required
Exception -- exception, optional
End; -- required

Stored Procedure
Create procedure name (parameters to be received can be written) is
-- Declaration Part
Begin
Exception
End;

Pass
Begin
Name (parameter)
End; to use the Stored Procedure


Function
Create function Name (variable in type) return value type is
Define Variables
Begin
Select
From
Return a value to be returned
End;

Variables and constants
If it is a constant declaration, you must add constant after the variable name
Definition method: variable name constant identifier data type of the variable [not null]: = (: = used for value assignment )......
In addition, the constant value cannot be modified, similar to final in java.

The variable name must be less than 30 characters. The first character is a letter, and the rest can be letters, numbers, and underscores (_).
A variable is usually identified by v _, and a constant is usually identified by c _.
The variable name cannot be the same as the field name.

V_ename emp. ename % TYPE indicates that the defined TYPE is the same


The type in the parameter list of stored procedures and functions cannot be set with precision. For example, number cannot be written as number (5, 2)
In addition, In out and in out In the parameter list
The three modes, In can be directly through
Begin
Name ('','','')
End; Use
However, in out and in out modes, you must assign values to the variables and place them in brackets for real parameters.
Declare
Mmm number (5, 3): = 100;
Begin
Name (mmm );
End; otherwise, an error will be reported, because the 100 may need to be modified in the process or function, so
A bucket is required, so a variable is required.


When calling a stored procedure for insert, if the parameter list contains three values: a, B, and c
And a has a default value of 123.
In this case, if I pass the value to this function, this format will be used when I pass the last two values.
Declere
Mm number (5, 2): = 10;
Nn number (5, 2): = 10;
Begin
Insert (B => mm, c => nn)
End;

At this time, we didn't input a, but it still defaults to 123. Then B and c are assigned mm and nn.


Drop function name --- delete function


Exercise questions

-- 1
Create or replace procedure ADD_EMP_09 (v_empNo emp1.EMPNO % type,
V_eName emp1.ENAME % type default 'unknown ',
V_sal emp1.SAL % type default 2000,
V_hireDate emp1.HIREDATE % type)
Begin
Insert into emp1
(Empno, ENAME, SAL, HIREDATE)
Values
(V_empNO, v_eName, v_sal, v_hireDate );
End;

Begin
ADD_EMP_09 (7988, 'nnmmk', 2332, sysdate );
End;

Begin
ADD_EMP_09 (v_empNo = & gt; 7548, v_hireDate = & gt; sysdate );
End;

-- 2
Create or replace function UPD_EMP_09 (v_empNo in emp1.EMPNO % type,
V_sal in emp1.SAL % type)
Return emp1.ENAME % type is
V_ename emp1.ENAME % type;
Begin
Update emp1
Set sal = v_sal
Where empno = v_empNo;
Select ename
Into v_ename
From emp1
Where empno = v_empNo;
Return v_ename;
End;

Begin
Dbms_output.put_line (UPD_EMP_09 (72.16,99999 ));
End;

-- 3
Create or replace function GET_ENAME_09 (v_empNo in emp1.EMPNO % type)
Return emp1.ENAME % type is
V_ename emp1.ENAME % type;
Begin
Select ename
Into v_ename
From emp1
Where empno = v_empNo;
Return v_ename;
End;
/
Begin
Dbms_output.put_line (GET_ENAME_09 (7369 ));
End;

Use show erro to view the error message.


This exercise contains the usage of the out mode. Take a closer look.

Create or replace function DUTY (v_empno emp. empno % type,
V_ename out emp. ename % type)
Return emp. sal % type is
V_duty emp. sal % type;
Begin
Select ename, sal * 0.05
Into v_ename, v_duty
From emp
Where empno = v_empno;
Return v_duty;
End;

Declare
Empno emp. empno % type: = 7499;
Ename emp. ename % type;
Begin
Dbms_output.put_line (ename | ''| DUTY (empno, ename ));
End;


Exception
Exception
When no_data_found then
V_empname: = 'no data'
When too_many_rows then
V_empname: = 'too writable rows'


Define a type

Type name is record (
Ename varchar2 (30 ),
Job emp. job % type
);
This can be defined in the variable definition, and can be used as a type by new variables.

For example, the name type name defines such a variable

If used
.....
Select ename, job
Into type name
From emp
Dbms_output.put_line (type name. ename | type name. job );
.....

Remember, the Data Type of the form parameter cannot have a length. For example, you cannot write the data as number (30). You only need to write the data as number.


Use % ROwTYPE

Usage
Where the variable is declared: dept_record dept % rowtype is consistent with the usage method defined by type,
It only contains all the fields in the table.

SQL % ROWCOUNT usage

The number of rows affected by the last statement can be obtained.


If then
.....
Elsif then
.....
Else
.....
End if;


Case
When v_aa = 'A' then...; (used when a sentence is assigned or printed; ended)
When v_bb = 'B' then .....;
Else ......;
End case;


Loop
Loop
........
Exit when
End loop;
This is similar to while in java. It is executed once first.

For I in 1 .. 10 loop increments
......
End loop;

Meaning of descending reverse

For I in reverse 1 .. 10 loop decreasing
......
End loop;

While v_num <11 loop
V_num: = v_num + 1;
Dbms_output.put_line (v_num );
End loop;

Exit the loop condition exit when .....
Or if... then exit


Note:

The division operation between integers is automatically rounded... Note This.

There are also several dbms_output.put (); after things are used together, you must add the last sentence
Dbms_output.put_line (''); otherwise, no result is returned.

If no data is returned when you use a collection function, a zero data record may also be returned, for example, using count (*)

Custom cursor

Cursor name is
Select
From

The method used to open a cursor. A custom cursor must be opened.
Method for closing a cursor close the cursor name

Fetch cursor name
Into field, field, field

You can define a table % rowtype.

Then, the rowtype defined by into can be used to store data.

The cursor pointer does not move down when it knows the last data record. If it continues printing, it only prints the last record.


The cursor name % isopen determines whether the cursor is opened.
Cursor name % notfound returns true if no data exists
Cursor name % found returns true if data exists
Cursor name % rowcount returns the number of rows in the result set


Cursor for Loop

Cursor name is
Select
From
Begin
For dept_c in cursor name loop
.....
End loop;
End;
If dept_c is a full table structure, it cannot be defined. Otherwise, a new one needs to be defined,
This is called a implicit cursor, and you do not need to write the cursor to open or close it yourself.


Parameter-based cursor

Cursor name () is
......;
When the cursor is opened, parameters are passed.
If it is a for loop, the parameters are passed in the for loop.

Lock record lines:

Add the following sentence to the complete select statement.
For update, these records are locked.
As long as you have not submitted it, it cannot be changed elsewhere.

Select
From
Order
For update
If both parties lock this record, an exception is thrown.

First, locate the data, lock the data, and update the data.

Update emp set sal = sal * 11
Where

Exception Handling

Exception during compilation
Runtime exception:
-- Two exceptions can be added after when and connected with or.
-- When others then capture all other exceptions
-- Only one of the blocks can be placed at the bottom.
-- No_data_found
-- No data is returned.
-- Too_many_rows
-- Return multiple rows of data
-- Dup_val_on_index
-- Primary key repetition exception
-- Zero_pide
-- Try to divide by zero
-- Invalid_number
-- An error occurred while converting the string to a number.

Custom exception:

Create or replace procedure DDDD (v_empno emp. empno % type) is
Aaa emp. empno % type;
E_a exception; -- Define an exception
Begin
Select ename
Into aaa
From emp
Where empno = v_empno;

If aaa = 'sss' then
Raise e_a; -- raise is an exception-triggered statement.
End if;
Exception
When e_a then
.......;
End;


If there is no predefined name, only the error code can use the following method:

Pragma exception_init (The Exception name and error code defined previously );
-- When this error code occurs, go to the Exception name you defined in the when below.

Sqlcode: Used to get error code
Sqlerrm: Used to get error information

Custom exception message:
-- Raise_app ........

Now let's take out the begin and end and nest it. If an exception occurs in the internal declaration or an exception thrown in the internal exception,
That is to say, an exception occurs in declare or an exception thrown through raise in the exception,
He will only seek to solve exceptions in the outer layer and will not handle exceptions in the internal layer.


Baotou:
Create or replace package name is
Declare functions, variables, etc.
Procedure process name (...);
End;

You can use the package name, process name, and function name to call the function.

Baotou is public and can be used outside,
The package body does not work.

Package body:
Create or repalce package body package name is
Write Process
Function
Stored procedures ..
End package name;
It is worth noting that the declared items in this package are private.
The package body can be overloaded.
The overload must have different parameter types or numbers. Note that char and varchar are of the same series and cannot be of different types.

Drop package name Delete Baotou and package body
Drop package body package name Delete package

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.