1. Trang chủ
  2. » Giáo án - Bài giảng

Stands for Procedural Language

68 80 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 đề Stands for Procedural Language
Trường học University (Please specify the university name if available)
Chuyên ngành Computer Science
Thể loại essay
Năm xuất bản Not specified
Thành phố Not specified
Định dạng
Số trang 68
Dung lượng 613 KB

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

Nội dung

PL SQL BlockRemember : Declare is optional and only required when variables need to be declared.. Whatever value user will enter here will be assign to variable name ‘ ‘ is used in cas

Trang 1

PL/SQL

Trang 3

Why PL SQL ?

 PL/SQL stands for Procedural Language/SQL.

 PL/SQL extends SQL by adding constructs found in

procedural languages like procedures, loops, variables, objects etc.

 Resulting a structural language that is more powerful than SQL

Trang 4

PL SQL, Is there any Advantage ?

 In case of SQL to send 3 queries we will need three network trips between client and server.

 In PL-SQL we bundle any number of queries in a block and in single network trip task is done.

SQL Query1

SQL Query2

SQL Query3

Client

Server

SQL Query1

SQL Query2

SQL Query3

Client Server

PL-SQL Block

Trang 5

Language features

 Supports constructs like any other 4th generation language:

 Variables and Data types

 Loops and Control statements

 Procedures and Functions

Trang 8

PL SQL Block

Remember :

Declare is optional and only required when variables need to be declared Exception is optional and required when Error/Exception handling is done Begin and End are mandatory as all logic and queries are written inside it.

Declare

Begin

Exception

End;

Trang 9

PL SQL program- Sample I

BEGIN

Insert into Dept values(70,’HR’,’Pune’);

Insert into Dept values(80,’PSD’,’Mumbai’);

Insert into Dept values(90,’ESG’,’Pune’);

END;

This program will insert three records at the same time in the table dept.

Trang 10

V_sum := v_num1 + v_num2 ;

Dbms_Output.Put_Line (‘The Sum of number is :’ || v_sum); END;

Trang 11

Save , Edit and Execute program

 Type your program in SQL * plus

 To save : Save <File Name>

Program is saved in the bin directory to save in other folder give complete path Eg: Save ‘C:\ESG\FirstPrg.sql’

 To make changes:

Edit <File Name>

To edit program saved in folder other then bin

Trang 12

 IF , ELSE , ELSIF , END IF

Cont

Trang 14

 Assignment operator: In PL SQL assignment operator is

Trang 15

 Important operators in PL SQL

 Line ends with operator: ;

 To join two strings: ||

Trang 16

Whatever value user will enter

here will be assign to variable

num1

Enter a value for Number1:

Trang 17

Whatever value user will enter

here will be assign to variable

name

‘ ‘ is used in case if entered data is not numeric

Enter a value for Name:

Trang 19

Display value : Examples

else values are not displayed.

Trang 20

Display value : Examples

Trang 21

 Select syntax is different then SQL , it contains INTO clause.

 If Select query can return more then one rows then you should

always use cursors

Trang 22

Select Syntax for a Single Row

Query.

 Select column1, column2

INTO Variable1,Variable2 From Table Name

Where condition …

 The only change is as many columns you want to get from the query you need to declare that many variables and use INTO clause

 All other parts of query are unchanged

 If Where condition here is such that query will return multiple records then CURSOR should be used Without that it will give error

Trang 25

language like Japenese , chinese etc

Number of characters it can store depend on language

To store multi byte variable length character data Its same as Varchar2 only difference is it is used to store characters of different language like Japenese , chinese etc

Number of characters it can store depend on language

Trang 28

Variable Declaration in PL SQL

 Variables are always declared in DECLARE section of the program

 Variable Name <Data Type>

 Various way to declare them

v_empno Number;

V_ename varchar2;

v_job Char(10);

Trang 29

Variable Declaration in PL SQL

 Dynamic and preferred way to declare a variable

Variable Name TableName.ColName%Type

v_empno Emp.Empno%Type;

V_ename Emp.Ename%Type;

v_deptno Dept.Deptno%Type;

 Advantages of declaring in above way

 Variable will always have same datatype as column

 Any change in column will change the type of variable also, so

we need not have to change and recompile the program to run

Trang 30

Variable Declaration in PL SQL

Variable Name TableName%RowType

v_emp Emp%RowType;

 Advantages of declaring in above way.

 Variable will become like a structure variable in C (i.e v_emp will have same structure like Emp Table) and you can refer to individual element as follows:

v_emp.empno v_emp.ename v_emp.sal

Trang 31

Variable Declaration in PL SQL

 Type : You can also make your own type in program and use in the declare section to declare variable

Type t_name is Varchar2(50);

now you can make variable of this type

