+ All Categories
Home > Business > Les08-Oracle

Les08-Oracle

Date post: 11-Jun-2015
Category:
Upload: suman1248
View: 983 times
Download: 0 times
Share this document with a friend
Popular Tags:
24
Copyright Oracle Corporation, 1998. All rights reserved. 8 8 Producing Readable Output with SQL*Plus
Transcript
Page 1: Les08-Oracle

Copyright Oracle Corporation, 1998. All rights reserved.

88

Producing Readable Output with SQL*Plus

Producing Readable Output with SQL*Plus

Page 2: Les08-Oracle

8-2 Copyright Oracle Corporation, 1998. All rights reserved.

ObjectivesObjectives

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

• Produce queries that require an input variable

• Customize the SQL*Plus environment

• Produce more readable output

• Create and execute script files

• Save customizations

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

• Produce queries that require an input variable

• Customize the SQL*Plus environment

• Produce more readable output

• Create and execute script files

• Save customizations

Page 3: Les08-Oracle

8-3 Copyright Oracle Corporation, 1998. All rights reserved.

SQL Statements Versus SQL*Plus Commands

SQL Statements Versus SQL*Plus Commands

SQLSQLstatementsstatements

SQL SQL

• A languageA language

• ANSI standardANSI standard

• Keyword cannot be Keyword cannot be abbreviatedabbreviated

• Statements manipulate Statements manipulate data and table data and table definitions in the definitions in the databasedatabase

SQL*PlusSQL*Plus

• An environmentAn environment

• Oracle proprietaryOracle proprietary

• Keywords can be Keywords can be abbreviatedabbreviated

• Commands do not Commands do not allow manipulation of allow manipulation of values in the databasevalues in the database

SQLSQLbufferbuffer

SQL*PlusSQL*Pluscommandscommands

SQL*PlusSQL*Plusbufferbuffer

Page 4: Les08-Oracle

8-4 Copyright Oracle Corporation, 1998. All rights reserved.

• Log in to SQL*Plus.

• Describe the table structure.

• Edit your SQL statement.

• Execute SQL from SQL*Plus.

• Save SQL statements to files and append SQL statements to files.

• Execute saved files.

• Load commands from file to bufferto edit.

• Log in to SQL*Plus.

• Describe the table structure.

• Edit your SQL statement.

• Execute SQL from SQL*Plus.

• Save SQL statements to files and append SQL statements to files.

• Execute saved files.

• Load commands from file to bufferto edit.

Overview of SQL*PlusOverview of SQL*Plus

Page 5: Les08-Oracle

8-5 Copyright Oracle Corporation, 1998. All rights reserved.

Logging In to SQL*PlusLogging In to SQL*Plus

• From Windows environment:From Windows environment:

• From command line:From command line: sqlplus [sqlplus [usernameusername[/[/password password [@[@databasedatabase]]]]]]

Page 6: Les08-Oracle

8-6 Copyright Oracle Corporation, 1998. All rights reserved.

Displaying Table StructureDisplaying Table Structure

Use the SQL*Plus DESCRIBE command to Use the SQL*Plus DESCRIBE command to display the structure of a table.display the structure of a table.Use the SQL*Plus DESCRIBE command to Use the SQL*Plus DESCRIBE command to display the structure of a table.display the structure of a table.

DESC[RIBE] tablenameDESC[RIBE] tablename

Page 7: Les08-Oracle

8-7 Copyright Oracle Corporation, 1998. All rights reserved.

Displaying Table StructureDisplaying Table Structure

SQL> DESCRIBE deptSQL> DESCRIBE dept

Name Null? Type----------------- -------- ------------DEPTNO NOT NULL NUMBER(2)DNAME VARCHAR2(14)LOC VARCHAR2(13)

Name Null? Type----------------- -------- ------------DEPTNO NOT NULL NUMBER(2)DNAME VARCHAR2(14)LOC VARCHAR2(13)

Page 8: Les08-Oracle

8-8 Copyright Oracle Corporation, 1998. All rights reserved.

SQL*Plus Editing CommandsSQL*Plus Editing Commands

• A[PPEND] text

• C[HANGE] / old / new

• C[HANGE] / text /

• CL[EAR] BUFF[ER]

• DEL

• DEL n

• DEL m n

• A[PPEND] text

• C[HANGE] / old / new

• C[HANGE] / text /

• CL[EAR] BUFF[ER]

• DEL

• DEL n

• DEL m n

Page 9: Les08-Oracle

8-9 Copyright Oracle Corporation, 1998. All rights reserved.

SQL*Plus Editing CommandsSQL*Plus Editing Commands

• I[NPUT]

• I[NPUT] text

• L[IST]

• L[IST] n

• L[IST] m n

• R[UN]

• n

• n text

• 0 text

• I[NPUT]

• I[NPUT] text

• L[IST]

• L[IST] n

• L[IST] m n

• R[UN]

• n

• n text

• 0 text

Page 10: Les08-Oracle

8-10 Copyright Oracle Corporation, 1998. All rights reserved.

SQL*Plus File CommandsSQL*Plus File Commands

• SAVE filename

• GET filename

• START filename

• @ filename

• EDIT filename

• SPOOL filename

• SAVE filename

• GET filename

• START filename

• @ filename

• EDIT filename

• SPOOL filename

Page 11: Les08-Oracle

8-11 Copyright Oracle Corporation, 1998. All rights reserved.

Interactive ReportsInteractive Reports

I want to input query values at runtime....sal = ? …

… deptno = ? … .. ename = ? ...

UserUser

Page 12: Les08-Oracle

8-12 Copyright Oracle Corporation, 1998. All rights reserved.

Substitution VariablesSubstitution 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.

Page 13: Les08-Oracle

