22
Developing and Debugging PL/SQL using Oracle SQL Developer This tutorial contains the following sections: Purpose Time to Complete Overview Prerequisites Creating a Database Connection Creating and Compiling a PL/SQL Procedure Running a PL/SQL Procedure Debugging a PL/SQL Procedure Summary Purpose This tutorial shows you how to create, run, and debug a PL/SQL procedure using Oracle SQL Developer. Time to Complete Approximately 30 minutes. Overview Oracle SQL Developer is a free graphical tool that enhances productivity and simplifies database development tasks. With Oracle SQL Developer, you can browse database objects, run SQL statements and SQL scripts, and edit and debug PL/SQL statements. You can also run any number of provided reports, as well as create and save your own. This tutorial focuses on creating, compiling, running and debugging PL/SQL. Prerequisites Before starting this tutorial, you should: 1 . Install Oracle SQL Developer 3.0 from OTN. Follow the readme instructions here. 2 . Install Oracle Database 11g with the Sample schema. 3. Unlock the HR user. Login to SQL Developer as the SYS user and execute the following commands: alter user hr identified by hr account unlock; grant debug connect session to hr; grant debug any procedure to hr Note: This tutorial is developed using Oracle SQL Developer 3.0. However, you can also use Oracle SQL Developer 2.1.1. 4 . Download and unzip the files.zip to a local folder on your file system. In this tutorial, we use the C:\sqldev3.0 folder. Creating a Database Connection The first step to managing database objects using Oracle SQL Developer 3.0 is to create a database connection. Perform the following steps: 1 . If you installed the SQL Developer icon on your desktop, click the icon to start your SQL Developer and move to Step 4. If you do not have the icon located on your desktop, perform the following steps to create a shortcut to Developing and Debugging PL/SQL using SQL Developer http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/sqldev/r3... 1 of 22 7/9/2014 11:45 AM

SQL Using SQL Developer

Embed Size (px)

DESCRIPTION

sql developer debuging

Citation preview

Page 1: SQL Using SQL Developer

Developing and Debugging PL/SQL using Oracle SQL Developer

This tutorial contains the following sections:

Purpose

Time to Complete

Overview

Prerequisites

Creating a Database Connection

Creating and Compiling a PL/SQL Procedure

Running a PL/SQL Procedure

Debugging a PL/SQL Procedure

Summary

Purpose

This tutorial shows you how to create, run, and debug a PL/SQL procedure using Oracle SQL Developer.

Time to Complete

Approximately 30 minutes.

Overview

Oracle SQL Developer is a free graphical tool that enhances productivity and simplifies database development tasks. WithOracle SQL Developer, you can browse database objects, run SQL statements and SQL scripts, and edit and debug PL/SQLstatements. You can also run any number of provided reports, as well as create and save your own. This tutorial focuses oncreating, compiling, running and debugging PL/SQL.

Prerequisites

Before starting this tutorial, you should:

1 . Install Oracle SQL Developer 3.0 from OTN. Follow the readme instructions here.

2 . Install Oracle Database 11g with the Sample schema.

3. Unlock the HR user. Login to SQL Developer as the SYS user and execute the following commands:alter user hr identified by hr account unlock;grant debug connect session to hr;grant debug any procedure to hr

Note: This tutorial is developed using Oracle SQL Developer 3.0. However, you can also use Oracle SQL Developer2.1.1.

4 . Download and unzip the files.zip to a local folder on your file system. In this tutorial, we use the C:\sqldev3.0

folder.

Creating a Database Connection

The first step to managing database objects using Oracle SQL Developer 3.0 is to create a database connection. Perform thefollowing steps:

1 . If you installed the SQL Developer icon on your desktop, click the icon to start your SQL Developer and move toStep 4. If you do not have the icon located on your desktop, perform the following steps to create a shortcut to

Developing and Debugging PL/SQL using SQL Developer http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/sqldev/r3...

1 of 22 7/9/2014 11:45 AM

Page 2: SQL Using SQL Developer

launch SQL Developer 3.0 directly from your desktop.

Open the directory where the SQL Developer 3.0 is located, right-click sqldeveloper.exe (on Windows) orsqldeveloper.sh (on Linux) and select Send to > Desktop (create shortcut).

2 . On the desktop, you will find an icon named Shortcut to sqldeveloper.exe. Double-click the icon to open SQLDeveloper 3.0.

Note: To rename, select the icon and then press F2 and enter a new name.

3 . Your Oracle SQL Developer opens.

Developing and Debugging PL/SQL using SQL Developer http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/sqldev/r3...

2 of 22 7/9/2014 11:45 AM

Page 3: SQL Using SQL Developer

4 . In the Connections tab, right-click Connections and select New Connection.

5 . The New / Select Database Connection dialog opens. Enter the connection details as follows and click Test.

