0% found this document useful (0 votes)
31 views10 pages

Solutions

1) This document contains examples of PL/SQL code blocks demonstrating basic programming concepts like variables, data types, conditional statements, loops, and procedures. 2) Examples include declaring and initializing variables, if/else statements, case statements, for loops to iterate over a range of numbers, and while loops. 3) Each code block is executed using the SQL command-line interface and outputs the results.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views10 pages

Solutions

1) This document contains examples of PL/SQL code blocks demonstrating basic programming concepts like variables, data types, conditional statements, loops, and procedures. 2) Examples include declaring and initializing variables, if/else statements, case statements, for loops to iterate over a range of numbers, and while loops. 3) Each code block is executed using the SQL command-line interface and outputs the results.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

ISLAMIC UNIVERSITY OF TECHNOLOGY

Organization of Islamic Cooperation

Board Bazar, Gazipur

Lab 06

CSE 4508

Md. Mohsinul Kabir


5.0.1 Block

Empty Block

BEGIN
NULL;
END;
/
SQL

Output:

PL/SQL procedure successfully completed.


TEXT

Hello World

SET SERVEROUTPUT ON SIZE 1000000


BEGIN
dbms_output.put_line('Hello World.');
END;
/
SQL

Output:

Hello World.

PL/SQL procedure successfully completed.


TEXT
scanf

DECLARE
my_var VARCHAR2(30);
BEGIN
my_var := '&i';
dbms_output.put_line('Hello ' || my_var);
END;
/
SQL

Output:

Enter value for i: a


old 4: my_var := '&i';
new 4: my_var := 'a';
Hello a

PL/SQL procedure successfully completed.


TEXT

Difference between char and varchar2

DECLARE
c CHAR(32767) := 'hello';
v VARCHAR2(32767) := 'hello';
BEGIN
dbms_output.put_line('c is [' || LENGTH(c) || ']') ;
dbms_output.put_line('v is [' || LENGTH(v) || ']') ;
v := v || ' ';
dbms_output.put_line ('v is [' || LENGTH(v) || ']');
END;
/
SQL
Output:

c is [32767]
v is [5]
v is [6]

PL/SQL procedure successfully completed.


TEXT

Records

DECLARE
TYPE demo_record_type IS RECORD
(id NUMBER DEFAULT 1, value VARCHAR2(10) := 'One');
demo DEMO_RECORD_TYPE;
BEGIN
dbms_output.put_line('[' || demo.id || '] [' || demo.value || ']');
END;
/
SQL

Output:

[1] [One]

PL/SQL procedure successfully completed.


TEXT
IF Statements

DECLARE
X NUMBER;
BEGIN
X := 10;
IF (X = 0) THEN
dbms_output.put_line('The value of x is 0');
ELSIF(X between 1 and 10) THEN
dbms_output.put_line('The value of x is between 1 and 10');
ELSE
dbms_output.put_line('The value of x is greater than 10');
END IF;
END;
/
SQL

Output:

The value of x is between 1 and 10

PL/SQL procedure successfully completed.


TEXT

Simple CASE Statement

DECLARE
selector NUMBER := 1;
BEGIN
CASE selector
WHEN 0 THEN
dbms_output.put_line('Case 0!');
WHEN 1 THEN
dbms_output.put_line('Case 1!');
ELSE
dbms_output.put_line('No match!');
END CASE;
END;
/
SQL
Output:

Case 1!

PL/SQL procedure successfully completed.


TEXT

LOOP and EXIT Statements

DECLARE
x number := 10;
BEGIN
LOOP
dbms_output.put_line(x);
x := x + 10;
IF x > 50 THEN
exit;
END IF;
END LOOP;
-- after exit, control resumes here
dbms_output.put_line('After Exit x is: ' || x);
END;
/
SQL

Output:

10
20
30
40
50
After Exit x is: 60

PL/SQL procedure successfully completed.


TEXT
LOOP and EXIT WHEN Statements

DECLARE
x number := 10;
BEGIN
LOOP
dbms_output.put_line(x);
x := x + 10;
exit WHEN x > 50;
END LOOP;
-- after exit, control resumes here
dbms_output.put_line('After Exit x is: ' || x);
END;
/
SQL

Output:

10
20
30
40
50
After Exit x is: 60
PL/SQL procedure successfully completed.
TEXT

FOR Loops

DECLARE
a number(2);
BEGIN
FOR a in 10 .. 20 LOOP
dbms_output.put_line('value of a: ' || a);
END LOOP;
END;
/
SQL
Output:

value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19
value of a: 20

PL/SQL procedure successfully completed.


TEXT

Reverse FOR Loops

DECLARE
a number(2);
BEGIN
FOR a IN REVERSE 10 .. 20 LOOP
dbms_output.put_line('value of a: ' || a);
END LOOP;
END;
/
SQL
Output:

value of a: 20
value of a: 19
value of a: 18
value of a: 17
value of a: 16
value of a: 15
value of a: 14
value of a: 13
value of a: 12
value of a: 11
value of a: 10

PL/SQL procedure successfully completed.


TEXT

WHILE Loop

DECLARE
a number(2) := 10;
BEGIN
WHILE a < 20 LOOP
dbms_output.put_line('value of a: ' || a);
a := a + 1;
END LOOP;
END;
/
SQL
Output:

value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19

PL/SQL procedure successfully completed.


TEXT

You might also like