0% found this document useful (0 votes)
22 views

2SQL in PLSQL Control Structure and Loops

Uploaded by

Lohith Seedella
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

2SQL in PLSQL Control Structure and Loops

Uploaded by

Lohith Seedella
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 11

SQL Operations, Conditional & Iterative

controls in PL/SQL
SQL Statements in PL/SQL

• Extract a row of data from the database by using the SELECT command.

• Make changes to rows in the database by using DML commands.

• INSERT
• UPDATE
• DELETE

• Control a transaction with the COMMIT, ROLLBACK, or SAVEPOINT


command.

-2 Document Name
CONFIDENTIAL
Retrieving Data in PL/SQL

• Retrieve data from the database with SELECT.


• SELECT statement must use with INTO clause and it must return exactly
one row.
SELECT select_list
INTO {variable_name[, variable_name]...
| record_name}
FROM table
WHERE condition;

• Example

DECLARE
v_orderdate ord.orderdate%TYPE;
v_shipdate ord.shipdate%TYPE;
BEGIN
SELECT orderdate, shipdate
INTO v_orderdate, v_shipdate
FROM ord
WHERE id = 620;
...
END;
-3 Document Name
CONFIDENTIAL
Manipulating Data Using PL/SQL

Make changes to database tables by using DML commands:


• INSERT
• UPDATE
• DELETE

INSERT

UPDATE

DELETE

-4 Document Name
CONFIDENTIAL
Updating and Deleting Data
• Increase the salary of all employees in the EMP table who are Analysts
Example:

DECLARE
v_sal_increase emp.sal%TYPE := 2000;
BEGIN
UPDATE emp
SET sal = sal + v_sal_increase
WHERE job = 'ANALYST';
END;

• Delete rows that belong to department 10 from the emp table.

DECLARE
DECLARE
v_deptno
v_deptno emp.deptno%TYPE
emp.deptno%TYPE :=:= 10;
10;
BEGIN
BEGIN
DELETE
DELETE FROM
FROM emp
emp
WHERE
WHERE deptno
deptno == v_deptno;
v_deptno;
END;
END;

-5 Document Name
CONFIDENTIAL
Conditional Control
• The logical flow of statements can be changed using conditional IF
statements and loop control structures.

• Conditional IF statements:
• IF-THEN-END IF
• IF-THEN-ELSE-END IF
• IF-THEN-ELSIF-END IF

Syntax Example

IF
IF condition
condition THEN
THEN IF
IF V_DEPTNO
V_DEPTNO == 10
10 THEN
THEN
statements;
statements; V_JOB := 'SALESMAN';
V_JOB := 'SALESMAN';
[ELSIF
[ELSIF condition
condition THEN
THEN ELSIF
ELSIF V_DEPTNO
V_DEPTNO == 2020 THEN
THEN
statements;]
statements;] V_JOB
V_JOB :=
:= ‘EXECUTIVE';
‘EXECUTIVE';
[ELSE
[ELSE ELSE
ELSE
statements;]
statements;] V_JOB
V_JOB :=
:= ‘MANAGER';
‘MANAGER';
END
END IF;
IF; END IF;
END IF;

-6 Document Name
CONFIDENTIAL
Iterative Control

• Loops repeat a statement or sequence of statements multiple times.

• Loop types:
• Simple loop
• FOR loop
• WHILE loop

• Loops can be Nested to multiple levels.

• Exit loop with the EXIT statement

-7 Document Name
CONFIDENTIAL
Simple Basic Loop
Syntax

LOOP -- delimiter
statement1; -- statements
. . .
EXIT [WHEN condition]; -- EXIT statement
END LOOP; -- delimiter

where: condition is a Boolean variable or


expression (TRUE, FALSE,
or NULL);

Example
DECLARE
v_ordid item.ordid%TYPE := 100;
BEGIN
FOR i IN 1..10 LOOP
INSERT INTO item(ordid, itemid)
VALUES(v_ordid, i);
END LOOP;
END;
-8 Document Name
CONFIDENTIAL
Numeric FOR Loop

• Syntax

FOR index in [REVERSE]


lower_bound..upper_bound LOOP
statement1;
statement2;
. . .
END LOOP;

• Loop index is implicitly of type NUMBER


• It is only defined within the loop
• Value may be referenced in an expression, but a new value may not be
assigned to it within the loop

Example

BEGIN
FOR i IN REVERSE 21…30 LOOP
INSERT INTO EMP_CNT(CNT)VALUES (i);
END LOOP;
END;

-9 Document Name
CONFIDENTIAL
WHILE Loop

• The sequence of statements will be repeated as long as condition evaluates


to TRUE

WHILE condition LOOP Condition is


statement1; evaluated at the
statement2; beginning of
. . . each iteration.
END LOOP;
• Example

DECLARE
ctr NUMBER (3) := 0;
BEGIN
WHILE ctr < 500 LOOP
INSERT INTO temp(message)
VALUES (‘Data inserted successfully’);
ctr := ctr +1 ;
END LOOP;
END;
-
10 Document Name
CONFIDENTIAL
Thank You

You might also like