Oracle. PL/SQL Procedural Language

Similar documents
PLSQL 9i Index. Section Title Page

ORACLE: PL/SQL Programming

Question: Which statement would you use to invoke a stored procedure in isql*plus?

Index. Boolean expression, , Business rules enforcement. see Declarative constraints table with Oracle constraints and,

UNIT II PL / SQL AND TRIGGERS

Banner Oracle PL/SQL and Database Objects Training Workbook

PL/SQL. Exception. When the PL/SQL engine cannot execute the PLSQL block it raise an error. Every Oracle error has an error number

"Charting the Course... Oracle 18c PL/SQL (5 Day) Course Summary

Introduction to Computer Science and Business

Meet MariaDB Vicențiu Ciorbaru Software MariaDB Foundation * * 2017 MariaDB Foundation

Oracle PL/SQL - 12c & 11g [Basic PL/SQL & Advanced PL/SQL]

5. Single-row function

Vendor: Oracle. Exam Code: 1Z Exam Name: Oracle Database 11g: Program with PL/ SQL. Version: Demo

Oracle Database 12c: Program with PL/SQL Duration: 5 Days Method: Instructor-Led

Contents I Introduction 1 Introduction to PL/SQL iii

Database Programming with PL/SQL

Oracle Database 11g: Program with PL/SQL Release 2

PL/SQL Block structure

Table of Contents. Oracle SQL PL/SQL Training Courses

Oracle Database: Program with PL/SQL

Oracle Database: Program with PL/SQL Ed 2

2 PL/SQL - fundamentals Variables and Constants Operators SQL in PL/SQL Control structures... 7

Oracle Database 12c R2: Program with PL/SQL Ed 2 Duration: 5 Days

Conditionally control code flow (loops, control structures). Create stored procedures and functions.

IZ0-144Oracle 11g PL/SQL Certification (OCA) training

Oracle12c Release 1 PL/SQL (3 Days)

Oracle Developer Track Course Contents. Mr. Sandeep M Shinde. Oracle Application Techno-Functional Consultant

Oracle PLSQL Training Syllabus

PLSQL Interview Questions :

Oracle Database: Program with PL/SQL

When a database trigger routine does not have to take place before the triggering event, which timing should you assign to the trigger?

Oracle - Oracle Database: Program with PL/SQL Ed 2

1Z Oracle Database 11g - Program with PL/SQL Exam Summary Syllabus Questions

Introduction p. 1 The Logical and Physical View of Tables p. 1 Database Types p. 4 NULLs p. 6 DDL and DML Statements p. 7 Column and Table Constraint

Slides by: Ms. Shree Jaswal

Topics Fundamentals of PL/SQL, Integration with PROIV SuperLayer and use within Glovia

PL / SQL Basics. Chapter 3

1Z0-144 Q&As Oracle Database 11g: Program with PL/ SQL

ORACLE TRAINING. ORACLE Training Course syllabus ORACLE SQL ORACLE PLSQL. Oracle SQL Training Syllabus

Oracle EXAM 1Z0-144 Oracle Database 11g: Program with PL/SQL

Table of Contents. PDF created with FinePrint pdffactory Pro trial version

1Z0-144.v Number: 1Z0-144 Passing Score: 800 Time Limit: 120 min File Version:

Oracle Database 11g: Program with PL/SQL

Sisteme Informatice şi Standarde Deschise (SISD) Curs 7 Standarde pentru programarea bazelor de date (1)

Part 18: Application Programming II (Stored Procedures,Triggers)

ADVANTAGES. Via PL/SQL, all sorts of calculations can be done quickly and efficiently without use of Oracle engine.

to use this Student Guide

Oracle Database: Introduction to SQL/PLSQL Accelerated

Oracle Class VI. Exception Block Cursors For Loops

Break Free from Oracle, leveraging your existing Oracle skills!

Lab # 4. Data Definition Language (DDL)

RNDr. Michal Kopecký, Ph.D. Department of Software Engineering, Faculty of Mathematics and Physics, Charles University in Prague

Programming in Oracle with PL/SQL. Procedural Language Extension to SQL

PL/SQL is a combination of SQL along with the procedural features of programming languages.

Oracle PL/SQL. DUMmIES. by Michael Rosenblum and Dr. Paul Dorsey FOR

Procedural Language Structured Query Language (PL/SQL)

