You are on page 1of 6

12/6/12

A Using SQL Command Line

Oracle Database Express Edition 2 Day Developer Guide 10g Release 2 (10.2) Part Number B25108-01

Home Book Contents Index Master Contact List Index Us


View PDF

Previous Next

A Using SQL Command Line


This section provides an introduction to SQL Command Line (SQL*Plus), an interactive and batch command-line query tool that is installed with Oracle Database Express Edition. This section contains the following topics: Overview of SQL Command Line Using SQL Command Line For information about running SQL language statements, see Chapter 3, "Using SQL". See Also: SQL*Plus User's Guide and Reference for complete information about SQL*Plus Oracle Database SQL Reference for information about using SQL statements Oracle Database Express Edition 2 Day DBA for information about connecting to Oracle Database XE with SQL Command Line

Overview of SQL Command Line


SQL Command Line (SQL*Plus) is a command-line tool for accessing Oracle Database XE. It enables you to enter and run SQL, PL/SQL, and SQL*Plus commands and statements to: Query, insert, and update data Execute PL/SQL procedures Examine table and object definitions Develop and run batch scripts Perform database administration You can use SQL Command Line to generate reports interactively, to generate reports as batch processes, and to write the results to a text file, to a screen, or to an HTML file for browsing on the Internet.

Using SQL Command Line


This section describes SQL Command Line (SQL*Plus), a command-line utility to run SQL and PL/SQL.
docs.oracle.com/cd/B25329_01/doc/appdev.102/b25108/xedev_sqlplus.htm#BJEEGJGJ 1/6

12/6/12

A Using SQL Command Line

This contains the following topics: Starting and Exiting SQL Command Line Displaying Help With SQL Command Line Entering and Executing SQL Statements and Commands SQL Command Line DESCRIBE Command SQL Command Line SET Commands Running Scripts From SQL Command Line Spooling From SQL Command Line Using Variables With SQL Command Line Note: Before starting SQL Command Line, make sure that the necessary environmental variables have been set up properly. See Oracle Database Express Edition 2 Day DBA for information about setting environmental variables for SQL Command Line.

Starting and Exiting SQL Command Line


To start SQL Command Line from the operating-system command prompt, enter the following: slls qpu When prompted, enter the username and password of the user account (schema) that you want to access in the local database. For example, enter H for the username and m _ r p s w r for the password when prompted. R yh_asod You can also include the username and password when you start SQL Command Line. For example: sllsh/yh_asod qpu rm_rpswr If you want to connect to a database running on a remote system, you need to include a connect string when starting SQL Command Line. For example: sllsh/yh_asodhs_optrnm qpu rm_rpswr@otcmue_ae After you have started SQL Command Line, the S L prompt displays as follows: Q> SL Q> At the S L prompt, you can enter SQL statements. Q> When you want to exit SQL Command Line, enter E I at the SQL prompt, as follows: XT SL EI Q> XT

Displaying Help With SQL Command Line


To display a list of Help topics for SQL Command Line, enter H L I D Xat the SQL prompt as follows: EP NE
docs.oracle.com/cd/B25329_01/doc/appdev.102/b25108/xedev_sqlplus.htm#BJEEGJGJ 2/6

12/6/12

A Using SQL Command Line

SL HL IDX Q> EP NE From the list of SQL Command Line Help topics, you can display Help about an individual topic by entering H L EP with a topic name. For example, the following displays Help about the SQL Command Line C L M command, which OUN enables you to format column output: SL HL CLM Q> EP OUN

Entering and Executing SQL Statements and Commands


