40
Chapter 21: Report writin g 1 Chapter Twenty One Producing Readable Output Objectives: Writing reports Page set up Page layout Queries with input

Chapter Twenty One Producing Readable Output

Embed Size (px)

DESCRIPTION

Chapter Twenty One Producing Readable Output. Objectives: Writing reports Page set up Page layout Queries with input. Producing Readable Output. Summary of Formatting Output Commands. COLUMN TTITLE BTITLE BREAK COMPUTE. COLUMN:. COLumnname | expression CLE ar FOR matformat - PowerPoint PPT Presentation

Citation preview

Page 1: Chapter Twenty One  Producing Readable Output

Chapter 21: Report writing 1

Chapter Twenty One Producing Readable Output

Objectives: Writing reports Page set up Page layout Queries with input

Page 2: Chapter Twenty One  Producing Readable Output

Producing Readable Output

Your Name January 7,2001

List of courses

Department Course No.

Course Name

Credit Date Started

Max enrollment

COSC 100 Intro 3 2-10-92 30

110 Intro II 3 2-11-98 15

200 Programming

3 14-2-93 20

220 Application 3 10-10-01

20

Total 12

MATH 100 Algebra 4 1-9-91 45

Grand Total

Report1.sql

110

Page 1

Page 3: Chapter Twenty One  Producing Readable Output

Chapter 21: Report writing 3

Summary of FormattingOutput Commands

COLUMN TTITLE BTITLE BREAK COMPUTE

Page 4: Chapter Twenty One  Producing Readable Output

Chapter 21: Report writing 4

COLUMN:COLumn name | expression

CLEar FORmat formatHEAding TextJUStify LE | CE | RNEWLineNEW_VALUE

continue

Page 5: Chapter Twenty One  Producing Readable Output

Chapter 21: Report writing 5

COLUMN:(continued)

NULl textNOPRInt PRInt

WRApped TRUncated

Page 6: Chapter Twenty One  Producing Readable Output

Chapter 21: Report writing 6

Formatting Option:COLUMN Name FORMAT A10COLUMN id FORMAT 9999COLUMN cr FORMAT 9COLUMN Gpa FORMAT 9.99

FORMAT:$9999909999909,999.99DATEA10L999

Page 7: Chapter Twenty One  Producing Readable Output

Chapter 21: Report writing 7

Formatting Option:COLUMN Name FORMAT A10COLUMN id FORMAT 9999COLUMN cr FORMAT 9COLUMN GPA FORMAT 9.99SELECT Name, id, cr, GPAFROM studentWHERE Major=‘COSC’ORDER BY GPA;

Page 8: Chapter Twenty One  Producing Readable Output

Chapter 21: Report writing 8

Heading Option:COLUMN Name HEADING ‘Student’’s

Name’COLUMN id HEADING ‘Student’’s ID’COLUMN cr*grade HEADING ‘Score’

COLUMN name HEADING ‘Student’’s Name’ JUSTIFY LE

COLumn Name HEAding ‘Name’ FORmat A10COLumn Major FORmat A7 NULL ‘NO Major’COL address HEADING ‘Addr’ FORMAT A7 TRUNC

Page 9: Chapter Twenty One  Producing Readable Output

Chapter 21: Report writing 9

Column Example:

COLUMN id HEADING ‘Stud ID’ FORMAT 9999TRUNCATED

Page 10: Chapter Twenty One  Producing Readable Output

Chapter 21: Report writing 10

Clear Setting

COLumn Major

CLEar COLumn

Page 11: Chapter Twenty One  Producing Readable Output

Chapter 21: Report writing 11

Break onBREAK ON id SKIP 2BREAK ON id DUPLICATE SKIP 2BREAK ON id NODUPLICATEBREAK ON Major ON ID

BREAK ON REPORT PAGE ON dept PAGE

Page 12: Chapter Twenty One  Producing Readable Output

Chapter 21: Report writing 12

Break onFormat:

BREAK ON column BREAK ON row BREAK ON page BREAK ON report

SKIP nSKIP page

CLEAR BREAK

Page 13: Chapter Twenty One  Producing Readable Output

Chapter 21: Report writing 13

ComputeCOMPUTE SUM OF gpa ON idCOMPUTE SUM LABEL ‘Total’ OF gpa ON idCOMPUTE AVG OF a, b, c, d ON e COMPUTE SUM AVG COUNT OF gpa ON id

