Variable 29• Assigning SQL query results to PL/SQL Variables: ➢ Use SELECT INTO statement... Variable 30• Assigning SQL query results to PL/SQL Variables: ➢ Use SELECT INTO statement...
Trang 2PL/SQL Statements
in Oracle
Group 5: Tạ Văn Vượng -1614189
Huỳnh Hoàng Kha – 1511449
Lê Đức Trung – 1613786 Châu Thành Đạt – 1710934 Nguyễn Ngọc Duy Phong - 1712617
Trang 3• Introduction to PL/SQL
• Syntax and Data types
• Constants and variable
• PL/SQL statements
• Print messages, raise errors
• Review questions and Exercises
3
Trang 4Introduction to PL/SQL
4
1.
Trang 5Introduction to PL/SQL
• Oracle
5
Trang 6Introduction to PL/SQL
• Oracle Database
6
Trang 7Introduction to PL/SQL
• Oracle Database
7
Trang 8Introduction to PL/SQL
• Procedural Language/Structured Query Language).
8
Trang 9Introduction to PL/SQL
• Procedural Language/Structured Query Language).
• Procedural Language extension of SQL.
9
Trang 10Introduction to PL/SQL
• Procedural Language/Structured Query Language).
• Procedural Language extension of SQL.
• Environment: Oracle Database
10
Trang 11Introduction to PL/SQL
• PL/SQL Developer:
11
Trang 12Syntax and Data types
12
2.
Trang 13Syntax and Data types
Trang 14Syntax and Data types
• Syntax of block:
14
Trang 15Syntax and Data types
• Symbols:
15
Trang 16Syntax and Data types
Trang 17Syntax and Data types
Trang 18Syntax and Data types
Trang 19Syntax and Data types
Trang 20Syntax and Data types
Trang 21Syntax and Data types
• Scalar:
➢ Datetime:
▪ Example: “01-OCT-12” – “DD-MON-YY”
21
Trang 22Syntax and Data types
Trang 23Syntax and Data types
• User-defined:
23
Trang 24Constants and variable
24
3.
Trang 25Variable 25
• Variable declaration:
➢ specific data type
➢ variable names are not case-sensitive, not
exceed 30 characters
➢ cannot use a reserved PL/SQL keyword
Trang 27Variable 27
• Initializing Variables:
➢ The DEFAULT keyword
greetings varchar2(20) DEFAULT 'Have a Good Day';
➢ The assignment operator
counter binary_integer := 0;
Trang 28Variable 28
• Variables Scope:
➢ Local variables
➢ Global variables
Trang 29Variable 29
• Assigning SQL query results to PL/SQL Variables:
➢ Use SELECT INTO statement
Trang 30Variable 30
• Assigning SQL query results to PL/SQL Variables:
➢ Use SELECT INTO statement
Trang 31Constant 31
• Declaring a constant:
Ex:
PI CONSTANT NUMBER := 3.141592654;
Trang 32PL/SQL statements
32
4.
Trang 33PL/SQL Statements 33
Trang 35IF – THEN 35
IF condition THEN
S;
END IF;
Condition: a Boolean or relational condition
S: a simple or compound statement
Trang 36IF – THEN 36
IF (a <= 20) THEN
c:= c+1;
END IF;
Trang 37Condition: a Boolean or relational condition
S1, S2: a simple or compound statement
Trang 38IF – THEN – ELSE 38
IF color = red THEN
dbms_output.put_line(‘Oh yeah RED')
ELSE
dbms_output.put_line(‘Why not RED');
END IF;
Trang 39IF – THEN – ELSIF 39
IF(boolean_expression 1)THEN
S1; Executes when the boolean expression 1 is true
ELSIF( boolean_expression 2) THEN
S2; Executes when the boolean expression 2 is true
ELSIF( boolean_expression 3) THEN
S3; Executes when the boolean expression 3 is true
ELSE
S4; executes when the none of the above condition is true END IF;
Trang 41CASE Statement 41
CASE selector
WHEN 'value1' THEN S1;
WHEN 'value2' THEN S2;
WHEN 'value3' THEN S3;
ELSE Sn; default case
END CASE;
Trang 42CASE Statement 42
CASE grade
when 'A' then dbms_output.put_line('Excellent');
when 'B' then dbms_output.put_line('Very good');
when 'C' then dbms_output.put_line('Well done');
when 'D' then dbms_output.put_line('You passed');
when 'F' then dbms_output.put_line('Better try again'); else dbms_output.put_line('No such grade');
END CASE;
Trang 43Searched CASE Statement 43
CASE
WHEN selector = 'value1' THEN S1;
WHEN selector = 'value2' THEN S2;
WHEN selector = 'value3' THEN S3;
ELSE Sn; default case
END CASE;
Trang 44Searched CASE Statement 44
case
when grade = 'A' then dbms_output.put_line('Excellent');
when grade = 'B' then dbms_output.put_line('Very good');
when grade = 'C' then dbms_output.put_line('Well done');
when grade = 'D' then dbms_output.put_line('You passed');
when grade = 'F' then dbms_output.put_line('Better try again'); else dbms_output.put_line('No such grade');
end case;
Trang 45• Loop control statements:
• Exit (and exit when)
• Continue
• Goto
Trang 46PL/SQL Basic Loop 46
LOOP
Sequence of statements;
END LOOP;
Trang 49PL/SQL While loop 49
WHILE condition LOOP
sequence_of_statements
END LOOP;
Trang 52PL/SQL For loop 52
FOR a in 10 20 LOOP
dbms_output.put_line('value of a: ' || a);
END LOOP;
Trang 53Loops may be nested also 53
WHILE condition1 LOOP
Trang 54Cursor 54
• A cursor is a pointer to a context area.
• A cursor holds the rows (one or more) returned by a SQL statement.
• The set of rows the cursor holds is referred to as the
active set.
• There are implicit cursors and explicit cursors
Trang 55Implicit and explicit cursor 55
• Implicit cursor is automatically created whenever an
SQL statement is executed (INSERT, UPDATE, and
DELETE), when there is no explicit cursor for the
statement Any SQL cursor attribute will be accessed as sql%attribute_name
• Explicit cursors are programmer-defined cursors for
gaining more control over the context area It is created
on a SELECT Statement which returns more than one
row.
Trang 58Opening the Cursor 58
Opening the cursor allocates the memory for the
cursor and makes it ready for fetching the rows
returned by the SQL statement into it
OPEN c_customers;
Trang 59Fetching the Cursor 59
Fetching the cursor involves accessing one row at a
time
FETCH c_customers INTO c_id, c_name, c_addr;
Trang 60Closing the Cursor 60
Closing the cursor means releasing the allocated
memory
CLOSE c_customers;
Trang 61Cursor using example 61
Trang 62Cursor using example 62
BEGIN
OPEN c_customers;
LOOP
FETCH c_customers into c_id, c_name, c_addr;
EXIT WHEN c_customers%notfound;
dbms_output.put_line(c_id || ' ' || c_name || ' ' || c_addr); END LOOP;
CLOSE c_customers;
END;
Trang 63Print messages, raise errors
63
5.
Trang 64Print messages, raise errors
• Print message:
➢ Syntax:
DBMS_OUTPUT.PUT_LINE(‘messages’)
64
Trang 65Print messages, raise errors
• Print message:
➢ Example:
65
Trang 66Print messages, raise errors
• Exception:
➢ Syntax:
66
Trang 67Print messages, raise errors
• Exception:
67
Trang 68Print messages, raise errors
• Two types of exception:
➢ System-defined exceptions
➢ User-defined exceptions
68
Trang 69Print messages, raise errors
Trang 70Print messages, raise errors
• User-defined exception:
➢ Syntax:
70
Trang 71Print messages, raise errors
• Raising Exceptions:
➢ Syntax:
71
Trang 72Print messages, raise errors
• Example:
72
Trang 73Review questions and Exercises
73
6.
Trang 74Exercises
• Write a PL/SQL block to differenciate between
CHAR and VARCHAR2 datatype
Trang 75Exercises
Trang 76Exercises
• Write a PL/SQL block display the discription
against a grade using CASE statement
Trang 77Exercises
Trang 78Exercises
Trang 79Exercises
• Write a PL/SQL block to print numbers from 0 to 9 using LOOP and FOR LOOP
Trang 80Exercises
LOOP
Trang 81Exercises
LOOP
Trang 82Exercises
FOR LOOP
Trang 83Exercises
FOR LOOP
Trang 84Exercises
• Write a PL/SQL block to check whether a number
is positive, negative or zero
Trang 85Exercises
Trang 86Exercises
Trang 87Exercises
• Write a PL/SQL block to check whether a number
is prime or not
Trang 88Exercises
Trang 89Exercises