1. Trang chủ
  2. » Tất cả

Plsql in oracle

90 2 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề PL/SQL Statements in Oracle
Tác giả Tạ Văn Vượng, Huỳnh Hoàng Kha, Lê Đức Trung, Châu Thành Đạt, Nguyễn Ngọc Duy Phong
Người hướng dẫn Ms. Trương Quỳnh Chi
Trường học Ho Chi Minh University of Technology
Chuyên ngành Computer Science and Engineering
Thể loại Bài tập lớn
Thành phố Ho Chi Minh
Định dạng
Số trang 90
Dung lượng 1,04 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

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 2

PL/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 4

Introduction to PL/SQL

4

1.

Trang 5

Introduction to PL/SQL

• Oracle

5

Trang 6

Introduction to PL/SQL

• Oracle Database

6

Trang 7

Introduction to PL/SQL

• Oracle Database

7

Trang 8

Introduction to PL/SQL

• Procedural Language/Structured Query Language).

8

Trang 9

Introduction to PL/SQL

• Procedural Language/Structured Query Language).

• Procedural Language extension of SQL.

9

Trang 10

Introduction to PL/SQL

• Procedural Language/Structured Query Language).

• Procedural Language extension of SQL.

• Environment: Oracle Database

10

Trang 11

Introduction to PL/SQL

• PL/SQL Developer:

11

Trang 12

Syntax and Data types

12

2.

Trang 13

Syntax and Data types

Trang 14

Syntax and Data types

• Syntax of block:

14

Trang 15

Syntax and Data types

• Symbols:

15

Trang 16

Syntax and Data types

Trang 17

Syntax and Data types

Trang 18

Syntax and Data types

Trang 19

Syntax and Data types

Trang 20

Syntax and Data types

Trang 21

Syntax and Data types

• Scalar:

➢ Datetime:

▪ Example: “01-OCT-12” – “DD-MON-YY”

21

Trang 22

Syntax and Data types

Trang 23

Syntax and Data types

• User-defined:

23

Trang 24

Constants and variable

24

3.

Trang 25

Variable 25

• Variable declaration:

➢ specific data type

➢ variable names are not case-sensitive, not

exceed 30 characters

➢ cannot use a reserved PL/SQL keyword

Trang 27

Variable 27

• Initializing Variables:

➢ The DEFAULT keyword

greetings varchar2(20) DEFAULT 'Have a Good Day';

➢ The assignment operator

counter binary_integer := 0;

Trang 28

Variable 28

• Variables Scope:

➢ Local variables

➢ Global variables

Trang 29

Variable 29

• Assigning SQL query results to PL/SQL Variables:

➢ Use SELECT INTO statement

Trang 30

Variable 30

• Assigning SQL query results to PL/SQL Variables:

➢ Use SELECT INTO statement

Trang 31

Constant 31

• Declaring a constant:

Ex:

PI CONSTANT NUMBER := 3.141592654;

Trang 32

PL/SQL statements

32

4.

Trang 33

PL/SQL Statements 33

Trang 35

IF – THEN 35

IF condition THEN

S;

END IF;

Condition: a Boolean or relational condition

S: a simple or compound statement

Trang 36

IF – THEN 36

IF (a <= 20) THEN

c:= c+1;

END IF;

Trang 37

Condition: a Boolean or relational condition

S1, S2: a simple or compound statement

Trang 38

IF – 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 39

IF – 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 41

CASE Statement 41

CASE selector

WHEN 'value1' THEN S1;

WHEN 'value2' THEN S2;

WHEN 'value3' THEN S3;

ELSE Sn; default case

END CASE;

Trang 42

CASE 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 43

Searched 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 44

Searched 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 46

PL/SQL Basic Loop 46

LOOP

Sequence of statements;

END LOOP;

Trang 49

PL/SQL While loop 49

WHILE condition LOOP

sequence_of_statements

END LOOP;

Trang 52

PL/SQL For loop 52

FOR a in 10 20 LOOP

dbms_output.put_line('value of a: ' || a);

END LOOP;

Trang 53

Loops may be nested also 53

WHILE condition1 LOOP

Trang 54

Cursor 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 55

Implicit 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 58

Opening 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 59

Fetching the Cursor 59

Fetching the cursor involves accessing one row at a

time

FETCH c_customers INTO c_id, c_name, c_addr;

Trang 60

Closing the Cursor 60

Closing the cursor means releasing the allocated

memory

CLOSE c_customers;

Trang 61

Cursor using example 61

Trang 62

Cursor 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 63

Print messages, raise errors

63

5.

Trang 64

Print messages, raise errors

• Print message:

➢ Syntax:

DBMS_OUTPUT.PUT_LINE(‘messages’)

64

Trang 65

Print messages, raise errors

• Print message:

➢ Example:

65

Trang 66

Print messages, raise errors

• Exception:

➢ Syntax:

66

Trang 67

Print messages, raise errors

• Exception:

67

Trang 68

Print messages, raise errors

• Two types of exception:

➢ System-defined exceptions

➢ User-defined exceptions

68

Trang 69

Print messages, raise errors

Trang 70

Print messages, raise errors

• User-defined exception:

➢ Syntax:

70

Trang 71

Print messages, raise errors

• Raising Exceptions:

➢ Syntax:

71

Trang 72

Print messages, raise errors

• Example:

72

Trang 73

Review questions and Exercises

73

6.

Trang 74

Exercises

• Write a PL/SQL block to differenciate between

CHAR and VARCHAR2 datatype

Trang 75

Exercises

Trang 76

Exercises

• Write a PL/SQL block display the discription

against a grade using CASE statement

Trang 77

Exercises

Trang 78

Exercises

Trang 79

Exercises

• Write a PL/SQL block to print numbers from 0 to 9 using LOOP and FOR LOOP

Trang 80

Exercises

LOOP

Trang 81

Exercises

LOOP

Trang 82

Exercises

FOR LOOP

Trang 83

Exercises

FOR LOOP

Trang 84

Exercises

• Write a PL/SQL block to check whether a number

is positive, negative or zero

Trang 85

Exercises

Trang 86

Exercises

Trang 87

Exercises

• Write a PL/SQL block to check whether a number

is prime or not

Trang 88

Exercises

Trang 89

Exercises

Ngày đăng: 04/04/2023, 08:25

TỪ KHÓA LIÊN QUAN

w