Connection Name: HR_ORCLUsername: hrPassword: <your_system_password>Hostname: localhostPort: 1521SID: <your_SID>

Developing and Debugging PL/SQL using SQL Developer http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/sqldev/r3...

3 of 22 7/9/2014 11:45 AM

Page 4: SQL Using SQL Developer

6 . Check for the status of the connection on the left-bottom side (above the Help button). It should read Success. ClickConnect. Then click Save.

7 . The connection was saved and you see the newly created connection in the Connections list.

When a connection is created, a SQL Worksheet is opened automatically. The SQL Worksheet allows you toexecute SQL against the connection you have opened. Expand the HR_ORCL connection.

Developing and Debugging PL/SQL using SQL Developer http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/sqldev/r3...

4 of 22 7/9/2014 11:45 AM

Page 5: SQL Using SQL Developer

Creating and Compiling a PL/SQL Procedure

In this topic you create, edit, and compile a PL/SQL procedure. Perform the following steps:

1 . Right-click Procedures node in the Connections navigator, to invoke the context menu, and select New Procedure.

2 . Enter EMP_LIST as the procedure name and then click to add a parameter. Double-click Parameters name to

allow you to change the value to pMaxRows. Change the type from VARCHAR2 to NUMBER. Click OK.

Developing and Debugging PL/SQL using SQL Developer http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/sqldev/r3...

5 of 22 7/9/2014 11:45 AM

Page 6: SQL Using SQL Developer

3 . The procedure is created.

Note: At this point, only the shell of the procedure is completed. In the next step, you add more PL/SQL code intothe procedure.

4 . Replace the following PL/SQL:

BEGIN NULL;END EMP_LIST;

with the following code:

(Note: This code is in the file emp_cursor.sql in the directory where you unzipped the files from the Prerequisites

Developing and Debugging PL/SQL using SQL Developer http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/sqldev/r3...

6 of 22 7/9/2014 11:45 AM

Page 7: SQL Using SQL Developer

section.)

CURSOR emp_cursor IS SELECT l.state_province, l.country_id, d.department_name, e.last_name, j.job_title, e.salary, e.commission_pct FROM locations l, departments d, employees e, jobs j WHERE l.location_id = d.location_id AND d.department_id = e.department_id AND e.job_id = j.job_id; emp_record emp_cursor%ROWTYPE; TYPE emp_tab_type IS TABLE OF emp_cursor%ROWTYPE INDEX BY BINARY_INTEGER; emp_tab emp_tab_type;i NUMBER := 1;BEGIN OPEN emp_cursor; FETCH emp_cursor INTO emp_record; emp_tab(i) := emp_record; WHILE ((emp_cursor%FOUND) AND (i <= pMaxRows) LOOP i := i + 1; FETCH emp_cursor INTO emp_record; emp_tab(i) := emp_record; END LOOP; CLOSE emp_cursor; FOR j IN REVERSE 1..i LOOP DBMS_OUTPUT.PUT_LINE(emp_tab(j).last_name); END LOOP;END;

Notice how the reserved words are formatted by Oracle SQL Developer. To format the code further, right-click withinthe code editor to invoke the sub menu and select Format.

Compile the PL/SQL subprogram by clicking Save in the toolbar.

Developing and Debugging PL/SQL using SQL Developer http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/sqldev/r3...

7 of 22 7/9/2014 11:45 AM

Page 8: SQL Using SQL Developer

5 . Compile errors, if any, are displayed.

6 . By expanding Procedures on the navigator, EMP_LIST can be viewed.

Developing and Debugging PL/SQL using SQL Developer http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/sqldev/r3...

8 of 22 7/9/2014 11:45 AM

Page 9: SQL Using SQL Developer

Note that when an invalid PL/SQL subprogram is detected by Oracle SQL Developer, the status is indicated with ared X over the icon for the subprogram in the Connections Navigator.

7 . Compilation errors are shown in the log window. You can navigate to the line reported in the error by simply double-clicking on the error. Oracle SQL Developer also displays errors and hints in the right hand gutter. If you hover overeach of the red bars in the gutter, the error message displays.

In this case, the error messages indicate that there is a formatting error in the LOOP statement. After reviewing thecode further, you see an extra parenthesis in the WHILE statement. Delete the extra parenthesis.

Developing and Debugging PL/SQL using SQL Developer http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/sqldev/r3...

9 of 22 7/9/2014 11:45 AM

Page 10: SQL Using SQL Developer

8 . Click Compile.

Developing and Debugging PL/SQL using SQL Developer http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/sqldev/r3...

10 of 22 7/9/2014 11:45 AM

Page 11: SQL Using SQL Developer

9 . The procedure compiled successfully. You are now ready to run the procedure.

Developing and Debugging PL/SQL using SQL Developer http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/sqldev/r3...

11 of 22 7/9/2014 11:45 AM

Page 12: SQL Using SQL Developer

Note: If you still see a red X over the icon for your procedure under the Procedures node, click the refresh icon. Agreen overlay indicates the procedure has been compiled for debugging. No additional overlay means the procedurehas been compiled without additional debugging directives. These are controlled by preference settings and thecompile droplist option. The default in SQL Developer is "Compile for Debug".

Running a PL/SQL Procedure

Once you have created and compiled a PL/SQL procedure, you can run it using Oracle SQL Developer. Perform the followingsteps:

1 . Right-click on EMP_LIST in the Connections navigator and select Run.

Developing and Debugging PL/SQL using SQL Developer http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/sqldev/r3...

12 of 22 7/9/2014 11:45 AM

Page 13: SQL Using SQL Developer

2 . This invokes the Run PL/SQL dialog. The Run PL/SQL dialog allows you to select the target procedure or function torun (useful for packages) and displays a list of parameters for the selected target. In the PL/SQL block text area, youwill see the generated code that Oracle SQL Developer uses to call the selected program. You can use this area topopulate parameters to be passed to the program unit and to handle complex return types.

In your EMP_LIST procedure, you have a parameter named PMAXROWS. In the Run PL/SQL dialog, you caninitialize that parameter to any number value.

Developing and Debugging PL/SQL using SQL Developer http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/sqldev/r3...

13 of 22 7/9/2014 11:45 AM

Page 14: SQL Using SQL Developer

Change PMAXROWS := NULL; to PMAXROWS := 5; Then click OK.

Developing and Debugging PL/SQL using SQL Developer http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/sqldev/r3...

14 of 22 7/9/2014 11:45 AM

Page 15: SQL Using SQL Developer

3 . The results are displayed in the Running - Log window.

Debugging a PL/SQL Procedure

Oracle SQL Developer also supports PL/SQL debugging with Oracle databases. In this topic, you debug a PL/SQL Procedure,step through the code and modify a value at runtime. Perform the following steps:

1 . To assist with debugging, line numbers can be added to the Code window. Right-click on the margin and selectToggle Line Numbers.

Developing and Debugging PL/SQL using SQL Developer http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/sqldev/r3...

15 of 22 7/9/2014 11:45 AM

Page 16: SQL Using SQL Developer

2 . To debug a procedure, you need to Compile for Debug first. This step adds in the compiler directives required fordebugging. Once you have completed the debug, you should compile the procedure again and remove the extradirectives.

Developing and Debugging PL/SQL using SQL Developer http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/sqldev/r3...

16 of 22 7/9/2014 11:45 AM

Page 17: SQL Using SQL Developer

3 . A breakpoint is a location in the code that you identify as a stopping point. When code is run in debug mode,execution will stop at the breakpoint.

Set a breakpoint in the EMP_LIST procedure by clicking in the margin at the line with the OPEN emp_cursor;statement. The line number is replaced with a red dot. This is a breakpoint symbol.

Then click the Debug icon.

Developing and Debugging PL/SQL using SQL Developer http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/sqldev/r3...

17 of 22 7/9/2014 11:45 AM

Page 18: SQL Using SQL Developer

4 . The Debug PL/SQL dialog should still show the value PMAXROWS = 5; Click OK.

Developing and Debugging PL/SQL using SQL Developer http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/sqldev/r3...

18 of 22 7/9/2014 11:45 AM

Page 19: SQL Using SQL Developer

5 . Click Log tab, if it is not already displayed.

Developing and Debugging PL/SQL using SQL Developer http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/sqldev/r3...

19 of 22 7/9/2014 11:45 AM

Page 20: SQL Using SQL Developer

6 . The debugger should halt at the line where you placed the breakpoint. You can now control the flow of execution,modify values of variables and perform other debugging functions. Click Step Into .

Developing and Debugging PL/SQL using SQL Developer http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/sqldev/r3...

20 of 22 7/9/2014 11:45 AM

Page 21: SQL Using SQL Developer

Note: You have been granted the DEBUG CONNECT SESSION and DEBUG ANY PROCEDURE user privileges inthe Prerequisites section to avoid the following error message when debugging.

Developing and Debugging PL/SQL using SQL Developer http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/sqldev/r3...

21 of 22 7/9/2014 11:45 AM

Page 22: SQL Using SQL Developer

Summary

In this tutorial, you have learned how to:

Create a Database ConnectionBrowse the DatabaseCreate and Compile a PL/SQL ProcedureRun a PL/SQL ProcedureDebug a PL/SQL Procedure

About Oracle |Oracle and Sun | | Careers | Contact Us | Site Maps | Legal

Notices | Terms of Use | Your Privacy Rights

Developing and Debugging PL/SQL using SQL Developer http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/sqldev/r3...

22 of 22 7/9/2014 11:45 AM