8-13 Copyright Oracle Corporation, 1998. All rights reserved.

Using the & Substitution VariableUsing the & Substitution Variable

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

SQL> SELECT empno, ename, sal, deptno 2 FROM emp 3 WHERE empno = &employee_num;

Enter value for employee_num: 73697369

EMPNO ENAME SAL DEPTNO--------- ---------- --------- --------- 7369 SMITH 800 20

Page 14: Les08-Oracle

8-14 Copyright Oracle Corporation, 1998. All rights reserved.

Using the SET VERIFY CommandUsing the SET VERIFY Command

Toggling the display of the text of a Toggling the display of the text of a command before and after SQL*Plus command before and after SQL*Plus replaces substitution variables with values.replaces substitution variables with values.

Toggling the display of the text of a Toggling the display of the text of a command before and after SQL*Plus command before and after SQL*Plus replaces substitution variables with values.replaces substitution variables with values.

SQL> SET VERIFY ONSQL> SELECT empno, ename, sal, deptno 2 FROM emp 3 WHERE empno = &employee_num;

Enter value for employee_num: 7369old 3: WHERE empno = &employee_numnew 3: WHERE empno = 7369

...

Page 15: Les08-Oracle

8-15 Copyright Oracle Corporation, 1998. All rights reserved.

Character and Date Values with Substitution VariablesCharacter and Date Values with Substitution Variables

Use single quotation marks for date and Use single quotation marks for date and character values.character values.Use single quotation marks for date and Use single quotation marks for date and character values.character values.

SQL> SELECT ename, deptno, sal*12 2 FROM emp 3 WHERE job='&job_title';

Enter value for job_title: ANALYSTANALYST

ENAME DEPTNO SAL*12---------- --------- ---------SCOTT 20 36000FORD 20 36000

Page 16: Les08-Oracle

8-16 Copyright Oracle Corporation, 1998. All rights reserved.

Specifying Column Names, Expressions, and Text at Runtime

Specifying Column Names, Expressions, and Text at Runtime

Use substitution variables to supplement Use substitution variables to supplement the following:the following:

• WHERE condition

• ORDER BY clause

• Column expression

• Table name

• Entire SELECT statement

Use substitution variables to supplement Use substitution variables to supplement the following:the following:

• WHERE condition

• ORDER BY clause

• Column expression

• Table name

• Entire SELECT statement

Page 17: Les08-Oracle

8-17 Copyright Oracle Corporation, 1998. All rights reserved.

Specifying Column Names, Expressions, and Text at Runtime

Specifying Column Names, Expressions, and Text at Runtime

SQL> SELECT empno, ename, job, &column_name 2 FROM emp 3 WHERE &condition 4 ORDER BY &order_column;

Enter value for column_name: salsalEnter value for condition: sal>=3000sal>=3000Enter value for order_column: enameename

EMPNO ENAME JOB SAL--------- ---------- --------- --------- 7902 FORD ANALYST 3000 7839 KING PRESIDENT 5000 7788 SCOTT ANALYST 3000

Page 18: Les08-Oracle

8-18 Copyright Oracle Corporation, 1998. All rights reserved.

Using the && Substitution VariableUsing the && Substitution Variable

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

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

SQL> SELECT empno, ename, job, &&column_name 2 FROM emp 3 ORDER BY &column_name;

Enter value for column_name: deptnodeptno EMPNO ENAME JOB DEPTNO--------- ---------- --------- --------- 7839 KING PRESIDENT 10 7782 CLARK MANAGER 10 7934 MILLER CLERK 10...14 rows selected.

Page 19: Les08-Oracle

8-19 Copyright Oracle Corporation, 1998. All rights reserved.

Defining User VariablesDefining 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 predefine a variable that includes spaces, you must enclose the value within single quotation marks when using the DEFINE command.

• 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 predefine a variable that includes spaces, you must enclose the value within single quotation marks when using the DEFINE command.

Page 20: Les08-Oracle

8-20 Copyright Oracle Corporation, 1998. All rights reserved.

The ACCEPT CommandThe 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]

Page 21: Les08-Oracle

8-21 Copyright Oracle Corporation, 1998. All rights reserved.

Using the ACCEPT CommandUsing the ACCEPT Command

ACCEPT dept PROMPT 'Provide the department name: 'SELECT * FROM deptWHERE dname = UPPER('&dept')/

Provide the department name: SalesSales

DEPTNO DNAME LOC--------- -------------- ------------- 30 SALES CHICAGO

Page 22: Les08-Oracle

8-22 Copyright Oracle Corporation, 1998. All rights reserved.

DEFINE and UNDEFINE CommandsDEFINE 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.

Page 23: Les08-Oracle

8-23 Copyright Oracle Corporation, 1998. All rights reserved.

Using the DEFINE CommandUsing the DEFINE Command

• Create a variable to hold the department name.

• Create a variable to hold the department name.

DEFINE DEPTNAME = "sales" (CHAR) DEFINE DEPTNAME = "sales" (CHAR)

• Use the variable as you would any other variable.

• Use the variable as you would any other variable.

SQL> DEFINE deptname = salesSQL> DEFINE deptname

SQL> SELECT * 2 FROM dept 3 WHERE dname = UPPER('&deptname');

Page 24: Les08-Oracle

8-24 Copyright Oracle Corporation, 1998. All rights reserved.

Customizing the SQL*Plus Environment

Customizing the SQL*Plus Environment

• Use SETSET commands to control current session.

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

• Use SETSET commands to control current session.

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

SQL> SET ECHO ONSQL> SET ECHO ON

SQL> SHOW ECHOecho ONecho ON

SQL> SHOW ECHOecho ONecho ON

SET system_variable value SET system_variable value


Recommended