Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to PL/SQL N. Dimililer. About PL/SQL –PL/SQL is an extension to SQL with design features of programming languages. –Data manipulation and.

Similar presentations


Presentation on theme: "Introduction to PL/SQL N. Dimililer. About PL/SQL –PL/SQL is an extension to SQL with design features of programming languages. –Data manipulation and."— Presentation transcript:

1 Introduction to PL/SQL N. Dimililer

2 About PL/SQL –PL/SQL is an extension to SQL with design features of programming languages. –Data manipulation and query statements of SQL are included within procedural units of code. –PL/SQL offers modern software engineering features such as data encapsulation, exception handling, information hiding, and object orientation.

3 PL/SQL Blocks Anonymous Block : Unnamed block of PL/SQL. These do not accept any parameters. Procedure : A series of statements accepting and/or returning zero or more variables through parameters. Function : A series of statements accepting zero or more variables that returns one value using an explicit RETURN statement. Package : A collection of procedures and functions that has two parts, a specification listing available procedures and functions and their parameters, and a body that contains the actual code for the procedures and functions. Trigger : A series of PL/SQL statements attached to a database table that execute whenever a triggering event (select, update, insert, delete) occurs.

4 Benefits of PL/SQL Integration Improved Performance Modularize Program Development Portability Identifiers can be used Procedural Language Control Structures Exception/Error handling

5 PL/SQL improves performance Application Other DBMSs Application Oracle with PL/SQL SQL SQL SQL SQL SQLIF...THENSQLELSESQL END IF; SQL

6 PL/SQL Block Structure DECLARE BEGIN EXCEPTION END;

7 DECLARE --optional BEGIN --mandatory EXCEPTION --optional END; Variables, cursors, user-defined exceptions SQL statements PL/SQL statements Actions to perform when errors occur

8 Executing Statements and PL/SQL Blocks from SQL*Plus Place a semicolon (;) at the end of a SQL statement or PL/SQL control statement. Use a slash (/) to run the anonymous PL/SQL block in the SQL*Plus buffer. When the block is executed successfully, without unhandled errors or compile errors, the message output should be as follows: PL/SQL procedure successfully completed. Place a period (.) to close a SQL*Plus buffer. A PL/SQL block is treated as one continuous statement in the buffer, and the semicolons within the block do not close or run the buffer.

9 PL/SQL statements and semicolon In PL/SQL, an error is called an exception. Section keywords DECLARE, BEGIN, and EXCEPTION are not followed by semicolons. requireEND and all other PL/SQL statements require a semicolon to terminate the statement. You can string statements together on the same line, but this method is not recommended for clarity or editing.

10 Use of Variables With PL/SQL you can declare variables and then use them in SQL and procedural statements anywhere an expression can be used. –Temporary storage of data –Manipulation of stored values –Reusability. –Ease of maintenance

11 Handling Variables in PL/SQL –Declare and initialize variables in the declaration section. –Assign new values to variables in the executable section. –Pass values into PL/SQL blocks through parameters.(will be covered later on when we talk about procedures and functions) –View results through output variables. –Remember: Uninitialized variables contain “NULL” value

12 identifier [CONSTANT] datatype [NOT NULL] [:= | DEFAULT expr]; identifier [CONSTANT] datatype [NOT NULL] [:= | DEFAULT expr]; Declare v_hiredateDATE; v_deptnoNUMBER(2) NOT NULL := 10; v_genderVARCHAR2(6) := ' FEMALE ' ; v_cityVARCHAR2(10); c_commCONSTANT NUMBER := 1400; Declare v_hiredateDATE; v_deptnoNUMBER(2) NOT NULL := 10; v_genderVARCHAR2(6) := ' FEMALE ' ; v_cityVARCHAR2(10); c_commCONSTANT NUMBER := 1400; Declaration of PL/SQL variables Syntax of declaration Example declarations You cannot assign NULL to v_deptno. Must be initialized The value of c_comm cannot be changed. Must be initialized at declaration

