Prerequisites for This Example Create a New PLSQL Library

Building a Report that Includes PLSQL 40-5

6. On the Data page, enter the following SELECT statement in the Data Source

definition field: SELECT FIRST_NAME, LAST_NAME, JOB_ID, SALARY,COMMISSION_PCT FROM EMPLOYEES ORDER BY LAST_NAME

7. Click Next.

8. On the Groups page, click Next.

9. Click Finish to display your first query in the Data Model view. It should look

something like this: Figure 40–3 Data Model view of the PLSQL report 10. Save your report.

40.3.2 Create a formula column that calculates bonuses

The steps in this section will show you how to create a formula column that will calculate the salary bonus for each employee using the PLSQL function. Note: You can enter this query in any of the following ways: ■ Copy and paste the code from the provided text file called plsql_code.txt into the Data Source definition field. ■ Click Query Builder to build the query without entering any code manually. ■ Type the code in the Data Source definition field. Note: If you are not already connected to a database, you will be prompted to connect to the database when you click Query Builder or Next. Ensure that you connect to a database that has the appropriate schema for this example. Section 40.1, Prerequisites for This Example describes the sample schema requirements for this example. 40-6 Oracle Reports Users Guide to Building Reports To create the BONUS formula column:

1. In the Data Model view, click group G_FIRST_NAME, then click the bottom

resize handle and drag it down to make room in the group for more columns. Here is an example of what it should look like now: Figure 40–4 Data Model with expanded G_FIRST_NAME

2. Click the Formula Column tool in the tool palette, then click in the G_FIRST_

NAME group to create a formula column. Figure 40–5 Data Model with unnamed formula column

3. Double-click the new formula column object CF_1 to display the Property

Inspector, and set the following properties: ■ Under General Information, set the Name property to BONUS. ■ Under PlaceholderFormula, click the PLSQL Formula property field to display the PLSQL Editor.

4. In the PLSQL Editor, use the template to enter the following PLSQL code:

function BONUSFormula return Number is begin return BONUS_PAY:JOB_ID, :SALARY, :COMMISSION_PCT; end; Building a Report that Includes PLSQL 40-7

5. Click Compile.

6. When there are no compilation errors, click Close to display the data model for

your report in the Data Model view. It should look something like this: Figure 40–6 Data Model with BONUS formula column 7. Save your report.

40.3.3 Create a report-level function that calculates total compensation

The steps in this section will show you how to write a function that returns the total compensation for each sales representative the values of columns SALARY plus COMM plus BONUS, as well as other employees SALARY plus BONUS.

1. In the Object Navigator, click the Program Units node, then choose Edit Create.

2. In the New Program Unit dialog box, in the Name field, type FINAL_CALC.

3. Select Function, then click OK.

4. In the PLSQL Editor, use the template to enter the following PLSQL code:

FUNCTION FINAL_CALC RETURN NUMBER IS BEGIN IF :JOB_ID = SA_REP THEN RETURN :BONUS + :SALARY + :COMMISSION_PCT :SALARY; ELSE RETURN :BONUS + :SALARY; END IF; END; Note: You can enter this code by copying and pasting it from the provided text file called plsql_code.txt. This code is for the Bonus Formula Column. Note: If there are compilation errors, compare your code closely against the code we have provided.