v_name and v_name2 both will become varchar2(50)

Trang 32

 Note here that for one IF we only need one END IF;

 No END IF is required for ELSIF i.e for one set of IF condition only one END IF; is required

Trang 34

Conditional Statements

 CASE : This is available from ORACLE 8i onwards only , not in ORACLE 8 and version prior to that

CASE WHEN <Variable> = <Value1> Then

Trang 36

 Exit when is required to give the condition to end the loop

 It is pre tested as condition is checked first and then code is executed

Trang 37

TYPES OF LOOPS

 Simple Loop

Loop

Exit When i = 10dbms_output.put_line (i);End Loop;

Pre Tested

Trang 38

 Exit when is required to give the condition to end the loop

 It is post tested as condition is checked after the code is executed

Trang 39

TYPES OF LOOPS

 Simple Loop

Loop

dbms_output.put_line (i);Exit When i = 10

End Loop;

Post Tested

Trang 40

 While is required for condition to end the Loop

 This is also pre tested

Trang 44

 This Loop will execute the given code 100 times for i = 100 to 1

 This is reverse i.e from last value to first value

Trang 45

Looping over results of a query using

FETCH

existing rows

FOUND?

FETCH if rows

Trang 46

Explicit Cursor Attributes

Obtain status information about a cursor.

Attribute Type Description

%ISOPEN Boolean Evaluates to TRUE if the cursor

is open.

%NOTFOUND Boolean Evaluates to TRUE if the most

recent fetch does not return a row.

%FOUND Boolean Evaluates to TRUE if the most

recent fetch returns a row;

complement of %NOTFOUND

%ROWCOUNT Number Evaluates to the total number of

rows returned so far.

Trang 47

fetch c into num;

exit when c%NOTFOUND;

end loop;

close c;

end;

Trang 48

Printing Output

 You need to use a function in the DBMS_OUTPUT package

in order to print to the output

 The output is actually buffered

 If you want to see the output on the screen, you must type the following (before starting):

 set serveroutput on format wrapped size 1000000

 Then print using

dbms_output put_line(your_string);

dbms_output.put(your_string);

Trang 50

Trapping Oracle Server Errors

Trang 51

dbms_output.put_line('Too many!'); WHEN OTHERS THEN

dbms_output.put_line(SQLERRM);

end;

Trang 53

Functions and Procedures

block

can be called several times

Trang 54

CREATE [OR REPLACE] PROCEDURE

procedure_name

[(parameter1 [mode1] datatype1, parameter2 [mode2] datatype2, .)]

IS|AS

PL/SQL Block;

Creating Procedures

Trang 55

 Modes:

 IN: procedure must be called with a value for the parameter Value cannot be changed

 OUT: procedure must be called with a variable for the

parameter Changes to the parameter are seen by the user (i.e., call by reference)

 IN OUT: value can be sent, and changes to the parameter are seen by the user

 Default Mode is: IN

Trang 56

create or replace procedure

Trang 57

Errors in a Procedure

 If there are errors in the procedure definition, they will not

be shown

To see the errors of a procedure called proc, type

SHOW ERRORS PROCEDURE proc

in the SQLPLUS prompt

 For functions, type

SHOW ERRORS FUNCTION fun_name

Trang 59

Creating a Function

supply a return type

CREATE [OR REPLACE] FUNCTION

function_name

[(parameter1 [mode1] datatype1,

parameter2 [mode2] datatype2,

[(parameter1 [mode1] datatype1,

parameter2 [mode2] datatype2,

)]

RETURN datatype

IS|AS

PL/SQL Block;

Trang 61

create or replace function

return 'You are great';

ELSIF rating >= 5 THEN

return 'Not bad';

return 'You are great';

ELSIF rating >= 5 THEN

return 'Not bad';

Trang 62

Trigger is like a procedure that is automatically invoked

by the DBMS in response to specified changes to data base

Trigger is like a ‘Daemon that monitors a data base, and is executed when the data base is modified in

a way that matches the event specification

A data base that has a set of associated triggers is called an active data base

Trang 65

Trigger Timings

Execute the trigger action before the triggering statement

Eliminate unnecessary processing of the triggering statement

AFTER triggers are used when you want the triggering statement

to complete before executing the trigger action

Trang 66

CREATE or REPLACE TRIGGER cs348

after INSERT ON weatherforecast

FOR EACH ROW

WHEN (:new.temp>= 60)

BEGIN

DBMS_OUTPUT.PUT_LINE(‘NICE WEATHER’); END cs348

/

Show error;

Trang 67

Triggers can be confusing

 Update table weatherforcast

T1 Fired

Before Update on weatherforcast

For each row

Trang 68

• What are the uses of triggers?

Flexible Management of integrity

Ngày đăng: 12/05/2014, 11:55

w