0% found this document useful (0 votes)
45 views6 pages

Chapter 12 13

The document discusses different types of IF-THEN conditional statements in Oracle including IF-THEN, IF-THEN-ELSE, and IF-THEN-ELSIF-ELSE. It also covers techniques for limiting and paginating query results, including using the ROWNUM clause and ROW_NUMBER function. The final section discusses using recursive sub-queries with the WITH clause to split delimited strings and generate integer sequences.

Uploaded by

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

Chapter 12 13

The document discusses different types of IF-THEN conditional statements in Oracle including IF-THEN, IF-THEN-ELSE, and IF-THEN-ELSIF-ELSE. It also covers techniques for limiting and paginating query results, including using the ROWNUM clause and ROW_NUMBER function. The final section discusses using recursive sub-queries with the WITH clause to split delimited strings and generate integer sequences.

Uploaded by

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

Chapter 12: IF-THEN-ELSE Statement

Section 12.1: IF-THEN

DECLARE

v_num1 NUMBER(10);

v_num2 NUMBER(10);

BEGIN

v_num1 := 2;

v_num2 := 1;

IF v_num1 > v_num2 THEN

DBMS_OUTPUT.put_line('v_num1 is bigger than v_num2');

END IF;

END;

Section 12.2: IF-THEN-ELSE

DECLARE

v_num1 NUMBER(10);

v_num2 NUMBER(10);

BEGIN

v_num1 := 2;

v_num2 := 10;

IF v_num1 > v_num2 THEN

DBMS_OUTPUT.put_line('v_num1 is bigger than v_num2');

ELSE

DBMS_OUTPUT.put_line('v_num1 is NOT bigger than v_num2');

END IF;

END;

Section 12.3: IF-THEN-ELSIF-ELSE

DECLARE

v_num1 NUMBER(10);

v_num2 NUMBER(10);
BEGIN

v_num1 := 2;

v_num2 := 2;

IF v_num1 > v_num2 THEN

DBMS_OUTPUT.put_line('v_num1 is bigger than v_num2');

ELSIF v_num1 < v_num2 THEN

DBMS_OUTPUT.put_line('v_num1 is NOT bigger than v_num2');

ELSE

DBMS_OUTPUT.put_line('v_num1 is EQUAL to v_num2');

END IF;

END;

GoalKicker.com – Oracle® Database Notes for Professionals 39

Chapter 13: Limiting the rows returned by

a query (Pagination)

Section 13.1: Get first N rows with row limiting clause

The FETCH clause was introduced in Oracle 12c R1:

SELECT val

FROM mytable

ORDER BY val DESC

FETCH FIRST 5 ROWS ONLY;

An example without FETCH that works also in earlier versions:

SELECT * FROM (

SELECT val

FROM mytable

ORDER BY val DESC

) WHERE ROWNUM <= 5;

Section 13.2: Get row N through M from many rows (before

Oracle 12c)

Use the analytical function row_number():


WITH t AS (

SELECT col1

, col2

, ROW_NUMBER() over (ORDER BY col1, col2) rn

FROM TABLE

SELECT col1

, col2

FROM t

WHERE rn BETWEEN N AND M; -- N and M are both inclusive

Oracle 12c handles this more easily with OFFSET and FETCH.

Section 13.3: Get N numbers of Records from table

We can limit no of rows from result using rownum clause

SELECT * FROM

SELECT val FROM mytable

) WHERE rownum<=5

If we want first or last record then we want order by clause in inner query that will give result based on
order.

Last Five Record :

SELECT * FROM

SELECT val FROM mytable ORDER BY val DESC

GoalKicker.com – Oracle® Database Notes for Professionals 40

) WHERE rownum<=5

First Five Record

SELECT * FROM

SELECT val FROM mytable ORDER BY val


) WHERE rownum<=5

Section 13.4: Skipping some rows then taking some

In Oracle 12g+

SELECT Id, Col1

FROM TableName

ORDER BY Id

OFFSET 20 ROWS FETCH NEXT 20 ROWS ONLY;

In earlier Versions

SELECT Id,

Col1

FROM (SELECT Id,

Col1,

ROW_NUMBER() over (ORDER BY Id) RowNumber

FROM TableName)

WHERE RowNumber BETWEEN 21 AND 40

Section 13.5: Skipping some rows from result

In Oracle 12g+

SELECT Id, Col1

FROM TableName

ORDER BY Id

OFFSET 5 ROWS;

In earlier Versions

SELECT Id,

Col1

FROM (SELECT Id,

Col1,

ROW_NUMBER() over (ORDER BY Id) RowNumber

FROM TableName)

WHERE RowNumber > 20


Section 13.6: Pagination in SQL

SELECT val

FROM (SELECT val, ROWNUM AS rnum

FROM (SELECT val

FROM rownum_order_test

ORDER BY val)

WHERE ROWNUM <= :upper_limit)

WHERE rnum >= :lower_limit ;

GoalKicker.com – Oracle® Database Notes for Professionals 41

this way we can paginate the table data , just like web serch page

GoalKicker.com – Oracle® Database Notes for Professionals 42

Chapter 14: Recursive Sub-Query

Factoring using the WITH Clause (A.K.A.

Common Table Expressions)

Section 14.1: Splitting a Delimited String

Sample Data:

CREATE TABLE table_name ( VALUE VARCHAR2(50) );

INSERT INTO table_name ( VALUE ) VALUES ( 'A,B,C,D,E' );

Query:

WITH items ( list, item, lvl ) AS (

SELECT VALUE,

REGEXP_SUBSTR( VALUE, '[^,]+', 1, 1 ),

FROM table_name

UNION ALL

SELECT VALUE,

REGEXP_SUBSTR( VALUE, '[^,]+', 1, lvl + 1 ),

lvl + 1

FROM items
WHERE lvl < REGEXP_COUNT( VALUE, '[^,]+' )

SELECT * FROM items;

Output:

LIST ITEM LVL

--------- ---- ---

A,B,C,D,E A 1

A,B,C,D,E B 2

A,B,C,D,E C 3

A,B,C,D,E D 4

A,B,C,D,E E 5

Section 14.2: A Simple Integer Generator

Query:

WITH generator ( VALUE ) AS (

SELECT 1 FROM DUAL

UNION ALL

SELECT VALUE + 1

FROM generator

WHERE VALUE < 10

SELECT VALUE

FROM generator;

Output:

You might also like