Using SQL*Plus
SQL*Plus is a command-line tool that provides access to the Oracle RDBMS.
SQL*Plus enables you to:
Enter SQL*Plus commands to configure the SQL*Plus environment
Startup and shutdown an Oracle database
Connect to an Oracle database
Enter and execute SQL commands and PL/SQL blocks
Format and print query results
SQL*Plus is available on several platforms. In addition, it has a web-based user
interface, iSQL*Plus.
SQL*Plus is a client terminal software allowing users to interact with Oracle server to manipulate data and data structures. Users type in SQL statements in SQL*Plus that send statements to Oracle server. Oracle server then validates and executes the statements on its databases. The query results are returned to SQL*Plus and displayed to the user. Besides sending SQL statements to the server, SQL*Plus also saves them into a local buffer and allow users to view and change the statements. The following figure illustrates the process.
After you login into SQL*Plus, at the SQL prompt, you can begin typing any SQL command. Upon hitting return (i.e., enter key) the SQL prompt will change to line number prompts. When you are finished typing a command, type / or RUN to execute the SQL command. Also, a semicolon at the end of the SQL command will execute the command immediately after hitting return. In addition to SQL commands, /, and RUN, you can also executes SQL*Plus file commands.
SQL*PLUS Commands Quick Reference
Below table shows, SQL*Plus commands available in the command-line interface. Not all commands or command parameters are shown.
How to … SQL*Plus Command
Log in to SQL*Plus SQLPLUS [ { username[/passward][@connect_identifier] | / } [ AS { SYSDBA | SYSOPER } ] | /NOLOG ]
List help topics available in SQL*Plus HELP [ INDEX | topic ]
Execute host commands HOST [ command ]
Show SQL*Plus system variables or environment settings SHOW { ALL | ERRORS | USER | system_variable | … }
Alter SQL*Plus system variables or environment settings SET system_variable value
Start up a database STARTUP PFILE = filename [ MOUNT [ dbname ] | NOMOUNT | … ]
Connect to a database CONNECT [ [ username [ /password ] [ @connect_identifier ] [ / AS { SYSOPER | SYSDBA } ]
]
List column definitions for a table, view, or synonym, or specifications for afunction or procedure DESCRIBE [ schema. ] object
Edit contents of the SQL buffer or a file EDIT [ filename [ .ext ] ]
Get a file and load its contents into the SQLBuffer GET filename [ .ext ] [ LIST | NOLLIST ]
Save contents of the SQL buffer to a file SAVE filename [ .ext ] [ CREATE | REPLACE | APPEND ]
List contents of the SQL Buffer LIST [ n | nm | n LAST | … ]
Delete contents of the SQL Buffer DEL [ n | nm | n LAST | … ]
Add new lines following current line in the SQL buffer INPUT [ text ]
Append text to end ofcurrent line in the SQL
buffer
APPEND text
Find and replace first occurrence of a text string in current line of the SQL buffer CHANGE sepchar old [ sepchar [ new [ sepchar ] ] ]sepchar can be any non-alphanumeric character such as “/” or “!”
Capture query results in a file and, optionally, send contents of file to default printer SPOOL [ filename [ .ext ] [ CREATE | REPLACE | APPEND | OFF | OUT ]
Run SQL*Plus statements stored in a file @ { url | filename [ .ext ] } [ arg… ]START filename [ .ext ] [ arg… ]
.ext can be omitted if the filename extension is .sql
Execute commands stored in the SQL buffer /
List and execute commands stored in the SQL buffer RUN
Execute a single PL/SQL statement or run a stored procedure EXECUTE statement
Disconnect from a database DISCONNECT
Shut down a database SHUTDOWN [ ABORT | IMMEDIATE | NORMAL | … ]
Log out of SQL*Plus { EXIT | QUIT } [ SUCCESS | FAILURE | WARNING | … ]
[ COMMIT | ROLLBACK ]
SQL*Plus file command allow you to execute commands (or programs) stored in an external file, input or output data from/to a file, and save SQL commands typed during current session.
Some SQL*Plus file commands are:
SAVE filename. This allows you to save buffer contents into a file.
START filename. This allows you to execute a batch of SQL statements stored in a file.
SPOOL filename. This allows you save SQL statements together with their outputs to a file.
GET filename. This retrieve a file and places it into the buffer.
@ filename. This allows you to execute a PL/SQL procedure(s) stored in a file.
Recall that the previously executed commands (in current SQL*Plus session) are stored in the local buffer. One way to change an SQL statement in the buffer is by using the line editor. The following are a list of line edit commands.
LIST or L–Lists the contents of the buffer
LIST n or L n–Lists the contents of line number n in the buffer and makes the line current
LIST * or L *–Lists the current line
LIST m n–Lists the range from m to n line
Append text or A text–Adds to the end of the current line (e.g., “A ,” adds a comma to the end of line
INPUT or I–Adds one or more lines after the current line so you can begin adding the text.
CHANGE /text–Deletes text from the current line
CHANGE /oldtext/newtext–Replaces oldtext with newtext in the current line
DEL — Deletes the current line
Besides line editor, you can also use the vi editor if you are a fan of Unix editor!.
To invoke the vi editor, type Edit at the SQL Prompt. Multiple SQL commands can be typed in vi editor. End each SQL command (except the last one) with a semicolon. After exiting notepad, type Start to run all of the commands.
Run SQL statements in a batch
To run SQL commands in a batch, you can put all your SQL commands into a text file and execute these commands in this file in SQL*PLUS.
Use your favorite editor to type in your SQL queries into a text file.
For Example,
$ more table.sql
DROP TABLE employee
/
commit
/
CREATE TABLE employee (
empno INTEGER NOT NULL,
name VARCHAR2(50) NOT NULL,
sal REAL NOT NULL,
primary key (empno));
/
INSERT INTO employee VALUES (1, ‘Jack’, 6000);
INSERT INTO employee VALUES (2, ‘Tom’, 6000);
INSERT INTO employee VALUES (3, ‘John’, 6000);
INSERT INTO employee VALUES (4, ‘Jane’, 6000);
/
UPDATE employee SET sal=500 WHERE name=’Jack’
/
CREATE INDEX test_index on employee(sal)
/
$
Connect into SQL*Plus, and run the batch of commands. For example, assume that you name the SQL file as table.sql.
SQL> START table.sql;
Output results
You can record your SQL command outputs to a file for output or editing purpose.
SQL> SPOOL <your file name>
For example,
SQL> SPOOL myoutput.out
All SQL commands and their outputs after this command are written into the file myoutput.out that by default is stored in the current working directory where you invoked SQL*Plus.
To end recording, use the following command:
SQL> SPOOL OFF
DUAL and select the current time
DUAL is the dummy table, mostly used to view the results from functions and calculations. The built-in function SYSDATE returns a DATE value containing the current date and time on your system. (Note Oracle is a client-server architecture and SQL*Plus is the client. SYSDATE gives you the time of the Unix system which you telnet in. It may NOT be the time of Oracle server unless you telnet into the machine running Oracle server.)
For example,
SQL> SELECT TO_CHAR(SYSDATE , 'Dy DD-Mon-YYYY HH24:MI:SS') as "Current Date/Time" FROM DUAL;;
Result:
Current Time
------------------------------------------------------------------------
Mon 15-July-2002 10:01:29
TO_CHAR is a function to format a value.
DUAL is built-in relation in Oracle which serves as a dummy relation to put in the FROM clause when nothing else is appropriate. For example, try “SELECT 2+2 FROM DUAL;”
To format a number attribute to a dollar format, use the column <attribute> format <format>:
SQL> COLUMN salary FORMAT $999,999
To indicate the displayed width of a character string attribute, use the column <attribute> format <A’format>. For example, set the width of the name attribute to 8 characters.
SQL> COLUMN name FORMAT A8
If a name is longer than 8 characters, the remaining is displayed at the second line (or several lines)
The set command can be used to change the default number of lines per page (14) and the number of characters per line (80).
For example, to set the number of lines per page to 60, use the following command:
SQL> SET PAGESIZE 60
All formatting remain active until they are cleared or reset or after you exit from SQL*Plus.
SQL> CLEAR COLUMN
If you forget a specific SQL command you could enter
SQL> HELP <the SQL command>;
You could also find out all commands by entering:
SQL> HELP menu;
Sometimes when you get something fuzzy, you can try the following
SQL> SET SERVEROUTPUT ON
SQL> SET ARRAYSIZE 1
SQL*Plus is a command-line tool that provides access to the Oracle RDBMS.
SQL*Plus enables you to:
Enter SQL*Plus commands to configure the SQL*Plus environment
Startup and shutdown an Oracle database
Connect to an Oracle database
Enter and execute SQL commands and PL/SQL blocks
Format and print query results
SQL*Plus is available on several platforms. In addition, it has a web-based user
interface, iSQL*Plus.
SQL*Plus is a client terminal software allowing users to interact with Oracle server to manipulate data and data structures. Users type in SQL statements in SQL*Plus that send statements to Oracle server. Oracle server then validates and executes the statements on its databases. The query results are returned to SQL*Plus and displayed to the user. Besides sending SQL statements to the server, SQL*Plus also saves them into a local buffer and allow users to view and change the statements. The following figure illustrates the process.
After you login into SQL*Plus, at the SQL prompt, you can begin typing any SQL command. Upon hitting return (i.e., enter key) the SQL prompt will change to line number prompts. When you are finished typing a command, type / or RUN to execute the SQL command. Also, a semicolon at the end of the SQL command will execute the command immediately after hitting return. In addition to SQL commands, /, and RUN, you can also executes SQL*Plus file commands.
SQL*PLUS Commands Quick Reference
Below table shows, SQL*Plus commands available in the command-line interface. Not all commands or command parameters are shown.
How to … SQL*Plus Command
Log in to SQL*Plus SQLPLUS [ { username[/passward][@connect_identifier] | / } [ AS { SYSDBA | SYSOPER } ] | /NOLOG ]
List help topics available in SQL*Plus HELP [ INDEX | topic ]
Execute host commands HOST [ command ]
Show SQL*Plus system variables or environment settings SHOW { ALL | ERRORS | USER | system_variable | … }
Alter SQL*Plus system variables or environment settings SET system_variable value
Start up a database STARTUP PFILE = filename [ MOUNT [ dbname ] | NOMOUNT | … ]
Connect to a database CONNECT [ [ username [ /password ] [ @connect_identifier ] [ / AS { SYSOPER | SYSDBA } ]
]
List column definitions for a table, view, or synonym, or specifications for afunction or procedure DESCRIBE [ schema. ] object
Edit contents of the SQL buffer or a file EDIT [ filename [ .ext ] ]
Get a file and load its contents into the SQLBuffer GET filename [ .ext ] [ LIST | NOLLIST ]
Save contents of the SQL buffer to a file SAVE filename [ .ext ] [ CREATE | REPLACE | APPEND ]
List contents of the SQL Buffer LIST [ n | nm | n LAST | … ]
Delete contents of the SQL Buffer DEL [ n | nm | n LAST | … ]
Add new lines following current line in the SQL buffer INPUT [ text ]
Append text to end ofcurrent line in the SQL
buffer
APPEND text
Find and replace first occurrence of a text string in current line of the SQL buffer CHANGE sepchar old [ sepchar [ new [ sepchar ] ] ]sepchar can be any non-alphanumeric character such as “/” or “!”
Capture query results in a file and, optionally, send contents of file to default printer SPOOL [ filename [ .ext ] [ CREATE | REPLACE | APPEND | OFF | OUT ]
Run SQL*Plus statements stored in a file @ { url | filename [ .ext ] } [ arg… ]START filename [ .ext ] [ arg… ]
.ext can be omitted if the filename extension is .sql
Execute commands stored in the SQL buffer /
List and execute commands stored in the SQL buffer RUN
Execute a single PL/SQL statement or run a stored procedure EXECUTE statement
Disconnect from a database DISCONNECT
Shut down a database SHUTDOWN [ ABORT | IMMEDIATE | NORMAL | … ]
Log out of SQL*Plus { EXIT | QUIT } [ SUCCESS | FAILURE | WARNING | … ]
[ COMMIT | ROLLBACK ]
SQL*Plus file command allow you to execute commands (or programs) stored in an external file, input or output data from/to a file, and save SQL commands typed during current session.
Some SQL*Plus file commands are:
SAVE filename. This allows you to save buffer contents into a file.
START filename. This allows you to execute a batch of SQL statements stored in a file.
SPOOL filename. This allows you save SQL statements together with their outputs to a file.
GET filename. This retrieve a file and places it into the buffer.
@ filename. This allows you to execute a PL/SQL procedure(s) stored in a file.
Recall that the previously executed commands (in current SQL*Plus session) are stored in the local buffer. One way to change an SQL statement in the buffer is by using the line editor. The following are a list of line edit commands.
LIST or L–Lists the contents of the buffer
LIST n or L n–Lists the contents of line number n in the buffer and makes the line current
LIST * or L *–Lists the current line
LIST m n–Lists the range from m to n line
Append text or A text–Adds to the end of the current line (e.g., “A ,” adds a comma to the end of line
INPUT or I–Adds one or more lines after the current line so you can begin adding the text.
CHANGE /text–Deletes text from the current line
CHANGE /oldtext/newtext–Replaces oldtext with newtext in the current line
DEL — Deletes the current line
Besides line editor, you can also use the vi editor if you are a fan of Unix editor!.
To invoke the vi editor, type Edit at the SQL Prompt. Multiple SQL commands can be typed in vi editor. End each SQL command (except the last one) with a semicolon. After exiting notepad, type Start to run all of the commands.
Run SQL statements in a batch
To run SQL commands in a batch, you can put all your SQL commands into a text file and execute these commands in this file in SQL*PLUS.
Use your favorite editor to type in your SQL queries into a text file.
For Example,
$ more table.sql
DROP TABLE employee
/
commit
/
CREATE TABLE employee (
empno INTEGER NOT NULL,
name VARCHAR2(50) NOT NULL,
sal REAL NOT NULL,
primary key (empno));
/
INSERT INTO employee VALUES (1, ‘Jack’, 6000);
INSERT INTO employee VALUES (2, ‘Tom’, 6000);
INSERT INTO employee VALUES (3, ‘John’, 6000);
INSERT INTO employee VALUES (4, ‘Jane’, 6000);
/
UPDATE employee SET sal=500 WHERE name=’Jack’
/
CREATE INDEX test_index on employee(sal)
/
$
Connect into SQL*Plus, and run the batch of commands. For example, assume that you name the SQL file as table.sql.
SQL> START table.sql;
Output results
You can record your SQL command outputs to a file for output or editing purpose.
SQL> SPOOL <your file name>
For example,
SQL> SPOOL myoutput.out
All SQL commands and their outputs after this command are written into the file myoutput.out that by default is stored in the current working directory where you invoked SQL*Plus.
To end recording, use the following command:
SQL> SPOOL OFF
DUAL and select the current time
DUAL is the dummy table, mostly used to view the results from functions and calculations. The built-in function SYSDATE returns a DATE value containing the current date and time on your system. (Note Oracle is a client-server architecture and SQL*Plus is the client. SYSDATE gives you the time of the Unix system which you telnet in. It may NOT be the time of Oracle server unless you telnet into the machine running Oracle server.)
For example,
SQL> SELECT TO_CHAR(SYSDATE , 'Dy DD-Mon-YYYY HH24:MI:SS') as "Current Date/Time" FROM DUAL;;
Result:
Current Time
------------------------------------------------------------------------
Mon 15-July-2002 10:01:29
TO_CHAR is a function to format a value.
DUAL is built-in relation in Oracle which serves as a dummy relation to put in the FROM clause when nothing else is appropriate. For example, try “SELECT 2+2 FROM DUAL;”
To format a number attribute to a dollar format, use the column <attribute> format <format>:
SQL> COLUMN salary FORMAT $999,999
To indicate the displayed width of a character string attribute, use the column <attribute> format <A’format>. For example, set the width of the name attribute to 8 characters.
SQL> COLUMN name FORMAT A8
If a name is longer than 8 characters, the remaining is displayed at the second line (or several lines)
The set command can be used to change the default number of lines per page (14) and the number of characters per line (80).
For example, to set the number of lines per page to 60, use the following command:
SQL> SET PAGESIZE 60
All formatting remain active until they are cleared or reset or after you exit from SQL*Plus.
SQL> CLEAR COLUMN
If you forget a specific SQL command you could enter
SQL> HELP <the SQL command>;
You could also find out all commands by entering:
SQL> HELP menu;
Sometimes when you get something fuzzy, you can try the following
SQL> SET SERVEROUTPUT ON
SQL> SET ARRAYSIZE 1
No comments:
Post a Comment