首页 > 开发 > 综合 > 正文

SQL*PLUS命令的使用大全

2024-07-21 02:05:27
字体:
来源:转载
供稿:网友

                               sql*plus命令的使用大全            oracle的sql*plus是与oracle进行交互的客户端工具。在sql*plus中,可以运行sql*plus命令与sql*plus语句。    我们通常所说的dml、ddl、dcl语句都是sql*plus语句,它们执行完后,都可以保存在一个被称为sql buffer的内存区域中,并且只能保存一条最近执行的sql语句,我们可以对保存在sql buffer中的sql 语句进行修改,然后再次执行,sql*plus一般都与数据库打交道。    除了sql*plus语句,在sql*plus中执行的其它语句我们称之为sql*plus命令。它们执行完后,不保存在sql buffer的内存区域中,它们一般用来对输出的结果进行格式化显示,以便于制作报表。    下面就介绍一下一些常用的sql*plus命令:   1. 执行一个sql脚本文件 sql>start file_name sql>@ file_name 我们可以将多条sql语句保存在一个文本文件中,这样当要执行这个文件中的所有的sql语句时,用上面的任一命令即可,这类似于dos中的批处理。   2. 对当前的输入进行编辑 sql>edit   3. 重新运行上一次运行的sql语句 sql>/   4. 将显示的内容输出到指定文件 sql> spool file_name    在屏幕上的所有内容都包含在该文件中,包括你输入的sql语句。   5. 关闭spool输出 sql> spool off    只有关闭spool输出,才会在输出文件中看到输出的内容。   6.显示一个表的结构 sql> desc table_name   7. col命令: 主要格式化列的显示形式。 该命令有许多选项,具体如下: 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]   1). 改变缺省的列标题 column column_name heading column_heading for example: sql>select * from dept;      deptno dname                        loc ---------- ---------------------------- ---------          10 accounting                   new york sql>col  loc heading location sql>select * from dept;     deptno dname                        location --------- ---------------------------- -----------         10 accounting                   new york   2). 将列名ename改为新列名employee name并将新列名放在两行上: sql>select * from emp department  name           salary ---------- ---------- ----------          10 aaa                11         sql> column ename heading ’employee|name’ sql>select * from emp             employee department  name           salary ---------- ---------- ----------           10 aaa                11 note: the col heading turn into two lines from one line.   3). 改变列的显示长度: for[mat] format sql>select empno,ename,job from emp;       empno ename      job        ---------- ----------     ---------        7369 smith      clerk             7499 allen      salesman   7521 ward       salesman   sql> col ename format a40       empno ename                                    job ----------   ----------------------------------------         ---------        7369 smith                                    clerk        7499 allen                                    salesman        7521 ward                                    salesman   4). 设置列标题的对齐方式 jus[tify] {l[eft]|c[enter]|c[entre]|r[ight]} sql> col ename justify center sql> /       empno           ename                   job ----------   ----------------------------------------       ---------        7369 smith                                    clerk        7499 allen                                    salesman 7521 ward                                     salesman 对于number型的列,列标题缺省在右边,其它类型的列标题缺省在左边   5). 不让一个列显示在屏幕上 nopri[nt]|pri[nt] sql> col job noprint sql> /       empno           ename ----------     ----------------------------------------        7369 smith        7499 allen 7521 ward   6). 格式化number类型列的显示: sql> column sal format $99,990 sql> / employee department name        salary    commission ---------- ---------- --------- ---------- 30          allen        $1,600    300   7). 显示列值时,如果列值为null值,用text值代替null值 comm nul[l] text sql>col comm nul[l] text   8). 设置一个列的回绕方式 wra[pped]|wor[d_wrapped]|tru[ncated]         col1 -------------------- how are you?   sql>col col1 format a5 sql>col col1 wrapped col1 ----- how a re yo u?   sql> col col1 word_wrapped col1 ----- how are you?   sql> col col1 word_wrapped col1 ----- how a   9). 显示列的当前的显示属性值 sql> column column_name   10). 将所有列的显示属性设为缺省值 sql> clear columns   8. 屏蔽掉一个列中显示的相同的值 break on break_column sql> break on deptno sql> select deptno, ename, sal from emp   where sal < 2500   order by deptno; deptno      ename         sal ---------- ----------- --------- 10           clark        2450 miller      1300 20            smith       800 adams       1100   9. 在上面屏蔽掉一个列中显示的相同的值的显示中,每当列值变化时在值变化之前插入n个空行。 break on break_column skip n   sql> break on deptno skip 1 sql> / deptno ename sal ---------- ----------- --------- 10 clark 2450 miller 1300   20 smith 800 adams 1100   10. 显示对break的设置 sql> break   11. 删除6、7的设置 sql> clear breaks   12. set 命令: 该命令包含许多子命令: set system_variable value system_variable value 可以是如下的子句之一: appi[nfo]{on|off|text} array[size] {15|n} 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] {.|c} cmds[ep] {;|c|on|off} colsep {_|text} com[patibility]{v7|v8|native} con[cat] {.|c|on|off} copyc[ommit] {0|n} copytypecheck {on|off} def[ine] {&|c|on|off} describe [depth {1|n|all}][linenum {on|off}][indent {on|off}] echo {on|off} editf[ile] file_name[.ext] emb[edded] {on|off} esc[ape] {/|c|on|off} feed[back] {6|n|on|off} 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] {80|n} lobof[fset] {n|1} logsource [pathname] long {80|n} longc[hunksize] {80|n} mark[up] html [on|off] [head text] [body text] [entmap {on|off}] [spool {on|off}] [pre[format] {on|off}] newp[age] {1|n|none} null text numf[ormat] format num[width] {10|n} pages[ize] {24|n} 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] {-|c|on|off} ver[ify] {on|off} wra[p] {on|off}   1). 设置当前session是否对修改的数据进行自动提交 sql>set auto[commit] {on|off|imm[ediate]| n}   2).在用start命令执行一个sql脚本时,是否显示脚本中正在执行的sql语句 sql> set echo {on|off}   3).是否显示当前sql语句查询或修改的行数 sql> set feed[back] {6|n|on|off}    默认只有结果大于6行时才显示结果的行数。如果set feedback 1 ,则不管查询到多少行都返回。当为off 时,一律不显示查询的行数   4).是否显示列标题 sql> set hea[ding] {on|off} 当set heading off 时,在每页的上面不显示列标题,而是以空白行代替   5).设置一行可以容纳的字符数 sql> set lin[esize] {80|n}    如果一行的输出内容大于设置的一行可容纳的字符数,则折行显示。   6).设置页与页之间的分隔 sql> set newp[age] {1|n|none} 当set newpage 0 时,会在每页的开头有一个小的黑方框。 当set newpage n 时,会在页和页之间隔着n个空行。 当set newpage none 时,会在页和页之间没有任何间隔。   7).显示时,用text值代替null值 sql> set null text   8).设置一页有多少行数 sql> set pages[ize] {24|n} 如果设为0,则所有的输出内容为一页并且不显示列标题   9).是否显示用dbms_output.put_line包进行输出的信息。 sql> set serverout[put] {on|off}  在编写存储过程时,我们有时会用dbms_output.put_line将必要的信息输出,以便对存储过程进行调试,只有将serveroutput变量设为on后,信息才能显示在屏幕上。   10).当sql语句的长度大于linesize时,是否在显示时截取sql语句。 sql> set wra[p] {on|off}    当输出的行的长度大于设置的行的长度时(用set linesize n命令设置),当set wrap on时,输出行的多于的字符会另起一行显示,否则,会将输出行的多于字符切除,不予显示。   11).是否在屏幕上显示输出的内容,主要用与spool结合使用。 sql> set term[out] {on|off}    在用spool命令将一个大表中的内容输出到一个文件中时,将内容输出在屏幕上会耗费大量的时间,设置set termspool off后,则输出的内容只会保存在输出文件中,不会显示在屏幕上,极大的提高了spool的速度。   12).将spool输出中每行后面多余的空格去掉 sql> set trims[out] {on|off}      13)显示每个sql语句花费的执行时间 set timing  {on|off}   14.修改sql buffer中的当前行中,第一个出现的字符串 c[hange] /old_value/new_value sql> l    1* select * from dept sql> c/dept/emp    1* select * from emp   15.编辑sql buffer中的sql语句 edi[t]   16.显示sql buffer中的sql语句,list n显示sql buffer中的第n行,并使第n行成为当前行 l[ist] [n]   17.在sql buffer的当前行下面加一行或多行 i[nput]   18.将指定的文本加到sql buffer的当前行后面 a[ppend] sql> select deptno,    2  dname    3  from dept;      deptno dname ---------- --------------          10 accounting          20 research          30 sales          40 operations   sql> l 2    2* dname sql> a ,loc    2* dname,loc sql> l    1  select deptno,    2  dname,loc    3* from dept sql> /        deptno dname          loc ---------- -------------- -------------          10 accounting     new york          20 research       dallas          30 sales          chicago          40 operations     boston   19.将sql buffer中的sql语句保存到一个文件中 save file_name   20.将一个文件中的sql语句导入到sql buffer中 get file_name   21.再次执行刚才已经执行的sql语句 run or /   22.执行一个存储过程 execute procedure_name   23.在sql*plus中连接到指定的数据库 connect user_name/[email protected]_alias   24.设置每个报表的顶部标题 ttitle   25.设置每个报表的尾部标题 btitle   26.写一个注释 remark [text]   27.将指定的信息或一个空行输出到屏幕上 prompt [text]   28.将执行的过程暂停,等待用户响应后继续执行 pause [text]   sql>pause adjust paper and press return to continue.   29.将一个数据库中的一些数据拷贝到另外一个数据库(如将一个表的数据拷贝到另一个数据库) copy {from database | to database | from database to database} {append|create|insert|replace} destination_table [(column, column, column, ...)] using query   sql>copy from scott/[email protected] to john/[email protected]  create emp_temp using select * from emp   30.不退出sql*plus,在sql*plus中执行一个操作系统命令: host   sql> host hostname 该命令在windows下可能被支持。   31.在sql*plus中,切换到操作系统命令提示符下,运行操作系统命令后,可以再次切换回sql*plus: !   sql>! $hostname $exit sql>   该命令在windows下不被支持。   32.显示sql*plus命令的帮助 help 如何安装帮助文件: sql>@ ? qlplus/admin/help/hlpbld.sql ? qlplus/admin/help/helpus.sql sql>help index   33.显示sql*plus系统变量的值或sql*plus环境变量的值 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.]name] lno parameters [parameter_name] pno rel[ease] repf[ooter] reph[eader] sga spoo[l] sqlcode tti[tle] user   1) . 显示当前环境变量的值: show all   2) . 显示当前在创建函数、存储过程、触发器、包等对象的错误信息 show error 当创建一个函数、存储过程等出错时,变可以用该命令查看在那个地方出错及相应的出错信息,进行修改后再次进行编译。   3) . 显示初始化参数的值: show parameters [parameter_name]   4) . 显示数据库的版本: show rel[ease]   5) . 显示sga的大小 show sga   6). 显示当前的用户名 show user

转此:http://www.cnoug.org/viewthread.php?tid=16867
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表