24
Introduction to PL/SQL 1.1 © 2006 SkillBuilders, Inc. V2.1 © 2006 SkillBuilders, Inc. SKILLBUILDERS Lesson 1 Introduction to PL/SQL A programmer’s introduction to the what, why, when and where of PL/SQL.

Introduction to PL/ Lesson 1 Introduction to PL/SQL

Embed Size (px)

Citation preview

Introduction to PL/SQL 1.1

© 2006 SkillBuilders, Inc. V2.1

© 2006 SkillBuilders, Inc.SKILLBUILDERS

Lesson 1Introduction to PL/SQL

A programmer’s introduction to the what, why, when and where of

PL/SQL.

Introduction to PL/SQL 1.2

© 2006 SkillBuilders, Inc. V2.1

© 2006 SkillBuilders, Inc.

1.2

Lesson Objectives

What is PL/SQL?What is it good for?Basic structure of a PL/SQL programSimple examples of common PL/SQL objects

Anonymous blockProcedureFunctionPackageTrigger

Tips for working in SQL*Plus

In-depth lessons dedicated to these

subjects later in this course…

Introduction to PL/SQL 1.3

© 2006 SkillBuilders, Inc. V2.1

© 2006 SkillBuilders, Inc.

1.3

What is PL/SQL?

PL/SQL = Procedural Language extensions for SQLProprietary language for Oracle database3rd generation procedural language

variable definition, assignmentconditional processing

IF and CASElooping constructserror handling

Seamless integration of SQL, SQL functions

PL/SQL is a procedural language for the ORACLE database. PL/SQL stands for Procedural Language extensions to SQL. It is a proprietary language; i.e. a PL/SQL program will not run on a SQL Server database.

As a 3rd generation language, it provides many of the standard capabilities you would expect including:

Variable definition and assignmentConditional processing (IF and CASE statements)Loop constructsError handing

It provides seamless integration (embedding) of SQL and SQL functions.

Note that Oracle client-side products such as Oracle Forms also support PL/SQL.

۞ PL/SQL is the equivalent of Transact-SQL (T-SQL) in Sybase and MS SQL Server.

Introduction to PL/SQL 1.4

© 2006 SkillBuilders, Inc. V2.1

© 2006 SkillBuilders, Inc.

1.4

Why Use PL/SQL?...

Efficient data manipulationGood for lots of SQL, limited procedural codeTight integration with SQL

Embed in PL/SQL statementsLots of work done automatically

Open / close queries and cursorsCompatible datatypes

No conversions

Not great for lots of complex procedural codeHowever, consider Native Compilation

Because of the tight integration of SQL and PL/SQL, PL/SQL is very good at data manipulation. So if you are coding a routine that contains lots of SELECT, UPDATE, DELETE and INSERT statements, with relatively little procedural code, PL/SQL is a good choice. In this course you will see how PL/SQL does many things automatically for you; e.g. open and close cursors.

Conversely, if you writing complex procedural code that will be repeatedly executed, Java or C might be a better choice. However, with the PL/SQL Native Compilation feature, PL/SQL is becoming a consideration even for computationally intensive routines.

Introduction to PL/SQL 1.5

© 2006 SkillBuilders, Inc. V2.1

© 2006 SkillBuilders, Inc.

1.5

...Why Use PL/SQL?

Simple languageLearn quickly Only need SQL*Plus to develop & deploy

Portable within Oracle databaseNo code change to migrate to another Oracle server

Even on different platform

Callable from any clientJava, .NET, COBOLAnyone who can connect to DB can call

PL/SQL is relatively simple to learn and you’ll only need SQL*Plus to develop, test and deploy PL/SQL programs.

PL/SQL programs are portable to any Oracle database running on any platform. Rarely, if ever, will code changes be required. An exception would be if specific OS paths or commands are hard-coded within a program.

PL/SQL programs are callable from any type of client. If the client can connect to the database, it can call a PL/SQL procedure or function – and that procedure can return a value or result set to the caller.

Introduction to PL/SQL 1.6

© 2006 SkillBuilders, Inc. V2.1

© 2006 SkillBuilders, Inc.

1.6

PL/SQL Program Structure

PL/SQL is a block-structured language 3 possible blocksOnly BEGIN block required

DECLARE

variables; constants; cursors;

BEGIN

PL/SQL and SQL statements;

EXCEPTION

exception handlers;

END;

DECLARE

variables; constants; cursors;

BEGIN

PL/SQL and SQL statements;

EXCEPTION

exception handlers;

END;

Declarative

Executable

Error

Handling

Terminate each statement or

declaration with semi-colon

PL/SQL is a block-structured language. Each PL/SQL program consists of up to 3 sections, orblocks. The declarative block contains all declarations for variables, constants and cursors. The BEGIN block contains the main body of PL/SQL and SQL statements; it is the only required section. Finally, the exception block is where all errors (exceptions in PL/SQL) can be trapped and resolved, i.e. “handled”.

Each declaration or statement must end in a semicolon.

Introduction to PL/SQL 1.7

© 2006 SkillBuilders, Inc. V2.1

© 2006 SkillBuilders, Inc.

1.7

Anonymous Blocks

Execute block from SQL*Plus

SQL> @copycust 3SQL> declare2 v_custno number := &1 ;3 begin4 INSERT INTO cust_history5 SELECT *6 FROM customer7 WHERE cust_no = v_custno;8 end;9 /

old 2: v_custno number := &1 ;new 2: v_custno number := 3 ;

PL/SQL procedure successfully completed.

SQL> @copycust 3SQL> declare2 v_custno number := &1 ;3 begin4 INSERT INTO cust_history5 SELECT *6 FROM customer7 WHERE cust_no = v_custno;8 end;9 /

old 2: v_custno number := &1 ;new 2: v_custno number := 3 ;

PL/SQL procedure successfully completed.

Copy customer to the history table

Don’t forget the slash

Seamless integration of SQL

Anonymous Blocks are called anonymous because they are not stored on the database – thus they do not have a name. They are also sometimes referred to as unnamed blocks. However, as this example illustrates, they can be stored in an operating system file (“COPYCUST.SQL”) Anonymous blocks are usually executed under SQL*PLUS to:

update table dataproduce reports build database objects

Typically, the code for an anonymous block is placed 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 (with the “at” sign), the anonymous block is read into the SQL*Plus buffer and scanned for SQL*Plus substitution variables. The slash tells SQL*Plus to send the contents of the SQL*Plus buffer to the server for execution.

Note that the SQL*Plus buffer can hold one and only one SQL command or PL/SQL block.

I have used a SQL*Plus substitution variable in place of the hard coded customer number. Thus, in this case, this PL/SQL block must be executed from SQL*Plus. Only SQL*Plus understands SQL*Plus substitution variables!

Notes continue on the next page…

Introduction to PL/SQL 1.8

© 2006 SkillBuilders, Inc. V2.1

SQL*Plus Tips

Optionally, use SET ECHO OFF to suppress the display of the anonymous block when executed from the SQL*Plus command prompt.

Optionally, use SET VERIFY OFF to suppress the display of the “old” and “new” substitution messages.

Supplemental Notes

Note that the example shown here is for educational purposes only – specifically to illustrate the block structure of a PL/SQL program. The SQL statement within the BEGIN block could more easily be executed without being coded within the confines of a PL/SQL block.

Introduction to PL/SQL 1.9

© 2006 SkillBuilders, Inc. V2.1

© 2006 SkillBuilders, Inc.

1.9

Compile Errors

SQL> @copycust 6INSRET INTO cust_history

