What is PL/SQL? 1.1 1. What is PL/SQL? PL/SQL storage and execution Different types of PL/SQL programs Call PL/SQL from different environments SKILLBUILDERS
What is PL/SQL? 1.2 1.2 What is PL/SQL? PL/SQL is a procedural language for the ORACLE server and its clients Key concept: SQL and PL/SQL combine to provide the following features: SQL - data manipulation and transaction processing PL/SQL - third generation procedural language: variable definition, assignment, conditional processing, looping constructs, file I/O, subprograms, error handling, etc. What is PL/SQL? PL/SQL is a procedural language for the ORACLE server and its clients. PL/SQL stands for Procedural Language extensions to SQL. Programs stored on the client side are called anonymous blocks. Server side stored PL/SQL programs are called triggers, stored procedures, or stored functions. Client-side products such as Oracle Forms also support PL/SQL.
What is PL/SQL? 1.3 PL/SQL Capabilities & Restrictions Capabilities PL/SQL can call: DML statements standard SQL functions other PL/SQL programs 1.3 Restrictions PL/SQL programs cannot directly contain DDL statements PL/SQL Capabilities & Restrictions Capabilities All standard SQL functions except the DECODE function and the aggregate functions (SUM, COUNT, MAX, MIN, etc.) may be used in PL/SQL. The DECODE and aggregate functions can, however, be used in SQL statements within PL/SQL programs. Restrictions PL/SQL can indirectly contain DDL statements (ALTER, CREATE, etc.) via the use of native dynamic SQL. This topic is beyond the scope of this course.
What is PL/SQL? 1.4 1.4 Types of PL/SQL Programs Anonymous blocks (also called unnamed blocks) Triggers Stored Procedures Stored Functions Types of PL/SQL Programs Anonymous Blocks are called anonymous because they are not stored and named within the server. They are also sometimes referred to as unnamed blocks. However, they can be stored in an operating system file, not within the database. Anonymous blocks are usually executed under SQL*PLUS to: update table data produce reports build database objects Triggers are, usually, code associated with a table and are executed automatically when an appropriate SQL statement is issued against the table. There are also triggers which are associated with system events such as Startup and Shutdown or DDL such as CREATE and DROP statements. Other triggers can be created which are associated with user events such as Logon or Logoff. Triggers are compiled and stored permanently in the Oracle server. Triggers are used to: Enforce complex business and/or integrity rules Audit modifications to a table Derive column values Maintain mirror tables continues
What is PL/SQL? 1.5 Types of programs continued Stored Procedures and Stored Functions are callable subprograms which are also compiled and stored permanently in the Oracle server. The main difference between the two is that stored functions must return a value to the caller. Otherwise, they are basically the same. There are many benefits in using stored procedures and functions including: increased performance shared code lowered maintenance cost portability speed (compiled) security
What is PL/SQL? 1.6 Executing PL/SQL 1.6 Client Server DECLARE x NUMBER; BEGIN INSERT INTO... SELECT x INTO x... IF x > 10 THEN... END; EXECUTE PROCEDURE... Server DB Files Procedures Functions Triggers Network Client with PL/SQL Call procedure Retrieve results Server DB Files Server Memory Network Client w/o PL/SQL INSERT INTO t1 INSERT INTO t2 INSERT INTO t3 SELECT * FROM t1 Key concept: A single call to the database invokes the procedure or function. Before stored procedures and functions, you had to make a separate call to the database for each SQL statement. The result was high network traffic and overhead.
What is PL/SQL? 1.7 1.7 PL/SQL Subprograms PL/SQL program units are subprograms called by: Client (Visual Basic, Oracle Forms) Server (triggers, scheduled jobs) 3GL s (C, C++, COBOL) SQL*Plus PL/SQL Subprograms PL/SQL program units are subprograms called by a client application, the server, a 3GL or a host development environment such as SQL*Plus. Anonymous block usage is not the norm. You will use anonymous blocks for quick and dirty tasks in development. Production level PL/SQL program units will be callable subprograms, not SQL*Plus scripts!
What is PL/SQL? 1.8 Storage PL/SQL Storage & Execution On the server Triggers Stored procedures Stored functions On the client Anonymous blocks Execution All execute on the server 1.8 PL/SQL Storage & Execution All types of PL/SQL programs except for anonymous blocks are stored on the server. Execution of any PL/SQL program, regardless of its type, is done on the server. In other words, PL/SQL is a server side language. How are PL/SQL programs executed? With the exception of triggers, other PL/SQL program types are executed by calling them in code. Triggers, on the other hand, are automatically executed by Oracle when the appropriate triggering event occurs.
What is PL/SQL? 1.9 1.9 PL/SQL Program Structure PL/SQL is a block-structured language A PL/SQL block consists of 3 parts Declarative Executable Exception Handling DECLARE variables; constants; cursors; BEGIN BEGIN PL/SQL and and SQL SQL DML DML statements; EXCEPTION exception handlers; END; END; PL/SQL Program Structure PL/SQL is a block-structured language. Each PL/SQL program consists of 3 sections, or parts. The declarative section contains all declarations for variables, constants and cursors. The executable section contains the main body of PL/SQL and SQL statements. Finally, the exception handling section is where all errors can be trapped and resolved. Syntax Each line of code in a PL/SQL program must end in a semicolon. In that way, PL/SQL is like SQL where every line must also end in a semicolon. SQL*Plus, on the other hand, terminates at the end of the physical line and as such does not use a statement terminator. If you wish to continue a SQL*Plus command onto the next physical line you need a dash as a continuation character.
What is PL/SQL? 1.10 10 Anonymous Block Example Copy a customer to the cust_history table 1.10 10 declare v_custno number := := 1 ; begin INSERT INTO cust_history SELECT * FROM customer WHERE cust_no = v_custno; end; / Anonymous Block Example Place the code for an anonymous block in a.sql script file. Note that we follow our anonymous block with a slash (/) on a line by itself. When we run the script from SQL*Plus, the anonymous block is read into the SQL*Plus buffer. The slash tells SQL*Plus to execute the contents of the SQL*Plus buffer. Note that the SQL*Plus buffer can hold one and only one SQL command or PL/SQL block. We have hard coded the customer number in the above example. You could use a SQL*Plus substitution variable in place of the hard coded customer number. It you do, this PL/SQL block must be executed from SQL*Plus. Only SQL*Plus understands SQL*Plus substitution variables! Coding Anonymous Blocks with the SQL*Plus buffer editor All normal SQL*Plus buffer editor commands work as they did for SQL commands. It is the same SQL*Plus buffer editor. But to end editing of an SQL statement we just entered a blank line. This worked because blank lines are not allowed in an SQL statement. But blank lines are allowed in a PL/SQL block for readability. Therefore SQL*Plus needs a different way of detecting that you want to end the buffer editor session. We tell SQL*Plus that we are done with the edit session by typing a period on a line by itself.
What is PL/SQL? 1.11 11 Anonymous Block Compile Errors 1.11 11 declare v_custno number := := 1 ; begin begin INSRET INTO INTO cust_history SELECT * FROM FROM customer WHERE WHERE cust_no = v_custno; end; end; / INSRET INTO INTO cust_history * ERROR ERROR at at line line 4: 4: ORA-06550: line line 4, 4, column 10: 10: PLS-00103: Encountered the the symbol "INTO" when when expecting one one of of the the following: := :=. ( @ % ; Anonymous Block Compile Errors Note the misleading error message. The problem is that INSERT is misspelled. While the error messages may not be as clear as we would like, notice the * is under the misspelled INSRET. When you execute an anonymous block, SQL*Plus sends it the the server to be compiled and executed. If the program compiles correctly, Oracle will execute it and return the results to SQL*Plus. If the program does not compile correctly, Oracle will return the compile errors to SQL*Plus. SQL*Plus will then display either the results or the errors to your screen. Later we will see that compile errors in triggers, stored procedures, and stored functions are not automatically displayed by SQL*Plus. You must specifically display them yourself.
What is PL/SQL? 1.12 12 PL/SQL Output to SQL*Plus PL/SQL may display output to SQL*Plus using the system procedure dbms_output.put_line Example 1.12 12 begin dbms_output.put_line('hello World') ; end end ; / PL/SQL Output to SQL*Plus PL/SQL may display output to SQL*Plus by using the Oracle supplied package procedure called DBMS_OUTPUT.PUT_LINE. This procedure displays text on the output device (console) in SQL*Plus. In order for the output from DBMS_OUTPUT.PUT_LINE to be seen, the SET SERVEROUTPUT on SQL*Plus command must be executed. You may want to consider putting this command in your LOGIN.SQL file as this setting reverts to the default (off) when the session ends.
What is PL/SQL? 1.13 13 Tools for PL/SQL Development SQL*Plus is the traditional tool Included with all Oracle database products Procedure Builder Sophisticated IDE GUI tool An extra cost product Sophisticated debugging features Breakpoints stepping through code variable examination, etc. 1.13 13 Tools for PL/SQL Development SQL*Plus is the traditional tool for PL/SQL development as it is included with all Oracle database products. You may choose to use a more robust development tool, like Procedure Builder, which provides a graphical development environment. This product has to be purchased separately but may be worth the extra cost as it provides many sophisticated debugging features and a better overall working environment for your development efforts. There are many third party tools also available (e.g. TOAD, www.toadsoft.com).
What is PL/SQL? 1.14 14 1.14 14 Working in SQL*Plus SET ECHO ON can be used to show compiled source on your screen Error messages will refer to a line and column number ECHO output will show line numbers Oracle Error messages manual is available to help Working in SQL*Plus Debugging your PL/SQL programs can be tedious if you are uncertain of where the error messages are pointing to. The simplest way to see the errors is to SET ECHO ON prior to the compile of your source. (@programname) You will have the program source echoed on your screen with line numbers. If there are any error messages, it will be simple to ascertain where the error occurred. You can also get the source line number from your USER_SOURCE library. You cannot just count lines in your original.sql file because the PL/SQL compiler does not count blank lines and some comment lines. It is inaccurate. Sometimes the messages are so clear you do not need to know the line number. Most often, messages are somewhat obscure and you can use all the help you can get!
What is PL/SQL? 1.15 15 PL/SQL HelloWorld Workshop PL/SQL Hello World 1.15 15 Workshop 1. No course is complete without the infamous Hello World! application. So let s create an anonymous PL/SQL block to print the string Hello World! to the screen. Code the PL/SQL block in a script, and test it by running the script. 2. Create an script called copy_to_history containing an anonymous PL/SQL block to copy a customer from the Customer table into the Cust_history table. Prompt the user for the customer number to be copied. 3. Verify that the copy worked.