PL/SQL syntax

Source: Internet
Author: User
Tags goto

1. Branching structure

In PL/SQL, the IF keyword is used as the starting segment of the program for the point structure.

There are several branching structures in general:

1) If Condition then statement end if;

2) If Condition then statement

Else then statement

End If;

3) If Condition then statement

elsif condition Then statement

...

Else then statement

End If;

The structure corresponding to the if () {},if () {}else () {},if () {}else if () {}...else{} in other languages

In addition, PL/SQL also provides the case keyword as an identifying segment of the branching structure.

There are several ways to use case:

1) Single-value matching for a field

Case Column1

When value1 then statement

When value2 then statement

...

End case;

This structure is similar to the switch structure in other languages.

2) Use case: When structure to express if: else conditional syntax

Case

When condition then statement

When condition then statement

...

End case;

2. Cyclic structure

In PL/SQL, the specific loop structure has the following types:

1) for

For i in Rs.first. Rs.last Loop

end Loop;

This structure is concise and efficient when traversing the collection that the cursor points to, which enables the automatic management of cursors: including cursors open, close, and auto Fetch.

Example:

Declare

cursor C_emp is

select * from EMP;

Emp_record Emp%rowtype;

begin

For Emp_record in C_emp

Loop

Dbms_output.put_line (emp_record.empno);

end Loop;

end;

This PL/SQL program does not open a cursor, does not use a FETCH statement, and does not close the cursor, but uses the for. The in structure automates these tasks automatically, so it is simple, efficient and error-prone.

2) Loop when

Loop

exit when (statement)

end Loop;

3) Loop while

While (statement)

Loop

end Loop;

4) Goto

Loop

if (statement) then goto label_name

End If;

end Loop;

<<label_name>>

In PL/SQL, you can use the form <<label_name>> to define a label that uses the GOTO keyword to jump to the label as it executes, thus ending the loop body.



This article from "Notdebug" blog, reproduced please contact the author!

PL/SQL syntax

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.