introduction to pl/sql
DESCRIPTION
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 query statements of SQL are included within procedural units of code. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Introduction to PL/SQL](https://reader036.vdocument.in/reader036/viewer/2022081515/56814749550346895db48831/html5/thumbnails/1.jpg)
Introduction to PL/SQL
N. Dimililer
![Page 2: Introduction to PL/SQL](https://reader036.vdocument.in/reader036/viewer/2022081515/56814749550346895db48831/html5/thumbnails/2.jpg)
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.
![Page 3: Introduction to PL/SQL](https://reader036.vdocument.in/reader036/viewer/2022081515/56814749550346895db48831/html5/thumbnails/3.jpg)
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.
![Page 4: Introduction to PL/SQL](https://reader036.vdocument.in/reader036/viewer/2022081515/56814749550346895db48831/html5/thumbnails/4.jpg)
Benefits of PL/SQL
• Integration
• Improved Performance
• Modularize Program Development
• Portability
• Identifiers can be used
• Procedural Language Control Structures
• Exception/Error handling
![Page 5: Introduction to PL/SQL](https://reader036.vdocument.in/reader036/viewer/2022081515/56814749550346895db48831/html5/thumbnails/5.jpg)
PL/SQL improves performance
ApplicationApplication Other DBMSsOther DBMSs
ApplicationApplicationOracle with
PL/SQLOracle with
PL/SQL
SQLSQL
SQLSQLSQLSQL
SQLSQL
SQLSQLIF...THENIF...THEN
SQLSQLELSEELSE
SQLSQLEND IF;END IF;SQLSQL
![Page 6: Introduction to PL/SQL](https://reader036.vdocument.in/reader036/viewer/2022081515/56814749550346895db48831/html5/thumbnails/6.jpg)
PL/SQL Block Structure
DECLAREDECLARE
BEGINBEGIN
EXCEPTIONEXCEPTION
END;END;
![Page 7: Introduction to PL/SQL](https://reader036.vdocument.in/reader036/viewer/2022081515/56814749550346895db48831/html5/thumbnails/7.jpg)
PL/SQL Block Structure
DECLAREDECLARE --optional --optional
BEGINBEGIN --mandatory --mandatory
EXCEPTIONEXCEPTION --optional --optional
END;END;
Variables, cursors, user-defined exceptions
SQL statementsPL/SQL statements
Actions to perform whenerrors occur
![Page 8: Introduction to PL/SQL](https://reader036.vdocument.in/reader036/viewer/2022081515/56814749550346895db48831/html5/thumbnails/8.jpg)
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.
![Page 9: Introduction to PL/SQL](https://reader036.vdocument.in/reader036/viewer/2022081515/56814749550346895db48831/html5/thumbnails/9.jpg)
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.
• END and all other PL/SQL statements requirerequire 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.
![Page 10: Introduction to PL/SQL](https://reader036.vdocument.in/reader036/viewer/2022081515/56814749550346895db48831/html5/thumbnails/10.jpg)
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
![Page 11: Introduction to PL/SQL](https://reader036.vdocument.in/reader036/viewer/2022081515/56814749550346895db48831/html5/thumbnails/11.jpg)
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
![Page 12: Introduction to PL/SQL](https://reader036.vdocument.in/reader036/viewer/2022081515/56814749550346895db48831/html5/thumbnails/12.jpg)
identifier [CONSTANT] datatype [NOT NULL] [:= | DEFAULT expr];
identifier [CONSTANT] datatype [NOT NULL] [:= | DEFAULT expr];
Declare v_hiredate DATE; v_deptno NUMBER(2) NOT NULL := 10;
v_gender VARCHAR2(6) := 'FEMALE'; v_city VARCHAR2(10); c_comm CONSTANT NUMBER := 1400;
Declare v_hiredate DATE; v_deptno NUMBER(2) NOT NULL := 10;
v_gender VARCHAR2(6) := 'FEMALE'; v_city VARCHAR2(10); c_comm CONSTANT NUMBER := 1400;
Declaration of PL/SQL variables
Syntax of declaration
Example declarationsYou cannot assign NULL to v_deptno. Must be initialized
The value of c_comm cannot be changed. Must be initialized at declaration
![Page 13: Introduction to PL/SQL](https://reader036.vdocument.in/reader036/viewer/2022081515/56814749550346895db48831/html5/thumbnails/13.jpg)
Assigning values to variables
v_ename := ‘Ali';v_ename := ‘Ali';
v_hiredate := '31-DEC-2012';v_hiredate := '31-DEC-2012';
identifier := expr;identifier := expr;
Set the employee name to “Ali”
Set v_hiredate to a date
Syntax of assignement
![Page 14: Introduction to PL/SQL](https://reader036.vdocument.in/reader036/viewer/2022081515/56814749550346895db48831/html5/thumbnails/14.jpg)
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
![Page 15: Introduction to PL/SQL](https://reader036.vdocument.in/reader036/viewer/2022081515/56814749550346895db48831/html5/thumbnails/15.jpg)
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
![Page 16: Introduction to PL/SQL](https://reader036.vdocument.in/reader036/viewer/2022081515/56814749550346895db48831/html5/thumbnails/16.jpg)
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 InitializationVariable
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
![Page 17: Introduction to PL/SQL](https://reader036.vdocument.in/reader036/viewer/2022081515/56814749550346895db48831/html5/thumbnails/17.jpg)
Example Anonymous Blocks
Read the name of user from keyboard and print “Hello <name>” 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’Old 4: v_name := &your_nameNew 4: v_name := ‘Veli’Hello Veli
Output
Substitution Variable: The value you enter from keyboard replaces the substitution variable Keyboard entry
![Page 18: Introduction to PL/SQL](https://reader036.vdocument.in/reader036/viewer/2022081515/56814749550346895db48831/html5/thumbnails/18.jpg)
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
![Page 19: Introduction to PL/SQL](https://reader036.vdocument.in/reader036/viewer/2022081515/56814749550346895db48831/html5/thumbnails/19.jpg)
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
![Page 20: Introduction to PL/SQL](https://reader036.vdocument.in/reader036/viewer/2022081515/56814749550346895db48831/html5/thumbnails/20.jpg)
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;
![Page 21: Introduction to PL/SQL](https://reader036.vdocument.in/reader036/viewer/2022081515/56814749550346895db48831/html5/thumbnails/21.jpg)
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;
![Page 22: Introduction to PL/SQL](https://reader036.vdocument.in/reader036/viewer/2022081515/56814749550346895db48831/html5/thumbnails/22.jpg)
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;
![Page 23: Introduction to PL/SQL](https://reader036.vdocument.in/reader036/viewer/2022081515/56814749550346895db48831/html5/thumbnails/23.jpg)
Summary
• We learned the overall format of a PL/SQL anonymous block.
• If statement is covered.
• Next lesson we will learn about loops.