Oracle的sql*plus是與oracle進行交互的客戶端工具
我們通常所說的DML
除了sql*plus語句
下面就介紹一下一些常用的sql*plus命令
SQL>start file_name
SQL>@ file_name
我們可以將多條sql語句保存在一個文本文件中
@與@@的區別是什麼?
@等於start命令
@命令調用當前目錄下的
@@用在sql腳本文件中
如
@@nest_start
則我們在sql*plus中
SQL> @ c:\temp\start
SQL>edit
SQL>/
SQL> SPOOL file_name
在屏幕上的所有內容都包含在該文件中
SQL> SPOOL OFF
只有關閉spool輸出
SQL> desc table_name
主要格式化列的顯示形式
該命令有許多選項
COL[UMN] [{ column|expr} [ option
Option選項可以是如下的子句:
ALI[AS] alias
CLE[AR]
FOLD_A[FTER]
FOLD_B[EFORE]
FOR[MAT] format
HEA[DING] text
JUS[TIFY] {L[EFT]|C[ENTER]|C[ENTRE]|R[IGHT]}
LIKE { expr|alias}
NEWL[INE]
NEW_V[ALUE] variable
NOPRI[NT]|PRI[NT]
NUL[L] text
OLD_V[ALUE] variable
ON|OFF
WRA[PPED]|WOR[D_WRAPPED]|TRU[NCATED]
COLUMN column_name HEADING column_heading
For example:
Sql>select * from dept;
DEPTNO DNAME LOC
sql>col LOC heading location
sql>select * from dept;
DEPTNO DNAME location
Sql>select * from emp
Department name Salary
SQL> COLUMN ENAME HEADING
Sql>select * from emp
Employee
Department name Salary
note: the col heading turn into two lines from one line
FOR[MAT] format
Sql>select empno
EMPNO ENAME JOB
Sql> col ename format a
EMPNO ENAME JOB
JUS[TIFY] {L[EFT]|C[ENTER]|C[ENTRE]|R[IGHT]}
SQL> col ename justify center
SQL> /
EMPNO ENAME JOB
對於NUMBER型的列
NOPRI[NT]|PRI[NT]
SQL> col job noprint
SQL> /
EMPNO ENAME
SQL> COLUMN SAL FORMAT $
SQL> /
Employee
Department Name Salary Commission
COMM NUL[L] text
SQL>COL COMM NUL[L] text
WRA[PPED]|WOR[D_WRAPPED]|TRU[NCATED]
COL
HOW ARE YOU?
SQL>COL COL
SQL>COL COL
COL
HOW A
RE YO
U?
SQL> COL COL
COL
HOW
ARE
YOU?
SQL> COL COL
COL
HOW A
SQL> COLUMN column_name
SQL> CLEAR COLUMNS
BREAK ON break_column
SQL> BREAK ON DEPTNO
SQL> SELECT DEPTNO
FROM EMP
WHERE SAL <
ORDER BY DEPTNO;
DEPTNO ENAME SAL
MILLER
ADAMS
BREAK ON break_column SKIP n
SQL> BREAK ON DEPTNO SKIP
SQL> /
DEPTNO ENAME SAL
MILLER
ADAMS
SQL> BREAK
SQL> CLEAR BREAKS
該命令包含許多子命令
SET system_variable value
system_variable value 可以是如下的子句之一
APPI[NFO]{ON|OFF|text}
ARRAY[SIZE] {
AUTO[COMMIT]{ON|OFF|IMM[EDIATE]|n}
AUTOP[RINT] {ON|OFF}
AUTORECOVERY [ON|OFF]
AUTOT[RACE] {ON|OFF|TRACE[ONLY]} [EXP[LAIN]] [STAT[ISTICS]]
BLO[CKTERMINATOR] {
CMDS[EP] {;|c|ON|OFF}
COLSEP {_|text}
COM[PATIBILITY]{V
CON[CAT] {
COPYC[OMMIT] {
COPYTYPECHECK {ON|OFF}
DEF[INE] {&|c|ON|OFF}
DESCRIBE [DEPTH {
ECHO {ON|OFF}
EDITF[ILE] file_name[
EMB[EDDED] {ON|OFF}
ESC[APE] {\|c|ON|OFF}
FEED[BACK] {
FLAGGER {OFF|ENTRY |INTERMED[IATE]|FULL}
FLU[SH] {ON|OFF}
HEA[DING] {ON|OFF}
HEADS[EP] {||c|ON|OFF}
INSTANCE [instance_path|LOCAL]
LIN[ESIZE] {
LOBOF[FSET] {n|
LOGSOURCE [pathname]
LONG {
LONGC[HUNKSIZE] {
MARK[UP] HTML [ON|OFF] [HEAD text] [BODY text] [ENTMAP {ON|OFF}] [SPOOL
{ON|OFF}] [PRE[FORMAT] {ON|OFF}]
NEWP[AGE] {
NULL text
NUMF[ORMAT] format
NUM[WIDTH] {
PAGES[IZE] {
PAU[SE] {ON|OFF|text}
RECSEP {WR[APPED]|EA[CH]|OFF}
RECSEPCHAR {_|c}
SERVEROUT[PUT] {ON|OFF} [SIZE n] [FOR[MAT] {WRA[PPED]|WOR[D_
WRAPPED]|TRU[NCATED]}]
SHIFT[INOUT] {VIS[IBLE]|INV[ISIBLE]}
SHOW[MODE] {ON|OFF}
SQLBL[ANKLINES] {ON|OFF}
SQLC[ASE] {MIX[ED]|LO[WER]|UP[PER]}
SQLCO[NTINUE] {> |text}
SQLN[UMBER] {ON|OFF}
SQLPRE[FIX] {#|c}
SQLP[ROMPT] {SQL>|text}
SQLT[ERMINATOR] {;|c|ON|OFF}
SUF[FIX] {SQL|text}
TAB {ON|OFF}
TERM[OUT] {ON|OFF}
TI[ME] {ON|OFF}
TIMI[NG] {ON|OFF}
TRIM[OUT] {ON|OFF}
TRIMS[POOL] {ON|OFF}
UND[ERLINE] {
VER[IFY] {ON|OFF}
WRA[P] {ON|OFF}
SQL>SET AUTO[COMMIT] {ON|OFF|IMM[EDIATE]| n}
SQL> SET ECHO {ON|OFF}
SQL> SET FEED[BACK] {
默認只有結果大於
SQL> SET HEA[DING] {ON|OFF}
當set heading off 時
SQL> SET LIN[ESIZE] {
如果一行的輸出內容大於設置的一行可容納的字符數
SQL> SET NEWP[AGE] {
當set newpage
當set newpage n 時
當set newpage none 時
SQL> SET NULL text
SQL> SET PAGES[IZE] {
如果設為
SQL> SET SERVEROUT[PUT] {ON|OFF}
在編寫存儲過程時
SQL> SET WRA[P] {ON|OFF}
當輸出的行的長度大於設置的行的長度時(用set linesize n命令設置)
SQL> SET TERM[OUT] {ON|OFF}
在用spool命令將一個大表中的內容輸出到一個文件中時
SQL> SET TRIMS[OUT] {ON|OFF}
set TIMING {ON|OFF}
SET SQLBLANKLINES ON
Sql*plus中
select deptno
from emp
where empno =
如果拷貝到sql*plus中執行
SET SERVEROUTPUT ON BUFFER
用dbms_output
如果想讓dbms_output
SQL> abc
set markup html
在
注意其中的spool on
C[HANGE] /old_value/new_value
SQL> l
SQL> c/dept/emp
EDI[T]
L[IST] [n]
I[NPUT]
A[PPEND]
SQL> select deptno
DEPTNO DNAME
SQL> L
SQL> a
SQL> L
SQL> /
DEPTNO DNAME LOC
SAVE file_name
GET file_name
RUN
or
/
EXECUTE procedure_name
CONNECT user_name/passwd@db_alias
TTITLE
BTITLE
REMARK [text]
PROMPT [text]
PAUSE [text]
Sql>PAUSE Adjust paper and press RETURN to continue
COPY {FROM database | TO database | FROM database TO database}
{APPEND|CREATE|INSERT|REPLACE} destination_table
[(column
sql>COPY FROM SCOTT/TIGER@HQ TO JOHN/CHROME@WEST
create emp_temp
USING SELECT * FROM EMP
HOST
Sql> host hostname
該命令在windows下可能被支持
!
sql>!
$hostname
$exit
sql>
該命令在windows下不被支持
HELP
如何安裝幫助文件
Sql>@ ?\sqlplus\admin\help\hlpbld
Sql>help index
Syntax
SHO[W] option
where option represents one of the following terms or clauses:
system_variable
ALL
BTI[TLE]
ERR[ORS] [{FUNCTION|PROCEDURE|PACKAGE|PACKAGE BODY|
TRIGGER|VIEW|TYPE|TYPE BODY} [schema
LNO
PARAMETERS [parameter_name]
PNO
REL[EASE]
REPF[OOTER]
REPH[EADER]
SGA
SPOO[L]
SQLCODE
TTI[TLE]
USER
Show all
Show error
當創建一個函數
show PARAMETERS [parameter_name]
show REL[EASE]
show SGA
show user
SQL>select * from tab;
SQL>select * from user_objects;
SQL>select * from user_tables;
SQL>select * from user_indexes;
方法有兩個
a
b
OLD_V[ALUE] variable [NOPRI[NT]|PRI[NT]]
下面對每種方式給予解釋
a
DEF[INE] [variable]|[variable = text]
定義一個用戶變量並且可以分配給它一個CHAR值
assign the value MANAGER to the variable POS
SQL> DEFINE POS = MANAGER
assign the CHAR value
SQL> DEFINE DEPTNO =
list the definition of DEPTNO
SQL> DEFINE DEPTNO
―――――――――――――――
DEFINE DEPTNO =
定義了用戶變量POS後
b
NEW_V[ALUE] variable
指定一個變量容納查詢出的列值
例:column col_name new_value var_name noprint
select col_name from table_name where ……
將下面查詢出的col_name列的值賦給var_name變量
一個綜合的例子:
得到一個列值的兩次查詢之差(此例為
column redo_writes new_value commit_count
select sum(stat
from v$sesstat stat
where stat
and sn
execute dbms_lock
set veri off
select sum(stat
from v$sesstat stat
where stat
and sn
VAR[IABLE] [variable [NUMBER|CHAR|CHAR (n)|NCHAR|NCHAR (n) |VARCHAR
定義一個綁定變量
可以用print命令顯示該綁定變量的信息
如
column inst_num heading
column inst_name heading
column db_name heading
column dbid heading
prompt
prompt Current Instance
prompt ~~~~~~~~~~~~~~~~
select d
from v$database d
v$instance i;
variable dbid number;
variable inst_num number;
begin
:dbid := &dbid;
:inst_num := &inst_num;
end;
/
說明
在sql*plus中
注意
綁定變量不同於變量
綁定變量
變量
&用來創建一個臨時變量
&&用來創建一個持久變量
如
select count(*) from emp where deptno = &deptnoval;
select count(*) from emp where deptno = &deptnoval;
select count(*) from emp where deptno = &deptnoval;
將下面三行語句存為一個腳本文件
select count(*) from emp where deptno = &&deptnoval;
select count(*) from emp where deptno = &&deptnoval;
select count(*) from emp where deptno = &&deptnoval;
#
有沒有過這樣的經歷? 在sql*plus中敲了很長的命令後
比如說
SQL> select deptno
這時
這種情況下
SQL>> select deptno
Name Null? Type
EMPNO NOT NULL NUMBER(
ENAME VARCHAR
JOB VARCHAR
MGR NUMBER(
HIREDATE DATE
SAL NUMBER(
COMM NUMBER(
DEPTNO NUMBER(
DEPTNO EMPNO ENAME
From:http://tw.wingwit.com/Article/program/Oracle/201311/18384.html