Presentation is loading. Please wait.

Presentation is loading. Please wait.

PL/SQL week10.

Similar presentations


Presentation on theme: "PL/SQL week10."— Presentation transcript:

1 PL/SQL week10

2 PL/SQL The PL/SQL programming language was developed by Oracle Corporation in the late 1980s as procedural extension language for SQL and the Oracle relational database. Following are notable facts about PL/SQL: PL/SQL is a completely portable, high-performance transaction-processing language. PL/SQL provides a built-in interpreted and OS independent programming environment. PL/SQL can also directly be called from the command-line SQL*Plus interface. Direct call can also be made from external programming language calls to database. PL/SQL's general syntax is based on that of ADA and Pascal programming language. Apart from Oracle, PL/SQL is available in TimesTen in-memory database and IBM DB2.

3 Features of PL/SQL PL/SQL is tightly integrated with SQL.
It offers extensive error checking. It offers numerous data types. It offers a variety of programming structures. It supports structured programming through functions and procedures. It supports object-oriented programming. It supports developing web applications and server pages.

4 Advantages of PL/SQL SQL is the standard database language and PL/SQL is strongly integrated with SQL. PL/SQL supports both static and dynamic SQL. Static SQL supports DML operations and transaction control from PL/SQL block. Dynamic SQL is SQL allows embedding DDL statements in PL/SQL blocks. PL/SQL allows sending an entire block of statements to the database at one time. This reduces network traffic and provides high performance for the applications. PL/SQL gives high productivity to programmers as it can query, transform, and update data in a database. PL/SQL saves time on design and debugging by strong features, such as exception handling, encapsulation, data hiding, and object-oriented data types. Applications written in PL/SQL are fully portable. PL/SQL provides high security level. PL/SQL provides access to predefined SQL packages. PL/SQL provides support for Object-Oriented Programming. PL/SQL provides support for Developing Web Applications and Server Pages.

5 Structure of PL/SQL PL/SQL is a block-structured language. That is, the basic units (procedures, functions, and anonymous blocks) that make up a PL/SQL program are logical blocks, which can contain any number of nested sub-blocks. Typically, each logical block corresponds to a problem or sub-problem to be solved. Thus, PL/SQL supports the divide-and-conquer approach to problem solving called stepwise refinement.

6 Structure of PL/SQL

7

8 PL/SQL program units A PL/SQL unit is any one of the following:
PL/SQL block Function Package Package body Procedure Trigger Type Type body

9 Every PL/SQL variable has a specific type associated with it.
There are four kinds of data types Scalar data type One of the types used by SQL for database columns A generic type used in PL/SQL such as NUMBER Composite data type Reference data type Declared to be the same as the type of some database column LOB data types for huge binary data used by images and sounds

10

11 Scalar Data Types Represent a single value Data type Description Sample Declaration Varchar2 Variable-length character string Lastname varchar2(30) char Fixed-length character string Gender char(1) Number Floating-point, fixed-point, or integer number Price number(5,2) date Date and time Todays_date Date; …. These data types are directly from data types used by SQL database field specification.

12 Bridging role reflected in data types
Some other data types used PL/SQL are more general purpose programming language oriented, not corresponding to database data types. INTEGER BOOLEAN DECIMAL Note that PL/SQL allows BOOLEAN variables, even though Oracle does not support BOOLEAN as a type for database columns.

13 PL/SQL Numeric Data Types and Subtypes

14 PL/SQL Character Data Types and Subtypes

15 PL/SQL Datetime and Interval Types

16 PL/SQL Large Object (LOB) Data Types

17 Composite data types Composite data types
RECORD: contains multiple scalar values, similar to a table record TABLE: tabular structure with multiple columns and rows VARRAY: variable-sized array

18 Reference data types %TYPE: same data type as a database field
In many cases, a PL/SQL variable will be used to manipulate data stored in a existing table. In this case, it is essential that the variable have the same type (compatible is also ok in some situation) as the relation column. Directly reference a specific database field or record and assume the data type of the associated field or record %TYPE: same data type as a database field %ROWTYPE: same data type as a database record

19 Data Types in PL/SQL can be tricky!
If there is any type mismatch, variable assignments and comparisons may not work the way you expect. To be safe, instead of hard coding the type of a variable, you should use the %TYPE operator. For example DECLARE     myBeer Beers.name%TYPE; gives PL/SQL variable myBeer whatever type was declared for the name column in relation Beers

20 Variables Userid varchar2(10); Variables
Used to store numbers, character strings, dates, and other data values Avoid using keywords, table names and column names as variable names Must be declared with data type before use: variable_name data_type_declaration; Userid varchar2(10); Default value is always NULL when declared without being initialized. The initial value of any variable, regardless of its type, is NULL.

