Database Journal
MS SQL Oracle DB2 Access MySQL PostgreSQL Sybase PHP SQL Etc SQL Scripts & Samples Tips Database Forum

» Database Journal Home
» Database Articles
» Database Tutorials
MS Access
SQL Scripts & Samples
» Database Forum
» Slideshows
» Sitemap
Free Newsletters:

By submitting your information, you agree that databasejournal.com may send you databasejournal offers via email, phone and text message, as well as email offers about other products and services that databasejournal believes may be of interest to you. databasejournal will process your information in accordance with the Quinstreet Privacy Policy.

News Via RSS Feed

Database Journal |DBA Support |SQLCourse |SQLCourse2

Featured Database Articles


Posted Jun 4, 2001

Learning the Basics of PL/SQL - Page 2

By Jonathan Gennick

Getting Started with PL/SQL

By now you should have a basic understanding of what PL/SQL is and how it relates to other Oracle products. You should have access to an Oracle database environment either at work or at home. During the rest of this chapter, you will learn some of the basics of PL/SQL, and you will write your first Oracle stored function.

PL/SQL Is Block Structured

New Term - PL/SQL is referred to as a block structured language A PL/SQL block is a syntactical unit that might contain program code, variable declarations, error handlers, procedures, functions, and even other PL/SQL blocks.

The Syntax for a PL/SQL Block

In this syntax, variable_declarations are any variables that you might want to define. Cursor definitions and nested PL/SQL procedures and functions are also defined here. program_code refers to the PL/SQL statements that make up the block. exception_handlers refers to program code that gets triggered in the event of a runtime error or exception.

The declaration section of a PL/SQL block is optional, although in practice it is unusual not to have any declarations at all. The exception handler portion of a PL/SQL block is also optional, and you won't see much of it until Day 7, "Procedures, Packages, Errors, and Exceptions."

Note - When you're defining PL/SQL functions, procedures, and triggers, the keyword DECLARE is not used. When defining a function, the function specification, or function header as it is sometimes called, begins the block. Similarly, procedure and trigger specifications begin procedure and trigger blocks. Function, procedure, and trigger blocks are covered in more detail on Day 2, "Writing Declarations and Blocks."

New Term - Any variable declarations must immediately follow DECLARE and come before BEGIN. The BEGIN and END keywords delimit the procedural portion of the block. This is where the code goes. The EXCEPTION keyword signifies the end of the main body of code, and begins the section containing exception handling code. The semicolon at the end of the block, and at the end of each statement, is the PL/SQL statement terminator, and signifies the end of the block.

Tip - Omitting the semicolon at the end of a block is a common oversight. Leave it off, and you'll get a syntax error. Remember to include it and you will save yourself lots of aggravation.

Blocks such as the one shown in "The Syntax for a PL/SQL Block" form the basis for all PL/SQL programming. An Oracle stored procedure consists of one PL/SQL block. An Oracle stored function consists of one PL/SQL block. An Oracle database trigger consists of one PL/SQL block. It is not possible to execute PL/SQL code except as part of a block.

PL/SQL blocks can be nested. One block can contain another block as in the following example:

 variable declarations go here
 some program code
  code in a nested block
 more program code

Nesting of blocks is often done for error-handling purposes. You will read more about error handling on Day 7.

Compiling and Executing a Simple Block

Are you ready to try writing your first PL/SQL code? Good. Remember that for this and all other examples in this book, you will be using SQL*Plus to send the PL/SQL code to the Oracle database for execution.

  1. Begin by running SQL*Plus and connecting to your Oracle database. Your initial SQL*Plus screen should look like the one shown in Figure 1.2.

  2. Next, type in the following lines of code from Listing 1.1 exactly as shown. Notice the slash at the end. It must be typed in as well, exactly as shown.

Initial SQL*Plus Screen
Initial SQL*Plus screen.

Listing 1.1 Your First PL/SQL Block
 x   NUMBER;
 x := 72600;

Tip - The slash at the end tells SQL*Plus that you are done typing PL/SQL code. SQL*Plus will then transmit that code to the Oracle database for execution. The slash has meaning to SQL*Plus only, not to PL/SQL.

Tip - The slash character must be typed on a line by itself, and it must be the first character on that line; otherwise, it will get sent to the database and generate an error message.

After you type the slash, SQL*Plus transmits your code to Oracle for execution. After your code executes, your output should look like the following:

 x  integer;
 x := 65400;

PL/SQL procedure successfully completed

The code you just executed was probably not very exciting, possibly because there was no output. PL/SQL does have some limited output facilities, and next you will learn how to produce some simple screen output.

What About Some Output?

When it was originally designed, PL/SQL had no output facilities at all. Remember that PL/SQL is not a standalone language. It is almost always used in conjunction with some other program or tool that handles the input, output, and other user interaction.

Oracle now includes the DBMS_OUTPUT package with PL/SQL, which provides you with some limited output capabilities. You will learn more about packages during Day 8, "Using SQL," but for now it's enough to know that you can use the dbms_output.put_line procedure as shown in Listing 1.2.

Listing 1.2 PL/SQL Block Showing the Use of the dbms_output.put_line Procedure
 x   NUMBER;
 x := 72600;
 dbms_output.put_line('The variable X = ');

The dbms_output.put_line() procedure takes exactly one argument and generates a line of text as output from the database server. In order for you to see that line of text, you must tell SQL*Plus to display it. This is done with the SQL*Plus command:

  1. Type the preceding command now. It needs to be executed only once per session, so you won't need to reissue it unless you exit SQL*Plus and get back in again.

  2. Next, type in the PL/SQL code from Listing 1.2. The resulting output from SQL*Plus should look like that shown below.

    The variable x=

Note - It is SQL*Plus that prints the server output on the screen for you to see. You must remember to execute the SET SERVEROUTPUT ON command, or you won't see any output. You also can use the SET SERVEROUTPUT OFF command to turn off output when you don't want to see it.

Oracle Archives

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Latest Forum Threads
Oracle Forum
Topic By Replies Updated
Oracle Data Mining: Classification jan.hasller 0 July 5th, 07:19 AM
Find duplicates - Unique IDs Lava 5 July 2nd, 08:30 AM
no matching unique or primary key rcanter 1 April 25th, 12:32 PM
Update values of one table based on condition of values in other table using Trigger Gladiator 3 February 29th, 06:01 PM

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.