To enter and execute SQL statements or commands, enter the statement or command at the SQL prompt. At the end of a SQL statement, put a semi-colon (;) and then press the Enter key to execute the statement. For example: SL SLC *FO epoes Q> EET RM mlye; If the statement does not fit on one line, enter the first line and press the Enter key. Continue entering lines, and terminate the last line with a semi-colon (;). For example: SL SLC epoe_d frtnm,ls_ae Q> EET mlyei, is_ae atnm 2 FO epoes RM mlye 3 WEEepoe_d> 15ADepoe_d< 10 HR mlyei = 0 N mlyei = 1; The output from the previous S L C statement is similar to: EET EPOE_DFRTNM MLYEI IS_AE LS_AE ATNM ---------------- ---------------- ---------- ----------15Dvd 0 ai Asi utn 16Vli 0 al Ptbla aaal 17Daa 0 in Lrnz oet 18Nny 0 ac Geneg rebr 19Dne 0 ail Fve ait 10Jh 1 on Ce hn 6rw slce. os eetd Note that a terminating semi-colon (;) is optional with SQL Command Line commands, such as D S R B o r S T ECIE E, but required with SQL statements.

SQL Command Line DESCRIBE Command


SQL Command Line provides the D S R B command to display a description of a database object. For example, ECIE the following displays the structure of the e p o e stable. This description is useful when constructing SQL mlye statements that manipulate the e p o e stable. mlye SL DSRB epoes Q> ECIE mlye Nm ae Nl?Tp ul ye -------------------- ---- ------------------------- ---- -----EPOE_D MLYEI NTNL NME() O UL UBR6 FRTNM IS_AE VRHR(0 ACA22) LS_AE ATNM NTNL VRHR(5 O UL ACA22) EAL MI NTNL VRHR(5 O UL ACA22) POENME HN_UBR VRHR(0 ACA22) HR_AE IEDT N O UL AE docs.oracle.com/cd/B25329_01/doc/appdev.102/b25108/xedev_sqlplus.htm#BJEEGJGJT N L D T

3/6

12/6/12

A Using SQL Command Line

HR_AE IEDT JBI O_D SLR AAY CMISO_C OMSINPT MNGRI AAE_D DPRMN_D EATETI

NTNL DT O UL AE NTNL VRHR(0 O UL ACA21) NME(,) UBR82 NME(,) UBR22 NME() UBR6 NME() UBR4

SQL Command Line SET Commands


The SQL Command Line S Tcommands can be used to specify various SQL Command Line settings, such as the E format of the output from SQL S L C statements. For example, the following S Tcommands specify the number EET E of lines for each page and the number of characters for each line in the output: SL STPGSZ 20 Q> E AEIE 0 SL STLNSZ 10 Q> E IEIE 4 To enable output from PL/SQL blocks with D M _ U P T P T L N use the following: B S O T U . U _ I E, SL STSREOTU O Q> E EVRUPT N To view all the settings, enter the following at the SQL prompt: SL SO AL Q> HW L For information about the SQL Command Line S R E O T U setting to display output from a PL/SQL program, EVRUPT see "Inputting and Outputting Data with PL/SQL". See Also: SQL*Plus User's Guide and Reference for information about setting up the SQL Command Line environment with a login file

Running Scripts From SQL Command Line


You can use a text editor to create SQL Command Line script files that contain SQL*Plus, SQL, and PL/SQL statements. For consistency, use the . q extension for the script file name. sl A SQL script file is executed with a S A Tor @command. For example, in a Windows environment, you can TR execute a SQL script as follows: SL @:m_cit\yslsrp.q Q> c\ysrpsm_q_citsl A SQL script file can be executed in a Linux environment as follows: SL SAT/oecoe/ysrpsm_q_citsl Q> TR hm/jnsm_cit/yslsrp.q You can use S TE H O to cause a script to echo each statement that is executed. You can use S TT R O T E CO N E EMU O Fto prevent the script output from displaying on the screen. F When running a script, you need to include the full path name unless the script is located in the directory from which SQL Command Line was started, or the script is located in the default script location specified by the S L A H QPT environment variable.
docs.oracle.com/cd/B25329_01/doc/appdev.102/b25108/xedev_sqlplus.htm#BJEEGJGJ 4/6

12/6/12

A Using SQL Command Line

See Also: Oracle Database Express Edition 2 Day DBA for information about setting environment variables for Oracle Database Express Edition SQL*Plus User's Guide and Reference for information about setting the SQL Command Line S L A Henvironment variable to specify the default location of SQL QPT scripts

