Creating your own reports

You can create any of the reports described earlier under User Defined Reports, either by copying them and then making modifications, or by starting from scratch. Defining your own reports also means that you can query instance data in your application or project. If you run certain queries regularly, they are well-suited to be stored as reports.

User-defined report styles include basic tabular reports, charting, and master-detail reports. In this section, we will use the HR sample schema to demonstrate how to build your own reports. You can use the techniques described to build more complex reports.

Getting started

The first time you use the SQL Developer Reports navigator, the User Defined Reports folder will be available and initially empty. You can populate the folder using a number of approaches, for example:

  • Importing existing reports that were previously created
  • Copying a report
  • Creating a report from scratch

Regardless of the approach used, it is advisable to categorize your reports. You can do this using folders.

Creating folders

Storing reports in folders serves a number of purposes in SQL Developer. Folders help you categorize reports that belong to a particular project or have a similar theme. This in turn helps you quickly find a category of reports, whether instance data or data dictionary reports. In addition, having folders of reports means that you can export, and therefore, later import the folders to reuse and share. You can create nested folders to further organize your reports.

To create a folder, select the User Defined Reports node in the Reports navigator and select New Folder from the context menu, as follows:

Creating folders

This invokes a simple dialog as shown in the following screenshot, which you can then populate with the details about the folder. This folder dialog only requires the Name field to be filled. However, if you provide more detail, it is useful to share reports later.

Creating folders

Storing reports

As soon as you create an initial folder or report, a new UserReports.xml file will be created in the C:Documents and Settings<user>Application DataSQL Developer folder.

The other change to be aware of is that reports created prior to SQL Developer 1.5 were created in UserReports.xml and stored in the C:Documents and Settings<user>.sqldeveloper folder. In SQL Developer 1.5, this location was changed to the Application Data folder as described.

The initial code created using the earlier mentioned folder example looks as follows:

<?xml version="1.0" encoding="UTF-8" ?><displays><folder>
  <name><![CDATA[HR Sample Schema]]></name>
  <tooltip><![CDATA[HR Sample Schema reports]]></tooltip>
  <description><![CDATA[All reports relating to the HR Sample
                Schema]]></description>
   </folder>
</displays>

This is the file that your new reports are added to, which you share with other users. We'll discuss sharing reports later.

Tip

As soon as you add reports to a folder or edit reports in the folder, the folder will be rendered in italics. Saving the new updates using the Save All button updates the folder definition and the UserReports.xml file.

Creating general reports

To create a report, select the User Defined Reports node or a subfolder under the node, and invoke the Create Report dialog using New Report from the context menu. The initial dialog is the same for all the reports. At this point, you can enter all of the details about the report, including the SQL, Bind Variables, Child Reports, and so on. Once all the required report building requirements are entered, the Test Report icon will run the test report on the database selected from the connections list we already have (on the right-hand side of the Test Report icon, as shown in the following screenshot):

Creating general reports

In the example that we have just seen, notice the report Style drop-down list. This allows you to control the visual output of the report (for example, a table or chart).

The Test button allows you to test the SQL script for validity, so it's often easier to leave the report's style unchanged. The default Style is Table. Test the SQL and then change the style as required.

Building general tabular reports

This is the most common form of reporting used and, with the exception of a few charts provided, all the shipped reports follow this style. Tabular reports are also used in drill-down and master-detail reports. Regardless of the report you're about to create, you need to start with the initial SQL query. Consider the following query:

SELECT B.DEPARTMENT_ID,
   B.DEPARTMENT_NAME,
   A.FIRST_NAME,
   A.LAST_NAME,
   A.PHONE_NUMBER,
   A.HIRE_DATE,
   A.SALARY,
   C.JOB_TITLE
FROM HR.DEPARTMENTS B,
   HR.EMPLOYEES A,
   HR.JOBS C
WHERE A.EMPLOYEE_ID = B.MANAGER_ID
AND B.DEPARTMENT_ID = A.DEPARTMENT_ID
AND C.JOB_ID        = A.JOB_ID;