Introduction to SQL/PLSQL Accelerated Ed 2

Data Definition Language (DDL)

Full file at

SQL is an English like language consisting of commands to store, retrieve, maintain & regulate access to your database.

2. Programming written ( main theme is to test our data structure knowledge, proficiency

ORACLE Job Placement Paper. Paper Type : General - other

A Unit of SequelGate Innovative Technologies Pvt. Ltd. All Training Sessions are Completely Practical & Real-time

Oracle Exam 1z0-144 Oracle Database 11g: Program with PL/SQL Version: 8.5 [ Total Questions: 103 ]

Developer. 1 enterprise. Professional Guide. Oracle Advanced PL/SQL. example questions for 1Z0-146 examination

Oracle Database 11g: SQL and PL/SQL Fundamentals

[Chapter 1] Oracle PL/SQL Language Pocket Reference

Overview of PL/SQL. About PL/SQL. PL/SQL Environment. Benefits of PL/SQL. Integration

EDUVITZ TECHNOLOGIES

Oracle PLSQL. Course Summary. Duration. Objectives

Oracle PLSQL Study Material ORACLE & PL/SQL STUDY MATERIAL

Oracle Database 11g & PL/SQL

Appendix A Practices and Solutions

SQL+PL/SQL. Introduction to SQL

IBM Database Conversion Workbench 3.5

Migrating from Oracle to Postgres

Creating and Managing Tables Schedule: Timing Topic

Oracle Database 10g: PL/SQL Fundamentals. Oracle Internal & Oracle Academy Use Only. Volume I Student Guide. D17112GC30 Edition 3.0 April 2009 D59413

Principles of Data Management

Question Bank PL/SQL Fundamentals-I

Oracle. Exam Questions 1Z Oracle 11g: Advanced PL/SQL. Version:Demo. 1Z0-146 Exam Questions Demo

ORACLE TRAINING CURRICULUM. Relational Databases and Relational Database Management Systems

Oracle 1z z0-146 Oracle Database 11g: Advanced PL/SQL. Practice Test. Version QQ:

Oracle. SQL(Structured Query Language) Introduction of DBMS. Build In Function. Introduction of RDBMS. Grouping the Result of a Query

The Oracle Interview consists of two parts. One for Written test Interview and Another one for HR interview.

Course Outline and Objectives: Database Programming with SQL

Question No : 1 Which statement is true about triggers on data definition language (DDL) statements?

Oracle Database: SQL and PL/SQL Fundamentals NEW

Database Programming with PL/SQL

Database Programming with PL/SQL

PL/SQL-TYCS. The 'Hello World' Example

IT Certification Exams Provider! Weofferfreeupdateserviceforoneyear! h ps://

Oracle Database: SQL and PL/SQL Fundamentals Ed 2

M.SC(IT) I YEAR( ) CORE: ADVANCED DBMS-163B Semester : I Multiple Choice Questions

Oracle TimesTen In-Memory Database

PL/SQL Lab Exercises and Examples

Introduction to Programming Using Java (98-388)

Proje D2K. CMM (Capability Maturity Model) level Project Standard:- Corporate Trainer s Profile

PL/SQL is one of three key programming languages embedded in the Oracle Database, along with SQL itself and Java.

PL/SQL User s Guide and Reference

Transcription:

PL/SQL Procedural Language based on Günther Stürner: 7 - A User s and Developer s Guide Michael R. Ault: 7.0 Administration & Management 8.16.. 10 R2 manuals Feuerstein et al: PL/SQL Language Application Processing PL/SQL Block Database Server Database PL/SQL block declare begin exception end; PL/SQL engine SQL engine DB engine Stored PL/SQL procedures / functions 1

PL/SQL in Client/Server /* calling a stored PL/SQL program */ exec sql execute begin abc (:x, :y, :z); end; end exec; sql sql proc appl. devel. tool PL/SQL engine Sqlplus Applications... Forms... Database Server PL/SQL engine SQL processor & DB engine abc Config file SET.. SQL*Plus Sqlplus commands (see SQL*Plus manuals) sqlplus Spool File declare x. y z begin abc (x, y, z); end; @script.sql PL/SQL engine Native SQL PL/SQL language (see PL/SQL manuals) Database Server (instance) SQL processor & DB engine (see SQL Reference manual) PL/SQL abc 2

isql*plus ks SQL*Plus User s Guide and Reference isql*plus Help 3

Types of PL/SQL programs 4GL procedures or functions client side triggers of Forms, Menu, Reports Anonymous PL/SQL blocks 3GL embedded SQL SQL*Plus scripts (host or client) Stored PL/SQL programs Created procedures, functions, packages stored in database in compiled form Database triggers (remote operations secured by 2PC) Declarative part Variable and constant declarations SQL datatypes, with NOT NULL constraint and initial value PL/SQL datatypes, arrays and records Cursor definitions Declare c1 (<parameters>) select <parameters>; User-defined exceptions myexception exception; max_op_cursors exception; user defined error names overriding error codes Pragma definitions (compiler directives) pragma exception_init( max_op_cursors, -1000) RESTRICT_REFERENCES SERIALLY_REUSABLE AUTONOMOUS_TRANSACTION 4

Identifiers, variables Identifiers: Names of PL/SQL objects: constants, variables, labels, exceptions, functions, procedures, cursors, record types,... Consists of letter, digits 0..9, dollar sign ($), underscore (_), number sign (#) Cannot include whitespaces (space, tab, carriage return) Must start with a letter A..Z Not case-sensitive (except quoted identifiers) Up to 30 characters long Declaring variables: <variable name> <datatype> [CONSTANT] [NOT NULL] [ {:= DEFAULT} <initial value> ] ; Delimiters In addition to typical SQL delimiters ** exponentiation concatenation := assignment = equal to <>!= not equal to ^= ~= << >> label : host variable % attribute indicator / anchoring %TYPE {<cursor> <table>}%rowtype <cursor>%<attribute> @ remote database indicator.. Range indicator => named parameter assignment <parameter> => <value> 5

Predefined PL/SQL Datatypes Number Types NUMBER (SQL, fixed point 38 digits or floating point in range [1E-130.. 1.0E126) ) BINARY_FLOAT, BINARY_DOUBLE (IEEE compatible) BINARY_INTEGER and subtypes (PL/SQL only) NATURAL, NATURALN, POSITIVE, POSITIVEN, SIGNTYPE ISO compatible subtypes INT[EGER], SMALLINT, DEC[IMAL] (will map into SQL NUMBER) FLOAT, REAL, DOUBLE PRECISION (will map to SQL FLOAT) Character Types and Large Objects CHAR[ACTER], NCHAR[ACTER], VARCHAR[2], NVARCHAR[2] LONG, LONG RAW, RAW, ROWID, UROWID BLOB, CLOB, NCLOB, XMLType (SQL) Boolean Types BOOLEAN (PL/SQL only) Date, Time and Interval Types DATE, TIMESTAMP [WITH [LOCAL] TIMEZONE] (SQL) INTERVAL YEAR TO MONTH, INTERVAL DAY TO SECOND.. datatypes, records, collections, arrays Data type copy myvar1 employee.emp_name%type; myvar2 myvar1%type; Row type copy of columns and data types for a record emp_rec employee%rowtype; emp_rec.salary := 1000; Single dimensional arrays type string_arr is table of varchar2(100) indexed by binary_integer; textline string_arr ; Associative Arrays ( index-by tables) Nested Tables Varrays variable-size arrays 6

Examples:.. Records TYPE emp_rec_type IS RECORD ( no smallint, name varchar2(50), birthdate date ) ; emp_rec emp_rec_type; emp_rec.no := 100; emp_rec.name := 'Jones Tom'; emp_rec.birthdate := to_date ('1980-10-10', 'YYYY-MM-DD'); Executable part Assignments variable := value expression SELECT column[s] INTO variable[s] FROM FETCH cursor INTO variable[s] Conditional processing IF THEN ELSE END IF Unconditional branching GOTO label <<label>> Loop structures LOOP END LOOP EXIT WHEN condition FOR END LOOP FORALL i IN 1..imax <statement> WHILE END LOOP Cursor loops Cursor control EXECUTE IMMEDIATE SQL-statement-string Raising exceptions 7

Native Dynamic SQL EXECUTE IMMEDIATE <SQL statement string> [ INTO { <define variable list> <record> <object variable> } ] [ USING [ IN OUT IN OUT ] <bind argument list> ] ; Note: bind parameters allowed only just like in embedded SQL. For object names etc use concatenation of VARCHAR2 values. Example: DECLARE deptname VARCHAR2(14) := 'Development'; location VARCHAR2(13) := 'Helsinki'; BEGIN EXECUTE IMMEDIATE 'insert into dept (deptno,dname,loc) ' 'values (:1, :2, :3) ' USING 50, deptname, location; END; Cursor processing Cursor status attributes: %FOUND %NOTFOUND %ROWCOUNT %ISOPEN Examples: if c1%found then if c1%isopen then DECLARE cursor C1 is select * from T order by ; c1_rec C1%rowtype; BEGIN open C1; loop fetch C1 into c1_rec; exit when C1%notfound; end loop; total_count := C1%rowcount; close C1; 8

Cursor FOR loop FOR <record> IN { <cursor name> (SELECT ) } LOOP <executable statements> END LOOP ; <record> is defined automatically as <cursor name>%rowtype FOR loop does automatically cursor open, fetch, and close operations Example: FOR emp_rec IN SELECT * FROM emp LOOP IF emp_rec.dept = 'D123' THEN salary_raise(emp_rec.emp_id, 15) ; END IF ; END LOOP ; Raising Exceptions Exception can be raised by PL/SQL engine RAISE statement RAISE <exception name> calling RAISE_APPLICATION_ERROR Exception handler can process and then re-raise the current exception by RAISE ; 9

Exception Handling BEGIN EXCEPTION When <exception name> then <statements>... When OTHERS then err_code := sqlcode; err_text := sqlerrm; insert into program_errors (error_code, error_text, error_time) values (err_code, err_text, sysdate); END; After exception processing the PL/SQL block is ended and control returns to the calling module or next statement after the END in case of local exceptions of a BEGIN-END block. Exceptions with PL/SQL mnemonics Exception name: for error code: Source: Stürner, 7 Cursor_already_open ORA-06511 dup_val_on_index ORA-00001 invalid_cursor ORA-01001 invalid_number ORA-01722 login_defined ORA-01017 no_data_found ORA-01403 not_logged_on ORA-01012 program_error ORA-06501 storage_error ORA-06500 timeout_on_resource ORA-00051 too_many_rows ORA-01422 transaction_backed_out ORA-00061 value_error ORA-06502 zero_divide ORA-01476 for the updated list with corresponding sqlcode values and explantions see PL/SQL User s Guide and Reference 10

Autonomous Transactions PROCEDURE main IS BEGIN INSERT ; My_at_proc ; UPDATE ; COMMIT; END; PROCEDURE My_at_proc IS PRAGMA AUTONOMOUS_TRANSACTION; BEGIN INSERT ; UPDATE ; DELET ; COMMIT; END; Changes in main transaction are not visible in the AT-transaction, but changes committed in the AT-transaction are visible in the calling transaction (even if it started after the main transaction) and other transactions. Developing a stand-alone procedure Create or Replace function/procedure p... Show errors function/procedure p PL/SQL compiler SQL processor Data Dictionary Error$ -tables Database object definitions Source / code Compiled code Status Dependencies 11

Create Function SQL > CREATE OR REPLACE FUNCTION <fname> (p1 <datatype>, p2<datatype>, ) RETURN <datatype> IS <PL/SQL block> The PL/SQL code is compiled by PL/SQL compiler parsed by SQL-processor and stored in the data dictionary Any errors are stored in the data dictionary table Error$ to accessed by views USER_ERRORS, ALL_ERRORS or DBA_ERRORS or SQL> SHOW ERRORS FUNCTION <fname> An example function SQL> CREATE OR REPLACE FUNCTION DayOfWeek (indate IN DATE) 2 RETURN SMALLINT 3 IS BEGIN 4 RETURN TO_NUMBER(TO_CHAR(indate,'D')); 5 END; 6 / Function created. SQL> SELECT DayOfWeek(SYSDATE) FROM DUAL; DAYOFWEEK(SYSDATE) ------------------ 3 Note: DUAL is a virtual single line table which is typically used for reporting current system data 12

Create Procedure SQL: CREATE OR REPLACE PROCEDURE <pname> (p1 IN <datatype>, p2 OUT <datatype>, ) IS <PL/SQL block> For full syntax see the SQL Reference manual SQL> SHOW ERRORS or SQL> SHOW ERRORS PROCEDURE <pname> or SQL> select * from user_errors where name = <pname> ; Create Trigger CREATE OR REPLACE <trigger name> { BEFORE AFTER INSTEAD OF} { {<trigger event> ON { NESTED TABLE <nested_table_column> OF <view> <table> <view>} [ <referencing clause> ] [FOR EACH ROW [WHEN <trigger condition> ] ] } <trigger event> ON DATABASE } <trigger body> ; INSERT UPDATE DELETE CREATE ALTER DROP LOGON LOGOF START SHUTDOWN SERVERERROR 13

Triggered RowVersioning CREATE TABLE VersionTest ( id INT, -- primary key s VARCHAR2(20), -- data columns.. rv NUMBER DEFAULT 0, -- reserved for triggered RowVersioning CONSTRAINT PK_VersionTest PRIMARY KEY (id) ) ; CREATE OR REPLACE TRIGGER TRG_VersionTest BEFORE UPDATE ON VersionTest FOR EACH ROW BEGIN :NEW.rv := :OLD.rv + 1; END; / PL/SQL Packages Encapsulation of modules Administration of procedures Managing access privileges Declaring global variables and constants available in the same session Hiding private procedures,.. Overloading 14

Specification part declaring the visible objects Creating the code and hidden objects Initialization block to be executed on the first call by a new user Creating PL/SQL Package Create or Replace Package pack1 as procedure p1 (a in number, ); function f1 (af in number) return number; var_1 ; my_ex exception; cursor c1; end pack1; Create or Replace Package Body pack1 as procedure p1 (a in number, ); <code> function f1 (af in number) return number; <code> procedure p_private ( ); <code> begin var_1 := <value>;... end pack1; Builtin Packages - Feuerstein 96 - PL/SQL packages DBMS_ALERT Notifications of database events DBMS_DDL Interface to some SQL DDL statements DBMS_JOB Automatic submitting of scheduled database tasks DBMS_LOCK Interface for user defined locks using OLM DBMS_MAIL Interface to Mail ( <= 9) DBMS_OUTPUT Messaging from PL/SQL programs DBMS_PIPE ShMem messaging between sessions DBMS_SESSION Interface to session level commands DBMS_SQL Dynamic SQL from PL/SQL DBMS_TRANSACTION Interface for transaction options DBMS_UTILITY Miscellaneous PL/SQL routines DBMS_JAVA see Database Java Developer's Guide. 15

.. Builtin Packages DBMS_STREAMS DBMS_XDB DBMS_XML... DBMS_RLS... HTF functions generating HTML tags HTP procedures generating HTML tags OWA_... Web API, cookies etc SDO_... Spatial Data Objects? UTL_FILE reading and writing op-sys files UTL_HTTP communicating with Web servers UTL_SMTP communicating with mail servers Debugging with DBMS_OUTPUT dbms_output.enable (<buffersize>); dbms_output. put (var); put_line(var); new_line; Database Cache Pipe dbms_output.disable; SQL> set serveroutput on SQL> execute <proc> ; <lines displayed on the terminal> dbms_output. get_line (line,status); get_lines (lines, numlines, status); 16

DBMS_PIPE session A Database Cache session B dbms_pipe. reset_buffer pack_message(ov); send_message(f); Pipe dbms_pipe. receive_message(ov) unpack:message(ov) DBMS_MAIL transaction Select update.... dbms_mail.send.. Commit / Rollback After successful commit 17

DBMS_LOCK Package for explicit locking (by integer values) user defined locks between 1-1073741823 Allocate_unique - name associated for integers 1073741824- Request - request a lock with a given mode Convert - convert the mode of a lock Sleep (secs) - suspend the session for a given period of time Release - releasing the lock A better Sleep ( ) CONNECT System@ORCL -- Source: 10g R2 PL/SQL User's Guide and Reference CREATE PROCEDURE java_sleep (milli_seconds IN NUMBER) AS LANGUAGE JAVA NAME 'java.lang.thread.sleep(long)'; / CREATE PROCEDURE sleep (seconds IN NUMBER) IS BEGIN java_sleep (seconds * 1000); END; / CREATE PUBLIC SYNONYM java_sleep FOR system.java_sleep; CREATE PUBLIC SYNONYM sleep FOR system.sleep; / GRANT EXECUTE ON java_sleep TO PUBLIC; GRANT EXECUTE ON sleep TO PUBLIC; / CALL sleep(10); 18