Spooling From SQL Command Line


The S O Lcommand can be used to direct the output from SQL Command Line to a disk file, which enables you to PO save the output for future review. To start spooling the output to an operating system file, you enter the S O Lcommand followed by a file name. For PO example: S L S O Lm _ o _ i e l g Q> PO ylgfl.o If you want to append the output to an existing file: S L S O Lm _ o _ i e l gA P N Q> PO ylgfl.o PED To stop spooling and close a file, enter the following: SL SOLOF Q> PO F

Using Variables With SQL Command Line


You can create queries that use variables to make S L C statements more flexible. You can define the variable EET before running a SQL statement, or you specify that the statement prompts for a variable value at the time that the SQL statement is run. When using a variable in a SQL statement, the variable name must be begin with an ampersand (&). This section contains the following topics: Prompting for a Variable Value in a Query Reusing a Variable Value in a Query Defining a Variable Value for a Query For information about using bind variables in PL/SQL code, see "Using Bind Variables With PL/SQL". Prompting for a Variable Value in a Query You can use &to identify a variable that you want to define dynamically. In Example A-1, including the & m l y e i variable causes the SQL statement to prompt for a value when the statement is executed. You can epoe_d then enter a value for the e p o e _ dthat corresponds to the employee information that you want to display, mlyei such as employee ID 125. Note that you can use any name for the variable, such as & y v r a l . m_aibe Example A-1 Prompting for a Variable Value in SQL Command Line
docs.oracle.com/cd/B25329_01/doc/appdev.102/b25108/xedev_sqlplus.htm#BJEEGJGJ 5/6

12/6/12

A Using SQL Command Line

- pop frepoe_di aqey yune t etravldI sc a 15 - rmt o mlyei n ur, o ed o ne ai D uh s 2 SLC epoe_d ls_ae jbi FO epoes EET mlyei, atnm, o_d RM mlye WEEepoe_d=&mlyei; HR mlyei epoe_d When you run the previous S L C statement, the output is similar to: EET Etrvlefrepoe_d 15 ne au o mlyei: 2 ... EPOE_DLS_AE MLYEI ATNM JBI O_D ---------------------------- ------------ ----15Nyr 2 ae S_LR TCEK Reusing a Variable Value in a Query You can use & to identify a variable that you want to define dynamically multiple times, but only want to prompt the & user once. In Example A-2, including the & c l m _ a evariable causes the SQL statement to prompt for a &ounnm value when the statement is executed. The value that is entered is substituted for all remaining occurrences of & c l m _ a ein the SQL statement. &ounnm Example A-2 Reusing a Variable Value in SQL Command Line - pop fraclm nm,sc a jbi,wihi te sbtttdi te - rmt o oun ae uh s o_d hc s hn usiue n h - rmiigietclsbttto vralspeie wt & - eann dnia usiuin aibe rfxd ih & SLC epoe_d ls_ae &clm_aeFO epoes EET mlyei, atnm, &ounnm RM mlye ODRB &clm_ae RE Y &ounnm; Defining a Variable Value for a Query In Example A-3, the & o _ dvariable is defined before running the SQL statement with the D F N command, jbi EIE and the defined value is substituted for the variable when the statement is executed. Because the variable has already been defined, you are not prompted to enter a value. Example A-3 Defining a Variable for a Query in SQL Command Line - dfn avral vlefraqeya flos - eie aibe au o ur s olw DFN jbi ="TCEK EIE o_d S_LR" - rnaqeyuigtedfndvlefrjbi (TCEK - u ur sn h eie au o o_d S_LR) SLC epoe_d ls_aeFO epoesWEEjbi ='jbi' EET mlyei, atnm RM mlye HR o_d &o_d;

Previous Next

Copyright 2005, 2006, Oracle. All rights reserved.

Home Book Contents Index Master Contact List Index Us

docs.oracle.com/cd/B25329_01/doc/appdev.102/b25108/xedev_sqlplus.htm#BJEEGJGJ

6/6

You might also like