When this query is used in a tabular report, the output will appear, as shown in the following screenshot:

Building general tabular reports

Adding bind variables

If you work with large sets of data, restricting the records returned using a WHERE clause makes the result set more manageable. Writing the query to support input values or a bind variable means that you can rerun the query using different values. This kind of a query makes a good report, because you can pass in values to restrict the result set.

It is important to know that while writing a query with bind variables, unless otherwise handled, the data is stored in the database in the uppercase. If you have data in mixed case in the database, you'll need to code for this.

For example, you can add AND C.JOB_TITLE LIKE :JOB_NAME to the previous query. This is fine, except you need to know what you are searching for and that, in this case, is Job Titles stored in mixed case. So, entering "sales" or "manager" would not return any records. In this example, it's safer to enter the code AND UPPER (C.JOB_TITLE) LIKE UPPER (:JOB_NAME). Then, the user does not need to know the composition of the data.

Once you have set up the binds in your report, you can run the report. You will be prompted for the value that you can add, as shown in the following screenshot:

Adding bind variables

The Create Report dialog also provides the opportunity to add more meaningful detail to the binds in your reports.

Below the SQL script region is a set of tabs, one of which is the Binds tab, as shown in the following screenshot. This allows you to add more detail to the bind variables declared, which is useful and especially true if you pass a number of parameters.

Adding bind variables

In the example shown in the following screenshot, we have provided a Default search value, Prompt, and ToolTip. All of these provide added information to other users running the report.

Adding bind variables

Tip

To invoke the Create Report dialog after it has been closed, select Edit Report from the context menu while highlighting the report.

Drilling down through reports

You can create a report that steps from one report to the next. You can also navigate to the definition of an object. Therefore, a report that returns a list of tables has an additional context menu that you can use to navigate to the table in question. The shipped reports also support drill-down reports. Consider the All Constraints report in the Tables | Constraints node. In addition to returning the Constraint details, the report displays a list of tables. Here, the context menu displays an extra menu option to navigate to the table in question:

Drilling down through reports

Creating a drill-down report

To create a drill-down report, create the initial master report, as you would do for any other tabular report. By way of an example, we'll create a master or top-level report using the same query that we used before:

SELECT DEPARTMENT_ID,
       DEPARTMENT_NAME,
       LOCATION_ID
FROM DEPARTMENTS

Create a second report using a query that will provide the results of the detail records (following the drill-down action from the first):

SELECT EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,DEPARTMENT_ID
FROM EMPLOYEES
WHERE department_id =:DEPARTMENT_ID

Give the report a descriptive name (for example, Employee Details); you'll need to access this later. Notice, for this report, we included a bind variable. Therefore, the report either requires a value to be entered using the Bind Variable dialog, or it has a value passed from the top-level or master report.

Now, return to the top-level report and select the Advanced tab.

Select Add Report and enter the Name of the detail report. This is the value that is displayed in the context menu. In the Report field, there is a drop-down list of all the reports available, whether shipped or user-defined. Find and select the detail report you created, as shown in the following screenshot. Clicking on Apply saves the changes and runs the report.

Creating a drill-down report

Running the top-level report is no different from any other tabular report. The context menu now displays an addition menu called Reports. All of the reports that you can navigate to from this report are displayed in the submenu. In the following example, the value that you have selected is passed to the called detail report as a bind parameter:

Creating a drill-down report

While drilling down through the reports, a new button becomes available to allow you to traverse back up through the reports, as shown in the following screenshot:

Creating a drill-down report

Tip

Case-sensitive bind variables

Bind variable values are case-sensitive. For bind variables that access a column name in the database, use the case of the column, as it is stored. The default is uppercase.

Master-detail reports

Master-detail reports are useful to review related data on a single screen. Initially, when you run a master-detail report, only the master records are displayed. Once you select one of the master records, the details for that master are displayed. Consider one of the shipped Data Dictionary reports, Active Sessions, shown in the following screenshot (this can be found under Database Administration | Sessions). The report has a number of sibling details that are displayed in a set of tabs below the master record.

Master-detail reports