AVGCOUNTMAXMINSTDVAR

Page 14: Chapter Twenty One  Producing Readable Output

Chapter 21: Report writing 14

Check the Setting:

COLUMNBREAKSHOWDEFINE

Page 15: Chapter Twenty One  Producing Readable Output

Chapter 21: Report writing 15

Page SetupLine size:

SET LINESIZE 75

Page size:SET PAGESIZE 60

Blank lines at the top:SET NEWPAGE 3

Writing into a file:SPOOL filename.lisSPOOL Gstudent.lisSPOOL OFF

Page 16: Chapter Twenty One  Producing Readable Output

Chapter 21: Report writing 16

Page SetupDisplay:

SET TERMOUT ONSPOOL filename.lis

SET TERMOUT OFF

Heading separator:SET HEADSEP charSET HEADSEP !

Underlining:SET UNDERLINE OFF

Page 17: Chapter Twenty One  Producing Readable Output

Chapter 21: Report writing 17

Page SetupTitle:

TTITLE text | variableTTITLE ‘Information ‘BTITLE ‘Confidential‘

TTITLE Left ‘Page: ’ SQL.PNO – Right ‘Date: ‘ SYSDATE Skip 1 – Center ‘ My Report ‘ Skip 3;

SQL.LNOSQL.PNOSQL.RELEASESQL.SQLCODESQL.USER

Page 18: Chapter Twenty One  Producing Readable Output

Chapter 21: Report writing 18

Page Setup

Host:HOST myeditfilename.SQL

HOST list 2

Start:START filename.SQL

Page 19: Chapter Twenty One  Producing Readable Output

Chapter 21: Report writing 19

Page SetupSummary:COLUMNCOLUMN colname

TTITLEBTITLE

BREAK

COMPUTE

DEFINE

SHOW HEADSEPSHOW LINESIZESHOW PAGESIZESHOW NEWPAGE

Page 20: Chapter Twenty One  Producing Readable Output

Chapter 21: Report writing 20

Set Commands

COLSEP [ |Text]FEEDBACK [OFF| ON| n]HEADING [OFF| ON]LINESIZE [n]PAGESIZE [n]PAUSE [OFF| ON| Text]TERMOUT[OFF| ON]

Page 21: Chapter Twenty One  Producing Readable Output

Chapter 21: Report writing 21

Report Writing Example:Your Name January 7,2001

List of courses

Department Course No.

Course Name

Credit Date Started

Max enrollment

COSC 100 Intro 3 2-10-92 30

110 Intro II 3 2-11-98 15

200 Programming

3 14-2-93 20

220 Application 3 10-10-01

20

Total 12

MATH 100 Algebra 4 1-9-91 45

Grand Total

Report1.sql

110

Page 1

Page 22: Chapter Twenty One  Producing Readable Output

Chapter 21: Report writing 22

Report Writing Example:CLEAR BREAKCLEAR COMPUTECOLUMN dept HEADING ‘Department’ FORMAT A12COLUMN c_num HEADING ‘Course|No.’ FORMAT 999COLUMN title HEADING ‘Course|Name’ FORMAT A13COLUMN cr HEADING ‘Credit’ FORMAT 99COLUMN D_start HEADING ‘Date|Started’ FORMAT

DATECOLumn m_enrol HEAding ‘Max|Enrollment’ FORMAT

999

Page 23: Chapter Twenty One  Producing Readable Output

Chapter 21: Report writing 23

Report Writing Example:COL SYSDATE NEW_VALUE xdate NOPRINT

FORMAT A1 TRUNCBREAK ON dept SKIP 2COMPUTE SUM OF cr ON dept--COMPUTE SUM OF cr ON dept REPORTTTITLE LEFT ‘Your Name:’SKIP 2 -

RIGHT xdate SKIP 1 -CENTER ‘List of Cources’ SKIP 1

Page 24: Chapter Twenty One  Producing Readable Output

Chapter 21: Report writing 24

Report Writing Example:

BTITLE LEFT ‘Report1.sql’ -RIGHT ‘Page No: ’ SQL.PNO

SELECT dept, c_num, title, cr, d_start.m_enroll

FROM departmentWHERE dept IN (‘COSC’, ‘MATH’)ORDER BY dept;

Page 25: Chapter Twenty One  Producing Readable Output

Chapter 21: Report writing 25

Login FileLogin.sql

prompt login.sql loadedset feedback offset sqlprompt ‘ ‘set sqlnumber offset numwidth 5set pagesize 24set linesize 79

Page 26: Chapter Twenty One  Producing Readable Output

Chapter 21: Report writing 26

Substitution Variables Substitution variables to

temporarily store values- &- & &- DEFINE and ACCEPT

Pass variable values between SQL statements

Dynamically alter headers and footers

Page 27: Chapter Twenty One  Producing Readable Output

Chapter 21: Report writing 27

USING & VARIABLE:

SELECT name, id, addressFROM student WHERE id=&student_id;

Enter value for student_id:

Page 28: Chapter Twenty One  Producing Readable Output

Chapter 21: Report writing 28

& Substitution Variables User provides the input to the query

SELECT Name, GPA, IDFROM StudentWHERE UPPER(Major) = &Major_Input;

Enter Value for Major_Input: ‘COSC’

Page 29: Chapter Twenty One  Producing Readable Output

Chapter 21: Report writing 29

& Substitution VariablesSELECT Name, ID, &Column_nameFROM StudentWHERE &conditionORDER BY ℴ

Enter Value for Column_name:Enter Value for Condition:Enter Value for Order:

Page 30: Chapter Twenty One  Producing Readable Output

Chapter 21: Report writing 30

&& Substitution Variables

Use a value of a variable more than one time

SELECT Name, ID, &&Column_nameFROM StudentWHERE

&Column_name=&NewName;

Enter value for Column_name:

Page 31: Chapter Twenty One  Producing Readable Output

Chapter 21: Report writing 31

SET VERIFY ON:SQL> SET VERIFY ONSQL> SELECT name, id, address

2 FROM student 3 WHERE id=&student_id;

Enter value for student_id: 1111Old 3: where id=&student_id;New 3: where id= 1111

Page 32: Chapter Twenty One  Producing Readable Output

Chapter 21: Report writing 32

Character and Date Values

With Substitution Variables:

SELECT name, id, b_dateFROM student WHERE b_date =

'&new_birthdate';

Enter value for new_birthdate:

Page 33: Chapter Twenty One  Producing Readable Output

Chapter 21: Report writing 33

Specifying Column Names, Expressions, and Text at Run Time

WHERE condition ORDER BY clause COLUMN expression TABLE name Entire SELECT statement

Page 34: Chapter Twenty One  Producing Readable Output

Chapter 21: Report writing 34

ExampleSELECT name, id, rank, &column_nameFROM faculty WHERE &condition ORDER BY &order_column;

Enter value for column_name:Enter value for condition:Enter value for order_column:

Page 35: Chapter Twenty One  Producing Readable Output

Chapter 21: Report writing 35

Defining User Variables You can pre-define variables using:

DEFINE: Create a user variable of CHAR data type

ACCEPT: Read user input and store it in a variable

Page 36: Chapter Twenty One  Producing Readable Output

Chapter 21: Report writing 36

Defining User Variables If you need to predefine a variable that

includes spaces, you need to enclose the value within single quotation marks, when using the DEFINE command.

DEFINE variable=CAR DEFINE variable DEFINE ACCEPT

Page 37: Chapter Twenty One  Producing Readable Output

Chapter 21: Report writing 37

ACCEPT Command: Create a customized prompt when

accepting user input

Explicitly defines a NUMBER or DATE datatype variable

Hides user input for security

Page 38: Chapter Twenty One  Producing Readable Output

Chapter 21: Report writing 38

ACCEPT Command:ACCEPT variable [ datatype] [FORMAT

format][prompt text ] [HIDE]

ACCEPT field PROMPT 'Input your Major:‘SELECT * FROM student WHERE major=UPPER ('&field')

Input your major:

Page 39: Chapter Twenty One  Producing Readable Output

Chapter 21: Report writing 39

DEFINE and UNDEFINE Use the UNDEFINE command to

clear Exit SQL*plus

To define variables for every session, modify your login.sql file

Page 40: Chapter Twenty One  Producing Readable Output

Chapter 21: Report writing 40

Create a VariableDEFINE dept_name=COSCDEFINE dept_name

SELECT *FROM facultyWHERE dept=UPPER

('&dept_name');

UNDEFINE dept_name