Get the App
SLTechnology News&Howtos  ›  Database  › 

PL/SQL structure

Shulou Source: shulou.com Published: 2022-06-01 03:12:09 09月22日 Update

Unnamed blocks: unnamed block

Named blocks: procedures, functions, packages, and triggers

Definition section: used to define variables, constants, cursors, triggers, compound data types

Executive part: used to implement the function of the application module, including PL/SQL statements and SQL statements to be executed

Exception handling part: used to handle possible running errors in the execution part

[DECLARE

...-definition section]

BEGIN

..-- the executive part

[EXCEPTION

.-- exception handling part]

END

-

DECLARE

A NUMBER

BEGIN

ARV 1: 2

DBMS_OUTPUT.PUT_LINE ('1 / 2 / 1 / 2 / 2) | | a)

EXCEPTION

WHEN OTHERS THEN

DBMS_OUTPUT.PUT_LINE ('exception')

END

/

Conditional branching statement:

DECLARE

A1 number: = 100

A2 number: = 200

BEGIN

IF A1 > a2 THEN

DBMS_OUTPUT.PUT_LINE ('A1 is larger than a2')

ELSIF a1=a2 THEN

DBMS_OUTPUT.PUT_LINE ('A1 is equal to a2')

ELSE

DBMS_OUTPUT.PUT_LINE ('A1 is smaller than a2')

ENDIF

END

/

DECLARE

Grade CHAR (1): ='B'

Appraisal VARCHAR2 (20)

BEGIN

Appraisal: =

CASE grade

WHEN'A 'THEN' excellent'

WHEN'B' THEN 'good'

WHEN'C 'THEN'

WHEN'D' THEN 'pass'

WHEN'F'THEN 'failed'

ELSE 'does not have such a rating'

DBMS_OUTPUT.PUT_LINE ('Grade' | | grade | |' is' | | appraisal)

END

/

Loop statement:

DECLARE

Total number: = 0

Salary number: = 1800

BEGIN

LOOP

Total: = total + salary

EXIT WHEN total > 2500

END LOOP

DBMS_OUTPUT.PUT_LINE ('total salary is' | | total)

END

/

DECLARE

Total number: = 0

Salary number: = 1800

BEGIN

WHILE total

Tags: Parts statements processing triggers functions branches functions variables constants data conditions modules cursors programs levels types procedures errors applications loops Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Xiaomi OPPO Reno Shulou Information Huawei