13 Assigning values to variables v_ename := ‘Ali'; v_hiredate := '31-DEC-2012'; identifier := expr; Set the employee name to “Ali” Set v_hiredate to a date Syntax of assignement

14 DBMS_OUTPUT.PUT_LINE Put_line is a procedure included in the Oracle-supplied package DBMS_OUTPUT Used for displaying data from a PL/SQL block on the screen Must be enabled in SQL*Plus with SET SERVEROUTPUT ON

15 Example Anonymous Blocks BEGIN DBMS_OUTPUT.PUT_LINE(‘Hello world’); END; BEGIN DBMS_OUTPUT.PUT_LINE(‘Today is’ || SYSDATE ); END; Simply prints “Hello world” on screen Put_line accepts a single string as parameter. Use concatenation operator if you have more than one string to display

16 Example Anonymous Blocks DECLARE v_name varchar2(20); BEGIN v_name := ‘Ali’; DBMS_OUTPUT.PUT_LINE(Hello || v_name); END; DECLARE v_name varchar2(20) := ‘Ali’; BEGIN DBMS_OUTPUT.PUT_LINE(Hello || v_name); END; Variable Initialization Variable declaration v_name varchar2(20) DEFAULT ‘Ali’; Task: Write an anonymous block to print “Hello Ali” on screen. Declare name as a variable Variable declared and initialized at declaration

17 Example Anonymous Blocks Read the name of user from keyboard and print “Hello ” on screen. DECLARE v_name varchar2(20); BEGIN v_name := &your_name; DBMS_OUTPUT.PUT_LINE(‘Hello ’ || v_name); END; Enter value for your_name: ‘Veli’ Old4: v_name := &your_name New4: v_name := ‘Veli’ Hello Veli Output Substitution Variable: The value you enter from keyboard replaces the substitution variable Keyboard entry

18 Controlling Flow of Events You can change the logical flow of statements using conditional IF statements and loop control structures. Conditional IF statements: –IF-THEN-END IF –IF-THEN-ELSE-END IF –IF-THEN-ELSIF-ELSE-END IF

19 IF condition THEN statements; [ELSIF condition THEN statements;] [ELSE statements;] END IF; IF condition THEN statements; [ELSIF condition THEN statements;] [ELSE statements;] END IF; IF v_dept_name = ‘ITEC' THEN v_dept_no := 35; END IF; IF v_dept_name = ‘ITEC' THEN v_dept_no := 35; END IF; Syntax Example: If v_dept_name is ‘ITEC’ change v_dept_no to 35. IF STATEMENT

20 Example Anonymous Block Task: Write an anonymous block that will print ‘weekend’ if the system date is ‘Sunday’ or ‘Saturday’. DECLARE v_current_date date := SYSDATE; BEGIN IF to_char(v_current_date, ‘d’) in (1,7) THEN dbms_output.put_line(‘weekend’); END IF; END;

21 Example Anonymous Block Task: Write an anonymous block that will print ‘weekend’ if the system date is ‘Sunday’ or ‘Saturday’ and ‘weekday’ otherwise. BEGIN IF to_char(sysdate, ‘d’) in (1,7) THEN dbms_output.put_line(‘weekend’); ELSE dbms_output.put_line(‘weekday’); END IF; END;

22 Example Anonymous Block Task: Write an anonymous block that will print weekend if the system date is ‘Sunday’ or ‘Saturday’, we have class if it is Tuesday or Thursday and no class otherwise. BEGIN IF to_char(sysdate, ‘d’) in (1,7) THEN dbms_output.put_line(‘week end’); ELSIF to_char(sysdate,’d’) in (3,5) THEN dbms_output.put_line(‘we have class’); ELSE dbms_output.put_line(‘no class’); END IF; END;

23 Summary We learned the overall format of a PL/SQL anonymous block. If statement is covered. Next lesson we will learn about loops.


Download ppt "Introduction to PL/SQL N. Dimililer. About PL/SQL –PL/SQL is an extension to SQL with design features of programming languages. –Data manipulation and."

Similar presentations


Ads by Google