21 Assignment Statements
We can assign values to variables, using the ":=" operator. Like any other programming languages you might have used before, the assignment can occur either immediately after the type of the variable is declared, or anywhere in the executable portion of the program. Assigns a value to a variable variable_name := value; Value can be a literal: s_first_name := ‘Steven'; Value can be another variable: first_name := s_first_name;

22

23 Examples Before you write the codes in the next slides , do the following : 1- in your sqlplus write the following code : set serveroutput on size 30000; 2- you can write your PL/SQL codes directly to sqlplus , OR you can write the code in a notepad file . When you save remember to save it as sql. For example , you can name the file , hello.sql to execute the code in sqlplus , simply type @:the path of the sql file. Example : @C:\Users\HANI\Documents\hello.sql

24 Example 1: Hello World DECLARE
message varchar2(20) := ‘Hello, World ! ‘; BEGIN dbms_output.put_line(message); END; /

25 Result

26 Example 2 DECLARE e_id emp.empno%type :=7788; e_name emp.ename%type;
e_job emp.job%type; e_sal emp.sal%type; BEGIN select ename,job,sal into e_name,e_job,e_sal from emp where empno=e_id; dbms_output.put_line ('Employee : ' || e_name || ' JOB: ' || e_job || ' Salary: '|| e_sal); END; /

27 results

28 constants

29

30

31

32

33 PL/SQL Operator Precedence

34 Conditions

35

36 Example 3 (if) DECLARE e_id emp.empno%type :=7788;
e_name emp.ename%type; e_job emp.job%type; e_sal emp.sal%type; BEGIN select ename,job,sal into e_name,e_job,e_sal from emp where empno=e_id; if (e_sal between 500 and 4000) then dbms_output.put_line ('Employee : ' || e_name || ' JOB: ' || e_job || ' Salary: '|| e_sal); end if; END; /

37 Results

38 Example 4(if-else) DECLARE e_id emp.empno%type :=7788;
e_name emp.ename%type; e_job emp.job%type; e_sal emp.sal%type; BEGIN select ename,job,sal into e_name,e_job,e_sal from emp where empno=e_id; if (e_sal between 500 and 2000) then dbms_output.put_line ('the employee get a very low salary'); else dbms_output.put_line ('Employee : ' || e_name || ' JOB: ' || e_job || ' Salary: '|| e_sal); end if; END; /

39 results

40 Example 5-using (if-elsif)
DECLARE e_id emp.empno%type :=7788; e_name emp.ename%type; e_job emp.job%type; e_sal emp.sal%type; BEGIN select ename,job,sal into e_name,e_job,e_sal from emp where empno=e_id; if (e_sal between 500 and 2000) then dbms_output.put_line ('Employee : ' || e_name || ' JOB: ' || e_job || ' Salary: '|| e_sal || 'Salary Grade C'); elsif (e_sal between 2500 and 4000) then dbms_output.put_line ('Employee : ' || e_name || ' JOB: ' || e_job || ' Salary: '|| e_sal || 'Salary Grade B'); elsif (e_sal >4000)then dbms_output.put_line ('Employee : ' || e_name || ' JOB: ' || e_job || ' Salary: '|| e_sal || 'Salary Grade A'); end if; END; /

41 results

42 Example 6 – case statement

43 results

44 Example 7 (Nested if)

45 results

46 loop

47 Example 8 (basic loop) DECLARE
type namesarray is varray(7) of varchar2(20); type salary is varray(7) of integer; names namesarray; sal salary; x number(2); BEGIN names:=namesarray('Ahmad','khaled','ali','yousef','hani','mohammad','sami'); sal:=salary(1500,2500,6000,6500,7444,1000,2600); x:=1; loop insert into week10 values(x,names(x),sal(x)); x:=x+1; if x>7 then exit; end if; end loop; end; /

48 Results

49 Example 9 using the EXIT WHEN

50 results

51 Example 9 while loop DECLARE
type namesarray is varray(7) of varchar2(20); type salary is varray(7) of integer; names namesarray; sal salary; x number(2); BEGIN names:=namesarray('abdullah','ramy','khalil','elias','yahya','ibrahim','saud'); sal:=salary(1570,2580,6900,7500,9444,10000,21600); x:=7; while x<14 loop insert into week10 values(x,names(x-6),sal(x-6)); x:=x+1; end loop; end; /

52 results

53 Example 10 For loop

54 results

55 Reverse loop

56

57 Strings

58

59 example11

60 Comments Enclosed between /* and */ Not executed by interpreter
C style comments (/* ... */) may be used. Enclosed between /* and */ On one line beginning with --


Download ppt "PL/SQL week10."

Similar presentations


Ads by Google