Top Banner
7 Copyright © Oracle Corporation, 2001. All rights reserved. Producing Readable Output with iSQL*Plus
29
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Les07

7Copyright © Oracle Corporation, 2001. All rights reserved.

Producing Readable Output with iSQL*Plus

Page 2: Les07

7-2 Copyright © Oracle Corporation, 2001. All rights reserved.

Objectives

After completing this lesson, you should be able to do the following:

• Produce queries that require a substitution variable

• Customize the iSQL*Plus environment

• Produce more readable output

• Create and execute script files

Page 3: Les07

7-3 Copyright © Oracle Corporation, 2001. All rights reserved.

Substitution Variables

I want to query different values.... salary = ? …

… department_id = ? … ... last_name = ? ...

User

Page 4: Les07

7-4 Copyright © Oracle Corporation, 2001. All rights reserved.

Substitution Variables

Use iSQL*Plus substitution variables to:

• Temporarily store values

– Single ampersand (&)

– Double ampersand (&&)

– DEFINE command

• Pass variable values between SQL statements

• Dynamically alter headers and footers

Page 5: Les07

7-5 Copyright © Oracle Corporation, 2001. All rights reserved.

Using the & Substitution VariableUsing the & Substitution Variable

Use a variable prefixed with an ampersand (&) to prompt the user for a value.Use a variable prefixed with an ampersand (&) to prompt the user for a value.

SELECT employee_id, last_name, salary, department_idFROM employeesWHERE employee_id = &employee_num ;

Page 6: Les07

7-6 Copyright © Oracle Corporation, 2001. All rights reserved.

Using the & Substitution VariableUsing the & Substitution Variable

1

2

101

Page 7: Les07

7-7 Copyright © Oracle Corporation, 2001. All rights reserved.

SELECT last_name, department_id, salary*12FROM employeesWHERE job_id = '&job_title' ;

Character and Date Values with Substitution Variables

Use single quotation marks for date and character values.

Page 8: Les07

7-8 Copyright © Oracle Corporation, 2001. All rights reserved.

Specifying Column Names, Expressions, and Text

Use substitution variables to supplement the following:

• WHERE conditions

• ORDER BY clauses

• Column expressions

• Table names

• Entire SELECT statements

Page 9: Les07

7-9 Copyright © Oracle Corporation, 2001. All rights reserved.

SELECT employee_id, last_name, job_id, &column_nameFROM employeesWHERE &conditionORDER BY &order_column ;

Specifying Column Names, Expressions, and Text

Page 10: Les07

7-10 Copyright © Oracle Corporation, 2001. All rights reserved.

Defining Substitution Variables

• You can predefine variables using the iSQL*Plus DEFINE command.

DEFINE variable = value creates a user variable with the CHAR data type.

• If you need to predefine a variable that includes spaces, you must enclose the value within single quotation marks when using the DEFINE command.

• A defined variable is available for the session

Page 11: Les07

7-11 Copyright © Oracle Corporation, 2001. All rights reserved.

DEFINE and UNDEFINE Commands

• A variable remains defined until you either:

– Use the UNDEFINE command to clear it

– Exit iSQL*Plus

• You can verify your changes with the DEFINE command.

DEFINE job_title = IT_PROGDEFINE job_titleDEFINE JOB_TITLE = "IT_PROG" (CHAR)

UNDEFINE job_titleDEFINE job_titleSP2-0135: symbol job_title is UNDEFINED

Page 12: Les07

7-12 Copyright © Oracle Corporation, 2001. All rights reserved.

SELECT employee_id, last_name, salary, department_idFROM employeesWHERE employee_id = &employee_num ;

Using the DEFINE Command with & Substitution Variable

• Create the substitution variable using the DEFINE command.

• Use a variable prefixed with an ampersand (&) to substitute the value in the SQL statement.

DEFINE employee_num = 200

Page 13: Les07

7-13 Copyright © Oracle Corporation, 2001. All rights reserved.

SELECT employee_id, last_name, job_id, &&column_nameFROM employeesORDER BY &column_name;

Use the double-ampersand (&&) if you want to reuse the variable value without prompting the user each time.

Using the && Substitution Variable

Page 14: Les07

7-14 Copyright © Oracle Corporation, 2001. All rights reserved.

old 3: WHERE employee_id = &employee_numnew 3: WHERE employee_id = 200

Using the VERIFY Command

Use the VERIFY command to toggle the display of thesubstitution variable, before and after iSQL*Plus replaces substitution variables with values.

SET VERIFY ONSELECT employee_id, last_name, salary, department_idFROM employeesWHERE employee_id = &employee_num;

Page 15: Les07

7-15 Copyright © Oracle Corporation, 2001. All rights reserved.

Customizing the iSQL*Plus Environment

• Use SETSET commands to control current session.

• Verify what you have set by using the SHOWSHOW command.

SET ECHO ONSET ECHO ON

SHOW ECHOecho ONecho ON

SHOW ECHOecho ONecho ON

SET system_variable value SET system_variable value

Page 16: Les07

7-16 Copyright © Oracle Corporation, 2001. All rights reserved.

SET Command Variables

• ARRAYSIZE {20 | n}

• FEEDBACK {6 | n |OFF | ON}

• HEADING {OFF | ON}

• LONG {80 | n}| ON | text}

SET HEADING OFFSET HEADING OFF

SHOW HEADINGHEADING OFFHEADING OFF

SHOW HEADINGHEADING OFFHEADING OFF

Page 17: Les07

7-17 Copyright © Oracle Corporation, 2001. All rights reserved.

iSQL*Plus Format Commands

• COLUMN [column option]• TTITLE [text | OFF | ON]• BTITLE [text | OFF | ON]• BREAK [ON report_element]

Page 18: Les07

7-18 Copyright © Oracle Corporation, 2001. All rights reserved.

The COLUMN Command

Controls display of a column:

• CLE[AR]: Clears any column formats

• HEA[DING] text: Sets the column heading

• FOR[MAT] format: Changes the display of the column using a format model

• NOPRINT | PRINT

• NULL

COL[UMN] [{column|alias} [option]]COL[UMN] [{column|alias} [option]]

Page 19: Les07

7-19 Copyright © Oracle Corporation, 2001. All rights reserved.

Using the COLUMN Command

• Create column headings.

COLUMN last_name HEADING 'Employee|Name' COLUMN salary JUSTIFY LEFT FORMAT $99,990.00COLUMN manager FORMAT 999999999 NULL 'No manager'

COLUMN last_name HEADING 'Employee|Name' COLUMN salary JUSTIFY LEFT FORMAT $99,990.00COLUMN manager FORMAT 999999999 NULL 'No manager'

COLUMN last_nameCOLUMN last_name

COLUMN last_name CLEARCOLUMN last_name CLEAR

• Display the current setting for the LAST_NAME column.

• Clear settings for the LAST_NAME column.

Page 20: Les07

7-20 Copyright © Oracle Corporation, 2001. All rights reserved.

COLUMN Format Models

Result

1234

001234

$1234

L1234

1234.00

1,234

Example

999999

099999

$9999

L9999

9999.99

9,999

Element

9

0

$

L

.

,

Description

Single zero-suppression

digit

Enforces leading zero

Floating dollar sign

Local currency

Position of decimal point

Thousand separator

Page 21: Les07

7-21 Copyright © Oracle Corporation, 2001. All rights reserved.

Using the BREAK Command

Use the BREAK command to suppress duplicates.

BREAK ON job_idBREAK ON job_id

Page 22: Les07

7-22 Copyright © Oracle Corporation, 2001. All rights reserved.

Using the TTITLE and BTITLE Commands

• Display headers and footers.

• Set the report header.

• Set the report footer.

TTI[TLE] [text|OFF|ON]TTI[TLE] [text|OFF|ON]

TTITLE 'Salary|Report'TTITLE 'Salary|Report'

BTITLE 'Confidential'BTITLE 'Confidential'

Page 23: Les07

7-23 Copyright © Oracle Corporation, 2001. All rights reserved.

Using the TTITLE and BTITLE Commands

• Display headers and footers.

• Set the report header.

• Set the report footer.

TTI[TLE] [text|OFF|ON]TTI[TLE] [text|OFF|ON]

TTITLE 'Salary|Report'TTITLE 'Salary|Report'

BTITLE 'Confidential'BTITLE 'Confidential'

Page 24: Les07

7-24 Copyright © Oracle Corporation, 2001. All rights reserved.

Creating a Script File to Run a Report

1. Create and test the SQL SELECT statement.

2. Save the SELECT statement into a script file.

3. Load the script file into an editor.

4. Add formatting commands before the SELECT statement.

5. Verify that the termination character follows the SELECT statement.

Page 25: Les07

7-25 Copyright © Oracle Corporation, 2001. All rights reserved.

Creating a Script File to Run a Report

6. Clear formatting commands after the SELECT statement.

7. Save the script file.

8. Load the script file into the iSQL*Plus text window, and click the Execute button.

Page 26: Les07

7-26 Copyright © Oracle Corporation, 2001. All rights reserved.

Sample Report

Page 27: Les07

7-27 Copyright © Oracle Corporation, 2001. All rights reserved.

Sample Report

Page 28: Les07

7-28 Copyright © Oracle Corporation, 2001. All rights reserved.

Summary

In this lesson, you should have learned how to:

• Use iSQL*Plus substitution variables to store values temporarily

• Use SET commands to control the current iSQL*Plus environment

• Use the COLUMN command to control the display of a column

• Use the BREAK command to suppress duplicates and divide rows into sections

• Use the TTITLE and BTITLE commands to display headers and footers

Page 29: Les07

7-29 Copyright © Oracle Corporation, 2001. All rights reserved.

Practice 7 Overview

This practice covers the following topics:

• Creating a query to display values using substitution variables

• Starting a command file containing variables