Creating master-detail reports

Initially, you create a regular tabular report as the master report. In the following example, we will use the same query as before:

SELECT DEPARTMENT_ID,
       DEPARTMENT_NAME,
       LOCATION_ID
FROM DEPARTMENTS

You can create, test, and even run the report as you would in any tabular report. This will ensure that the master records and reports are correct. In the example shown in the following screenshot, we have run Report Test:

Creating master-detail reports

To add the detail, click on the Add Child button on the main report. This invokes a new panel in the dialog and allows you to add the detail records.

Creating master-detail reports

The example in the following screenshot displays the full set of employees for any record in the master report. In order to have a meaningful set of records for each department, you need to link the master to the detail. For example:

Creating master-detail reports
SELECT EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,DEPARTMENT_ID
FROM   EMPLOYEES
WHERE  department_id = :DEPARTMENT_ID

The two key elements here are first that the department_id bind variable is in uppercase (or the case of the column name in the database) as the report is looking for this "variable" in the database, and the second is that you have the join value in the master report.

Once you have created the report, you can tab through the master records to display each set of related detail records.

Notice that it is the name of the report that is displayed in the master and detail tabs.

Creating master-detail reports

Adding charts

You can create reports and display them as charts instead of as a tabular output. The basic premise of a chart is that you need values for the x- and y-axis and the central value. Therefore, a report query is structured as follows:

SELECT <group>,<series>,<value>
FROM <table(s)>

The query SELECT DEPARTMENT_ID, LAST_NAME, SALARY FROM EMPLOYEES is an easy example to start with.

Once you have entered the query, switch the display style to Chart and select the Chart Details tab.

Adding charts

The Property node under Master Report in the left navigation pane provides you with more control on the style of chart you are going to display. The choices of chart styles are as follows:

  • Pie
  • Bar stacked (vertical or horizontal)
  • Bar cluster (vertical or horizontal)

The number of records returned should drive the choice of the chart style you make. For example, the PIE_MULTI choice for Chart Type produces a pie chart for each record and is therefore better suited for fewer records or a query different from the one displayed earlier.

There are a number of other settings, which you can adjust, that control the appearance of the final chart. In the example we are using, Legend has been switched off as this displays each LAST_NAME and makes the report very busy.

Adding charts

The final output for the chart is shown in the following screenshot:

Adding charts

Notice that as you roll the mouse pointer on the various regions in the chart, the values are displayed. You can now incorporate these charts as details in a master-detail report.

SQL Developer offers a limited choice of graphical reports. The product is not a graphical reporting tool, so while these reports may seem limited, they do provide a visual report of the data being queried. Other graphical chart options can be pie charts and gauge charts.

Importing and exporting

Once you have a selection of reports created, you can export and save or share the reports. To export a report or a folder of reports, select Export from the context menu. You can export the following:

  • The highest level of User Defined Reports
  • A folder and subfolders
  • Individual reports

If you have created folders to categorize reports, then these folders are ideal to export.

In the same way that you can export folders of reports or individual reports, you can import reports and folders of reports from other users. Any report that you import is added to your personal UserReports.xml file and, as such, it can be edited and deleted as you choose.

Sharing reports through user-defined extensions

If you create a selection of reports that you want to share among team members as read-only reports, you can include these in a separate Shared Reports node. These reports are not editable, much like the shipped reports, although they can be copied and added to the User Defined reports section and edited at this point. To share reports, you first need to export a set of reports. Once you have the new XML file or reports, place it on a shared server that is available for all the users who need access. Open the Preferences dialog from the Tools menu, expand the Database node in the tree on the left-hand side, and select User Defined Extensions. Here, you can add the reports by browsing to the location of the report. The location should include the XML file name. Set Type to REPORT.

Once you have completed this step, you need to restart SQL Developer for the new extension type to be included. Once reopened, the new Shared Reports node will be displayed in the top-level list of folders.

Tip

Save shared reports on a web server

You can save the report's XML file on a web server. If the file is stored on a web server, use the full URL for the location of the file in the User Defined Extensions settings.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset