Exforsys.com
 
Home Tutorials Oracle 9i
 

Tutorial 1: Introduction to Oracle 9i:SQL, PLSQL. and SQL *Plus

 

Introduction to SQL

Page 2 of 2


All of these block types share most PL/SQL features so during this tutorial the features that apply to all block types will be grouped into single subjects.



Sample Code
  1. Definition OF ANY variables OR objects that are used within the declared block.
  2. Statements that make up the block.
  3. ALL EXCEPTION handlers.
  4. END OF block marker.
  5.  
  6. TEMP_COST NUMBER(10,2);
  7. SELECT COST INTO TEMP_COST FROM JD11.BOOK WHERE ISBN = 21;
  8. IF TEMP_COST > 0 THEN
  9. UPDATE JD11.BOOK SET COST = (TEMP_COST*1.175) WHERE ISBN = 21;
  10. UPDATE JD11.BOOK SET COST = 21.32 WHERE ISBN = 21;
  11. INSERT INTO JD11.ERRORS (CODE, MESSAGE) VALUES(99, 'ISBN 21 NOT FOUND');
Copyright exforsys.com


As you can see there are several elements in the example that haven't been covered in the SQL tutorial, these elements are the PL/SQL extensions. They include:


Variables and Constants These objects are used to store and manipulate block level data. They can be CHAR, VARCHAR2, NUMBER, DATE or BOOLEAN data types.
SQL support All SQL statements are supported within PL/SQL blocks including transaction control statements.
Composite Data types Records allow groups of fields to be defined and manipulated in PL/SQL blocks.
Flow Control If, Loop, GOTO and Labels provide conditional actions, tests, branching and iterative program control.
Built-in functions Most SQL data functions are supported within PL/SQL blocks.
Cursor handling Cursors (a memory area holding a result set) can be explicitly defined and manipulated allowing the processing of multiple rows. A group of PL/SQL system attributes provide the ability to test a cursor's internal state.
Exception handling Blocks have the ability to trap and handle local error conditions (implicit exceptions). You may also self generate explicit exceptions that deal with logic and data errors.
Code storage Blocks may be stored within an Oracle database as procedures, functions, packages (a group of blocks) and triggers.


Rules of block Structure


  1. Every unit of PL/SQL must constitute a block. As a minimum there must be the delimiting words BEGIN and END around the executable statements.
  2. SELECT statements within PL/SQL blocks are embedded SQL (an ANSI category). As such they must return one row only. SELECT statements that return no rows or more than one row will generate an error. If you want to deal with groups of rows you must place the returned data into a cursor. The INTO clause is mandatory for SELECT statements within PL/SQL blocks (which are not within a cursor definition), you must store the returned values from a SELECT.
  3. If PL/SQL variables or objects are defined for use in a block then you must also have a DECLARE section.
  4. If you include an EXCEPTION section the statements within it are only processed if the condition to which they refer occurs. Block execution is terminated after an exception handling routine is executed.
  5. PL/SQL blocks may be nested, nesting can occur wherever an executable statement could be placed (including the EXCEPTION section).

Your first example in PL/SQL will be an anonymous block --that is a short program that is ran once, but that is neither named nor stored persistently in the database.


SQL> SET SERVEROUTPUT ON
SQL> BEGIN
2 dbms_output.put_line('First World');
3 END;
4 /



PL/SQL code is compiled by submitting it to SQL*Plus. Unless your program is an anonymous block, your errors will not be reported. Instead, SQL*Plus will display the message ``warning: procedure created with compilation errors''. You will then need to type:


SQL> SHOW ERRORS



to see your errors listed. If you do not understand the error message and you are using Oracle on UNIX, you may be able to get a more detailed description using the oerr utility, otherwise use Oracle's documentation (see References section).


SQL> SELECT sysdate FROM DUAL
2 /



Next, we will rewrite the anonymous block above as a procedure. Note that we now use the user function to greet the user.


CREATE OR REPLACE PROCEDURE welcome
IS
user_name VARCHAR2(8) := user;
BEGIN -- `BEGIN' ex
dbms_output.put_line('First World, '
|| user_name || '!');
END;
/



Make sure you understand the changes made in the code: Once you have compiled the procedure, execute it using the EXEC command.


SQL> EXEC welcome




Generating Output on Screen


The built-in packages offer a number of ways to generate output from within your PL/SQL program. While updating a database table is, of course, a form of "output" from PL/SQL, this chapter shows you how to use two packages that explicitly generate output. UTL_FILE reads and writes information in server-side files, and DBMS_OUTPUT displays information to your screen.


Here is a very good chapter from O'Reilly which helps to understand the complete concept and usage of “Generating Output on Screen”  Click here to Read




First Page: Tutorial 1: Introduction to Oracle 9i:SQL, PLSQL. and SQL *Plus


Read Next: Tutorial 2: SQL *Plus Commands



 

 

Comments


rags23 said:

  its really fantastic to loacte the reqvired matter here. thanx
November 11, 2006, 5:46 pm

vishakha arawal said:

  thanks so much
September 26, 2007, 10:11 am

ravikr said:

  how will transfer the data from flat file to database by using pl/sql
November 9, 2007, 1:57 am

hongsungwon said:

  thanks! sir
November 10, 2008, 1:51 am

SARATH.P.R said:

  This tutorial is very help full for beginers
December 11, 2008, 2:18 am

Magu said:

  Good one for beginners to learn sql
December 18, 2008, 3:33 pm

maria said:

  thanks for your help. This tutorial is very ususfull for us beginers.
December 23, 2008, 3:47 am

sandesh shivaraju said:

  where will i get the solutions for the practice questions in the book
introduction to oracle9i:sql
one more question is,
what is the query to display the last name and the job title of a table who do not have a manager?
December 30, 2008, 2:29 am

Ali said:

  Thanks For this hard works Great.
January 25, 2009, 4:40 am

R.SARANATHAN said:

  I would like to look for the course around chennai on Oracle apps supply chain mgmt and financials. [Oracle certified institute]
January 27, 2009, 12:42 am

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links

 

Subscribe via RSS


Get Daily Updates via Subscribe to Exforsys Free Training via email


Get Latest Free Training Updates delivered directly to your Inbox...

Enter your email address:


 

Subscribe to Exforsys Free Training via RSS
 

 
Partners -  Privacy and Legal Policy -  Site News -  Contact   Sitemap  

Copyright © 2000 - 2009 exforsys.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape