8 Producing Readable Output with SQL*Plus. 8-2 Objectives At the end of this lesson, you should be able to: Produce queries that require an input variable.

Slides:



Advertisements
Similar presentations
Copyright  Oracle Corporation, All rights reserved. 2 Single-Row Functions.
Advertisements

7 Copyright © Oracle Corporation, All rights reserved. Producing Readable Output with i SQL*Plus.
12-1 Copyright  Oracle Corporation, All rights reserved. What Is a View? EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
Restricting and sorting data 16 May May May Created By Pantharee Sawasdimongkol.
Subqueries 11. Objectives After completing this lesson, you should be able to do the following: Describe the types of problems that subqueries can solve.
1Eyad Alshareef Enhanced Guide to Oracle 10g Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data.
Copyright  Oracle Corporation, All rights reserved. 2 Restricting and Sorting Data.
Copyright  Oracle Corporation, All rights reserved. 6 Subqueries.
Copyright  Oracle Corporation, All rights reserved. 9 Manipulating Data: INSERT, UPDATE, DELETE.
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
Writing Basic SQL statement 2 July July July Create By Pantharee Sawasdimongkol.
SQL Query Extras MIS 433. Rerunning the last Query n Type the forward slash “/” to rerun the last query that was entered.
Chapter 1 Writing Basic SQL Statements Important Legal Notice:  Materials on this lecture are from a book titled “Oracle Education” by Kochhar, Gravina,
Logical Operators Operator AND OR NOT Meaning Returns TRUE if both component conditions are TRUE Returns TRUE if either component condition is TRUE Returns.
Ceng 356-Lab2. Objectives After completing this lesson, you should be able to do the following: Limit the rows that are retrieved by a query Sort the.
o At the end of this lesson, you will be able to:  Describe the life-cycle development phases  Discuss the theoretical and physical aspects of a relational.
Copyright  Oracle Corporation, All rights reserved. I Introduction.
Copyright  Oracle Corporation, All rights reserved. 1 Writing Basic SQL Statements.
Dr. Philip Cannata 1 Programming Languages Prolog Part 3 SQL & Prolog.
Displaying Data from Multiple Tables (Join) Displaying Data from Multiple Tables (Join) Displaying Data from Multiple Tables (Join Displaying Data from.
After completing this lesson, you should be able to do the following: List the capabilities of MySQL SELECT statements Execute a basic SELECT statement.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Introduction to Relational Databases &
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Cursors These slides are licensed under.
Subqueries.
2 Writing Basic SELECT Statements. 1-2 Copyright  Oracle Corporation, All rights reserved. Capabilities of SQL SELECT Statements Selection Projection.
Copyright  Oracle Corporation, All rights reserved. Writing Basic SQL Statements.
Copyright س Oracle Corporation, All rights reserved. I Introduction.
RELATSIOONILISED ANDMEBAASID(alg) SQLi VÕIMALUSED.
7 Multiple-Column Subqueries. 7-2 Objectives At the end of this lesson, you should be able to: Write a multiple-column subquery Describe and explain the.
SQL- DQL (Oracle Version). 2 SELECT Statement Syntax SELECT [DISTINCT] column_list FROM table_list [WHERE conditional expression] [GROUP BY column_list]
Session 2: SQL (A): Parts 1 and 2 Original materials supplied by the Oracle Academic Initiative (OAI). Edited for classroom use by Professor Laku Chidambaram.
Oracle Command Spool Spool C:\temp\Lab9.lst Select Hotel_no, room_no, type, price From Room Order by Hotel_no; Spool Off.
1 Writing Basic SQL Statements. 1-2 Objectives At the end of this lesson, you should be able to: List the capabilities of SQL SELECT statements Execute.
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
I-1 Copyright س Oracle Corporation, All rights reserved. Data Retrieval.
Manipulating Data. Objectives After completing this lesson, you should be able to do the following: Describe each DML statement Insert rows into a table.
9 Manipulating Data. 9-2 Objectives At the end of this lesson, you should be able to: Describe each DML statement Insert rows into a table Update rows.
Copyright  Oracle Corporation, All rights reserved. 12 Creating Views.
Copyright  Oracle Corporation, All rights reserved. 8 Producing Readable Output with SQL*Plus.
Chapter 14 Formatting Readable Output. Chapter Objectives  Add a column heading with a line break to a report  Format the appearance of numeric data.
An Introduction To SQL Part 2 (Special thanks to Geoff Leese)
Use of Data Security Commands Security: Information is vital to success, but when damaged or in the wrong hands, it can threaten success. OREACLE provides.
Chapter Twelve Report Writing Objectives: -Writing reports -Page set up -Page layout.
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
Copyright س Oracle Corporation, All rights reserved. I Introduction.
2 Copyright © 2009, Oracle. All rights reserved. Restricting and Sorting Data.
2-1 Limiting Rows Using a Selection “…retrieve all employees in department 10” EMP EMPNO ENAME JOB... DEPTNO 7839KINGPRESIDENT BLAKEMANAGER CLARKMANAGER.
6 Subqueries. 6-2 Objectives At the end of this lesson, you should be able to: Describe the types of problems that subqueries can solve Define subqueries.
Copyright س Oracle Corporation, All rights reserved. 12 Creating Views.
1-1 Copyright  Oracle Corporation, All rights reserved. Logging In to SQL*Plus From Windows environment:From Windows environment: From command line:From.
Copyright  Oracle Corporation, All rights reserved. 2 Restricting and Sorting Data.
Chapter 21: Report writing1 Chapter Twenty One Producing Readable Output Objectives: Writing reports Page set up Page layout Queries with input.
Writing Basic SQL Statements. Objectives After completing this lesson, you should be able to do the following: –List the capabilities of SQL SELECT statements.
A Guide to SQL, Sixth Edition 1 Chapter 7 Reports.
Defining a Column Alias
Copyright  Oracle Corporation, All rights reserved. Introduction.
Copyright س Oracle Corporation, All rights reserved. 1 Writing Basic SQL Statements.
Communicating with a RDBMS Using SQL Database SQL> SELECT loc 2 FROM dept; SQL> SELECT loc 2 FROM dept; SQL statement is entered Statement is sent to database.
RELATSIOONILISED ANDMEBAASID (algus , ORACLE materjalid)
SQL and SQL*Plus Interaction
Chapter Twenty Producing Readable Output
Subqueries.
Manipulating Data Schedule: Timing Topic 40 minutes Lecture
What Is a View? EMPNO ENAME JOB EMP Table EMPVU10 View
(SQL) Manipulating Data
Restricting and Sorting Data
Writing Basic SQL Statements
Restricting and Sorting Data
Producing Readable Output with iSQL*Plus
Presentation transcript:

8 Producing Readable Output with SQL*Plus

8-2 Objectives At the end of this lesson, you should be able to: Produce queries that require an input variable Customize the SQL*Plus environment Produce more readable output Create and execute script files Save customizations At the end of this lesson, you should be able to: Produce queries that require an input variable Customize the SQL*Plus environment Produce more readable output Create and execute script files Save customizations

8-3 Interactive Reports I want to input query values at runtime....sal = ? … … deptno = ? ….. ename = ?... User

8-4 Substitution Variables Use SQL*Plus substitution variables to temporarily store values – Single ampersand (&) – Double ampersand (&&) – DEFINE and ACCEPT commands Pass variable values between SQL statements Dynamically alter headers and footers Use SQL*Plus substitution variables to temporarily store values – Single ampersand (&) – Double ampersand (&&) – DEFINE and ACCEPT commands Pass variable values between SQL statements Dynamically alter headers and footers

8-5 Using the & Substitution Variable Use a variable prefixed with an ampersand (&) to prompt the user for a value. SQL> SELECTempno, ename, sal, deptno 2 FROMemp 3 WHEREempno = &employee_num; 7369 Enter value for employee_num: 7369 EMPNO ENAME SAL DEPTNO SMITH

8-6 Using the SET VERIFY Command Toggling the display of the text of a command before and after SQL*Plus replaces substitution variables with values. SQL> SET VERIFY ON SQL> SELECTempno, ename, sal, deptno 2 FROMemp 3 WHEREempno = &employee_num; Enter value for employee_num: 7369 old 3: WHERE empno = &employee_num new 3: WHERE empno =

8-7 Character and Date Values with Substitution Variables Use single quotation marks for date and character values. SQL> SELECT ename, deptno, sal*12 2 FROM emp 3 WHERE job='&job_title'; ANALYST Enter value for job_title: ANALYST ENAME DEPTNO SAL* SCOTT FORD

8-8 Specifying Column Names, Expressions, and Text at Runtime Use substitution variables to supplement A WHERE condition An ORDER BY clause A column expression A table name An entire SELECT statement Use substitution variables to supplement A WHERE condition An ORDER BY clause A column expression A table name An entire SELECT statement

8-9 Specifying Column Names, Expressions, and Text at Runtime SQL> SELECTempno, ename, job, &column_name 2 FROMemp 3 WHERE&condition 4 ORDER BY&order_column; sal Enter value for column_name: sal sal>=3000 Enter value for condition: sal>=3000 ename Enter value for order_column: ename EMPNO ENAME JOB SAL FORD ANALYST KING PRESIDENT SCOTT ANALYST 3000

8-10 Using the && Substitution Variable Use the double-ampersand (&&) if you want to reuse the variable value without prompting the user each time. SQL> SELECTempno, ename, job, &&column_name 2 FROMemp 3 ORDER BY&column_name; deptno Enter value for column_name: deptno EMPNO ENAME JOB DEPTNO KING PRESIDENT CLARK MANAGER MILLER CLERK rows selected.

8-11 Defining User Variables You can predefine variables using one of two SQL*Plus commands: – DEFINE–Create a CHAR datatype user variable – ACCEPT–Read user input and store it in a variable If you need to use a single space when using the DEFINE command, you must enclose the space within single quotation marks. You can predefine variables using one of two SQL*Plus commands: – DEFINE–Create a CHAR datatype user variable – ACCEPT–Read user input and store it in a variable If you need to use a single space when using the DEFINE command, you must enclose the space within single quotation marks.

8-12 The ACCEPT Command Creates a customized prompt when accepting user input Explicitly defines a NUMBER or DATE datatype variable Hides user input for security reasons Creates a customized prompt when accepting user input Explicitly defines a NUMBER or DATE datatype variable Hides user input for security reasons ACCEPT variable [datatype] [FORMAT format] [PROMPT text] {HIDE} ACCEPT variable [datatype] [FORMAT format] [PROMPT text] {HIDE}

8-13 Using the ACCEPT Command ACCEPT dept PROMPT 'Provide the department name: ' SELECT * FROM dept WHERE dname = UPPER('&dept') / Sales Provide the department name: Sales DEPTNO DNAME LOC SALES CHICAGO

8-14 DEFINE and UNDEFINE Commands A variable remains defined until you either: – Use the UNDEFINE command to clear it – Exit SQL*Plus You can verify your changes with the DEFINE command. To define variables for every session, modify your login.sql file so that the variables are created at startup. A variable remains defined until you either: – Use the UNDEFINE command to clear it – Exit SQL*Plus You can verify your changes with the DEFINE command. To define variables for every session, modify your login.sql file so that the variables are created at startup.

8-15 Using the DEFINE Command Create a variable to hold the department name. DEFINE DEPTNAME = "sales" (CHAR) Use the variable as you would any other variable. SQL> DEFINE deptname = sales SQL> DEFINE deptname SQL> SELECT * 2 FROM dept 3 WHERE dname = UPPER('&deptname');

8-16 Customizing the SQL*Plus Environment SETUse SET commands to control current session. SHOWVerify what you have set by using the SHOW command. SETUse SET commands to control current session. SHOWVerify what you have set by using the SHOW command. SQL> SET ECHO ON SQL> SHOW ECHO echo ON SQL> SHOW ECHO echo ON SET system_variable value

8-17 SET Command Variables ARRAYSIZE {20 | n}ARRAYSIZE {20 | n} COLSEP {_ | text}COLSEP {_ | text} FEEDBACK {6 | n |OFF | ON}FEEDBACK {6 | n |OFF | ON} HEADING {OFF | ON}HEADING {OFF | ON} LINESIZE {80 | n}LINESIZE {80 | n} LONG {80 | n}LONG {80 | n} PAGESIZE {24 | n}PAGESIZE {24 | n} PAUSE {OFF | ON | text}PAUSE {OFF | ON | text} TERMOUT {OFF | ON}TERMOUT {OFF | ON} ARRAYSIZE {20 | n}ARRAYSIZE {20 | n} COLSEP {_ | text}COLSEP {_ | text} FEEDBACK {6 | n |OFF | ON}FEEDBACK {6 | n |OFF | ON} HEADING {OFF | ON}HEADING {OFF | ON} LINESIZE {80 | n}LINESIZE {80 | n} LONG {80 | n}LONG {80 | n} PAGESIZE {24 | n}PAGESIZE {24 | n} PAUSE {OFF | ON | text}PAUSE {OFF | ON | text} TERMOUT {OFF | ON}TERMOUT {OFF | ON}

8-18 Saving Customizations in the login.sql File The login.sql file contains standard SET and other SQL*Plus commands that are implemented at login. You can modify login.sql to contain additional SET commands. The login.sql file contains standard SET and other SQL*Plus commands that are implemented at login. You can modify login.sql to contain additional SET commands.

8-19 SQL*Plus Format Commands COLUMN [column option] COLUMN [column option] TTITLE [text | OFF | ON] TTITLE [text | OFF | ON] BTITLE [text | OFF | ON] BTITLE [text | OFF | ON] BREAK [ON report_element] BREAK [ON report_element]

8-20 The COLUMN Command Controls display of a column CLE[AR]–Clears any column formats FOR[MAT] format–Changes the display of the column using a format model HEA[DING] text–Sets the column heading JUS[TIFY] {align}–Aligns the column heading to be left, center, or right Controls display of a column CLE[AR]–Clears any column formats FOR[MAT] format–Changes the display of the column using a format model HEA[DING] text–Sets the column heading JUS[TIFY] {align}–Aligns the column heading to be left, center, or right COL[UMN] [{column|alias} [option]]

8-21 Using the COLUMN Command Create column headings. Display the current setting for the ENAME column. Clear settings for the ENAME column. COLUMN ename HEADING 'Employee|Name' FORMAT A15 COLUMN sal JUSTIFY LEFT FORMAT $99, COLUMN mgr FORMAT NULL 'No manager' COLUMN ename HEADING 'Employee|Name' FORMAT A15 COLUMN sal JUSTIFY LEFT FORMAT $99, COLUMN mgr FORMAT NULL 'No manager' COLUMN ename COLUMN ename CLEAR

8-22 COLUMN Format Models Result N/A $1234 L ,234 Example N/A $9999 L ,999 Element An 9 0 $ L., Description Sets a display width of n Single zero-suppression digit Enforces leading zero Floating dollar sign Local currency Position of decimal point Thousand separator

8-23 Using the BREAK Command Suppresses duplicates and sections rows To suppress duplicates Suppresses duplicates and sections rows To suppress duplicates SQL> BREAK ON ename ON job SQL> BREAK ON report SQL> BREAK ON ename SKIP 4 ON job SKIP2 To produce grand totals To section out rows at break values

8-24 Using the TTITLE and BTITLE Commands Display headers and footers TTI[TLE] [text|OFF|ON] SQL> TTITLE 'Salary|Report' Set the report header Set the report footer SQL> BTITLE 'Confidential'

8-25 Creating a Script File to Run a Report 1. Create the SQL SELECT statement. 2.Save the SELECT statement to 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. 1. Create the SQL SELECT statement. 2.Save the SELECT statement to 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.

8-26 Creating a Script File to Run a Report 6.Clear formatting commands after the SELECT statement. 7.Save the script file. 8.Enter “START filename” to run the script. 6.Clear formatting commands after the SELECT statement. 7.Save the script file. 8.Enter “START filename” to run the script.

8-27 Sample Report Fri Oct 24 page 1 Employee Report Job Category Employee Salary CLERK ADAMS $1, CLERK JAMES $ CLERK MILLER $1, CLERK SMITH $ MANAGER BLAKE $2, MANAGER CLARK $2, MANAGER JONES $2, SALESMAN ALLEN $1, SALESMAN MARTIN $1, SALESMAN TURNER $1, SALESMAN WARD $1, Confidential

8-28 Summary Substitution variables can be used in script files with the following: Single ampersand Double ampersand ACCEPT command DEFINE command UNDEFINE command A command line Substitution variables can be used in script files with the following: Single ampersand Double ampersand ACCEPT command DEFINE command UNDEFINE command A command line

8-29 Practice Overview Creating a query to display values using substitution variables Starting a command file containing variables Using the ACCEPT command Creating a query to display values using substitution variables Starting a command file containing variables Using the ACCEPT command

8-30

8-31

8-32