*ERROR at line 4:ORA-06550: line 4, column 10:PLS-00103: Encountered the symbol "INTO" when expecting one of the following::= . ( @ % ;ORA-06550: line 8, column 1:PLS-00103: Encountered the symbol "END"

SQL> @copycust 6INSRET INTO cust_history

*ERROR at line 4:ORA-06550: line 4, column 10:PLS-00103: Encountered the symbol "INTO" when expecting one of the following::= . ( @ % ;ORA-06550: line 8, column 1:PLS-00103: Encountered the symbol "END"

“*” will be somewhere near

clause that caused error

While the error message has identified the correct line (line 4 contains INSERT spelled incorrectly), note the misleading error message.

Later we will see that compile errors in triggers, stored procedures, and stored functions are notautomatically displayed by SQL*Plus. You must specifically display them yourself with the SQL*Plus SHOW ERRORS command.

Introduction to PL/SQL 1.10

© 2006 SkillBuilders, Inc. V2.1

© 2006 SkillBuilders, Inc.

1.10

Output to SQL*Plus

Use DBMS_OUTPUT.PUT_LINE to display text on screen

SQL> set serveroutput onSQL> begin2 dbms_output.put_line('Hello World') ;3 end ;4 /

Hello World

PL/SQL procedure successfully completed.

SQL> set serveroutput onSQL> begin2 dbms_output.put_line('Hello World') ;3 end ;4 /

Hello World

PL/SQL procedure successfully completed.

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.

Introduction to PL/SQL 1.11

© 2006 SkillBuilders, Inc. V2.1

© 2006 SkillBuilders, Inc.

1.11

ProceduresSQL> @procedure1SQL> create procedure p1 (p_output in varchar2) as 2 begin3 dbms_output.put_line(p_output); 4 end;5 /

Procedure created.

SQL> exec p1(‘Hello World!’)Hello World

PL/SQL procedure successfully completed.

SQL> @procedure1SQL> create procedure p1 (p_output in varchar2) as 2 begin3 dbms_output.put_line(p_output); 4 end;5 /

Procedure created.

SQL> exec p1(‘Hello World!’)Hello World

PL/SQL procedure successfully completed.

Procedure is now compiled and

stored in database

Call the procedure with the SQL*Plus

EXECUTE command

Procedures and functions are callable subprograms which are also compiled and stored permanently in the Oracle server. (The main difference between procedures and functions is that stored functions must return a value to the caller. Otherwise, they are basically the same. You will learn much more about this later in this course.)

In the example shown above, I coded a simple stored procedure in file “PROCEDURE1.SQL”. The procedure is created with the CREATE PROCEDURE statement (refer to the Oracle9i SQL Reference for more information on this statement; you will learn a lot more about this statement later in this course). When I run the file, it compiles and stores the stored procedure in the database. Note that the name of the procedure is “p1”; the name of the OS file that contains the source code is “PROCEDURE1.SQL”. I use the SQL*Plus EXECUTE command (“exec p1”) to call the stored procedure.

Introduction to PL/SQL 1.12

© 2006 SkillBuilders, Inc. V2.1

© 2006 SkillBuilders, Inc.

1.12

Procedure Compile ErrorsSQL> drop procedure p1;Procedure dropped.

SQL> @procedure1SQL> create procedure p1 (p_output in varchar2) as 2 begin3 dbms_output.put_line(p_output); 4 end;5 /

Warning: Procedure created with compilation errors.

SQL> show errorsErrors for PROCEDURE P1:

LINE/COL ERROR-------- ------------------------------------------------------4/1 PLS-00103: Encountered the symbol "END" when expecting

SQL> drop procedure p1;Procedure dropped.

SQL> @procedure1SQL> create procedure p1 (p_output in varchar2) as 2 begin3 dbms_output.put_line(p_output); 4 end;5 /

Warning: Procedure created with compilation errors.

SQL> show errorsErrors for PROCEDURE P1:

LINE/COL ERROR-------- ------------------------------------------------------4/1 PLS-00103: Encountered the symbol "END" when expecting

Drop procedure before trying to

recreate, or use “or replace” on create

Use the SQL*Plus SHOW ERRORS

command

Compile errors generated by the CREATE PROCEDURE (and CREATE FUNCTION, CREATE TRIGGER) statements are not automatically displayed on the SQL*Plus screen. These errors are stored in a data dictionary view called USER_ERRORS. They can be displayed by querying this view or, as shown in this slide, by using the SQL*Plus SHOW ERRORS command. The complete text of the error message is:

LINE/COL ERROR

-------- -----------------------------------------------

4/1 PLS-00103: Encountered the symbol "END" when expecting one of the following: := . ( % ;

The symbol ";" was substituted for "END" to continue.

The problem is a missing semi-colon at the end of line 3.

Notes continue on the next page…

Introduction to PL/SQL 1.13

© 2006 SkillBuilders, Inc. V2.1

Supplemental Notes

Note that instead of using DROP PROCEDURE you can add the “OR REPLACE” option to the CREATE PROCEDURE command:

SQL> @p1

SQL> create or replace procedure p1 (p_output in varchar2) as

2 begin

3 dbms_output.put_line(p_output);

4 end;

5 /

Procedure created.

The DROP statement can be used to drop most Oracle procedural objects, e.g. DROP FUNCTION, DROP TRIGGER, DROP PACKAGE, DROP PACKAGE BODY etc.

Introduction to PL/SQL 1.14

© 2006 SkillBuilders, Inc. V2.1

© 2006 SkillBuilders, Inc.

1.14

FunctionsSQL> @function1SQL> create or replace function f12 return char3 as4 begin5 return ('test');6 end;7 /

Function created.

SQL> select f1() from dual;

F1()--------------------------------------------test

SQL> @function1SQL> create or replace function f12 return char3 as4 begin5 return ('test');6 end;7 /

Function created.

SQL> select f1() from dual;

F1()--------------------------------------------test

Must identify datatype function

will return

Call function from an SQL or PL/SQL

statement

A PL/SQL function is very similar to a procedure; it is just that the function always returns a value to the caller and is invoked semantically different from the procedure. Whereas the procedure invocation is a statement in itself, the function is called within the context of another PL/SQL or SQL statement.

Introduction to PL/SQL 1.15

© 2006 SkillBuilders, Inc. V2.1

© 2006 SkillBuilders, Inc.

1.15

Packages...Package is a group of PL/SQL objects

SQL> create or replace package p1 as2 function f1 return char;3 procedure proc1;4 end;5 /

Package created.

SQL> create or replace package p1 as2 function f1 return char;3 procedure proc1;4 end;5 /

Package created.

SQL> create or replace package body p1 as2 function f1 return char3 as4 begin5 return ('test');6 end;78 procedure p1(p_output in varchar2) as 9 begin10 dbms_output.put_line(p_output); 11 end;12 end;13 /

SQL> create or replace package body p1 as2 function f1 return char3 as4 begin5 return ('test');6 end;78 procedure p1(p_output in varchar2) as 9 begin10 dbms_output.put_line(p_output); 11 end;12 end;13 /

A package is a group of PL/SQL objects often including procedures and functions. There are many benefits of packaging, which we will discuss in a subsequent lesson dedicated to packages. For now, it is just important to understand that PL/SQL objects can be packaged – as opposed to standalone objects.

Refer to the supplied script PACKAGE1.SQL for a working copy of the code shown here.

Introduction to PL/SQL 1.16

© 2006 SkillBuilders, Inc. V2.1

© 2006 SkillBuilders, Inc.

1.16

...Packages

Qualify packaged objects with package name

SQL> select p1.f1 from dual;

F1----------------------------------test

SQL> select p1.f1 from dual;

F1----------------------------------test

A packaged object is referenced by its package name, as shown above.

Introduction to PL/SQL 1.17

© 2006 SkillBuilders, Inc. V2.1

© 2006 SkillBuilders, Inc.

1.17

Data Dictionary

Dictionary records existence of PL/SQL objects

SQL> select object_name, object_type from user_objects2 where object_type in ('PROCEDURE', 'FUNCTION');

OBJECT_NAME OBJECT_TYPE------------------------- ------------------F1 FUNCTIONP1 PROCEDURE

SQL> select object_name, object_type from user_objects2 where object_type in ('PROCEDURE', 'FUNCTION');

OBJECT_NAME OBJECT_TYPE------------------------- ------------------F1 FUNCTIONP1 PROCEDURE

The existence of PL/SQL objects (procedures, functions, packages, etc) is recorded in the data dictionary. USER_OBJECTS contains one row for each object.

Introduction to PL/SQL 1.18

© 2006 SkillBuilders, Inc. V2.1

© 2006 SkillBuilders, Inc.

1.18

Triggers...

Code tied to a tableExecutes automatically when DML executes

SQL> @trigger1SQL> create trigger customer_name2 before update or insert on customer3 for each row4 begin5 /* convert character values to upper case */6 :new.lastname := upper( :new.lastname );7 :new.firstname := upper( :new.firstname );8 dbms_output.put_line('trigger fired');9 end;10 /

Trigger created.

SQL> @trigger1SQL> create trigger customer_name2 before update or insert on customer3 for each row4 begin5 /* convert character values to upper case */6 :new.lastname := upper( :new.lastname );7 :new.firstname := upper( :new.firstname );8 dbms_output.put_line('trigger fired');9 end;10 /

Trigger created.

Trigger tied to CUSTOMER table

Use the UPPER function to convert characters inserted into the database

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 often used to:

Enforce complex business and/or integrity rules

Audit modifications to a table

Derive column values

Maintain mirror tables

This trigger, called “customer_name”, will execute whenever an INSERT or UPDATE statement is executed on the DAVE.CUSTOMER table (the schema of the compiler is used for the table, unless qualified or a synonym exists).

Introduction to PL/SQL 1.19

© 2006 SkillBuilders, Inc. V2.1

© 2006 SkillBuilders, Inc.

1.19

...TriggersSQL> insert into customer2 (cust_no, firstname, lastname)3 values4 (3423, 'dave', 'anderson');

trigger fired

1 row created.

SQL> select firstname, lastname2 from customer3 where cust_no = 3423;

FIRSTNAME LASTNAME--------------- --------------------DAVE ANDERSON

SQL> insert into customer2 (cust_no, firstname, lastname)3 values4 (3423, 'dave', 'anderson');

trigger fired

1 row created.

SQL> select firstname, lastname2 from customer3 where cust_no = 3423;

FIRSTNAME LASTNAME--------------- --------------------DAVE ANDERSON

Verification that trigger executed

Characters are converted to upper

case

We see that the PUT_LINE procedure displayed the text “trigger fired” when the INSERT statement executed. This is purely diagnostics. And we see that the trigger did indeed work; the characters have been converted to upper case characters.

Refer to the supplied script TRIGGER1.SQL for a copy of the code shown in this example.

Introduction to PL/SQL 1.20

© 2006 SkillBuilders, Inc. V2.1

© 2006 SkillBuilders, Inc.

1.20

Tools for Development

SQL*Plus is a common toolIncluded with all Oracle database products

Procedure BuilderSophisticated IDE GUI toolAn extra cost productSophisticated debugging features

Breakpointsstepping through codevariable examination, etc.

TOADDeveloper and DBA tool

SQL*Plus is the traditional tool for PL/SQL development as it is included with all versions of the Oracle database. 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 such as TOAD from Quest Software. Free limited feature versions of this product are available at www.toadsoft.com.

Introduction to PL/SQL 1.21

© 2006 SkillBuilders, Inc. V2.1

© 2006 SkillBuilders, Inc.

1.21

Working in SQL*Plus

SET ECHO ON Show compiled source on your screen

SET SERVEROUTPUT ONPUT_LINE calls will be displayed after program returns control to SQL*Plus

Put in LOGIN.SQLSHOW ERRORS

Error messages will refer to a line and column numberOracle Error messages manual is available to help

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. 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 source file because the PL/SQL compiler does not count blank lines and some comment lines.)

I recommend that you put the SET ECHO ON and SERT SERVEROUTPUT commands in your LOGIN.SQL file. This file, when placed in your working directory, will be automatically executed when you start SQL*Plus. Refer to the sample LOGIN.SQL file supplied with this course for and example.

For anonymous blocks, the error will be displayed when you try to execute the code. For procedures, functions, packages and triggers, remember to use the SHOW ERRORS command to display the error messages.

Introduction to PL/SQL 1.22

© 2006 SkillBuilders, Inc. V2.1

© 2006 SkillBuilders, Inc.

1.22

Introduction to PL/SQL Workshops

Hello WorldSetup

Workshop – Hello World

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. Convert the anonymous block into a stored procedure. Compile and test the procedure.3. Convert the stored procedure into a function. Compile and test the function.4. Drop your standalone versions of your procedure and function. Package and test the

packaged versions.

Workshop – Setup

This workshop creates your personal userid and the tables that you will need during this course. All work should be done from your course directory. This is your home directory in Unix, or c:\oraclassin Windows.

1. In Windows, create a shortcut on your desktop for SQL*Plus. There are several ways to do this depending on preference. A simple method is:

1. Click on Start…Search/Find…Files or Folders2. Key in SQLPLUSW.exe, verify the search is starting from the correct drive (probably C:)

and click on the START SEARCH button3. In search results, RIGHT mouse click on SQLPLUSW.exe and select CREATE

SHORTCUT from the menu. . . . continues

Introduction to PL/SQL 1.23

© 2006 SkillBuilders, Inc. V2.1

1. Step 1 continued…

4. Click on the YES button when the dialog indicates it can’t create a shortcut here. Do you want it on the desktop?

5. Close the Search/Find window

6. On your Desktop, click your RIGHT mouse button on the icon for SQL*Plus

7. Select the PROPERTIES menu option

8. Set the START IN directory to your course directory (c:\oraclass for Windows

2. Copy the setup files from the Student Setup Files disk folder to your course directory if they are not already there. In a classroom setting, the instructor will tell you where the setup files are located.

3. Connect to SQL*Plus with userid SYSTEM and password MANAGER. Your instructor will advise you if you should use a different userid or password. This will be the userid you should use whenever a userid with DBA privileges is required. Once connected, you need to connect to a SYSDBA account as follows: connect / as sysdba

Your instructor will advise you if you should use a different userid or password

4. Verify that SQL*Plus is running in your course directory. To do this, use the HOST command in Windows or ! in Unix to shell out to the operating system to view your current directory. If needed, log out of SQL*Plus, reset the current directory and log back in.

5. Create your personal userid using the USERID.SQL script. You will be prompted for the ID tocreate and the class directory. Then, it will create your personal userid with a password the same as the ID. This is the userid you should use for all labs except when a userid with DBA privileges is required.

SQL> @userid

ID> yourname

Path> either c:\oraclass\ or /export/home/nstudent/

If you see an error message about “nothing to drop”, ignore it. If you see an error message about the path, start over from @userid and try again. Caution: You must include the trailing slash on the path directory name.

6. Connect to your personal userid using the SQL*Plus CONNECT command.

SQL> connect yourname/yourname

7. Verify that you are on your personal userid by viewing the pseudo-column USER.

SQL> show user

continues…

Introduction to PL/SQL 1.24

© 2006 SkillBuilders, Inc. V2.1

8. Drop all existing tables on your personal userid, and recreate them using the DDL.SQL script provided. Ignore all error messages with the words “does not exist”.

SQL> @ddl

9. Populate all your tables with data using the INSERT script provided. Again, ignore all error messages with the words “does not exist”.

SQL> @insert

10. Verify that your tables exist.select table_name from user_tables;

11. Verify that your tables have data. SELECT * from customer;

12. Use the SQL*Plus buffer editor to execute the following SQL command:SELECT custno, lastname, firstname

FROM customer;

Purposely misspell CUST_NO as CUSTNO as shown above. Use the SQL*Plus buffer editor commands to correct the statement and then run it.

Use the L to see the command in the edit bufferUse the C to change the CUSTNO to CUST_NOUse the / to execute the corrected SELECT command

13. Repeat the previous exercise using a separate window with notepad or vi (or your favorite ASCII editor). Save the command in a file called test.sql in your class directory, and run it from SQL*Plus.

Open the editor of your choice as you normally would in Windows or UNIXType in the SELECT commandSave the file as “TEST2.SQL” in your c:\oraclass or UNIX home directory. (Note use the quotes to prevent the file from having the .txt extension)Type @test2 in SQL*Plus to execute the TEST2.SQL file