| 订阅 | 在线投稿
分享
 
 
 

Oracle中SQLPLUS的常用指令收集与技巧

2008-08-15 06:29:04 编辑來源:互联网 国际版 评论
 
 
本文为【Oracle中SQLPLUS的常用指令收集与技巧】的汉字拼音对照版显示拼音
  anzhuangleoracle9ijinrusal*plusxuyaoshuruyonghumingkoulinghezhujibiaozhifu

  system/manager

  sys/change_on_intall

  scott/tiger

  internal/oracle

  yishangweichushimima

  zhujizifuchuankonghuozheshi@+nidefuwuming,zaibendidenglubuxuyaobendifuwuming

  xiamianjiujieshaoyixiayixiechangyongdesql*plusmingling

  shouxianchakandangqian使shiyongdeshujukushili:

  select name from V$database;

  qiehuanlianggeshujukushili

  zaisqlplusliconnect username/password@sid

  huohost set ORACLE_SID=sid

  huocmdliset ORACLE_SID=sid

  huoxiugaizhucebiaoliORACLE_SIDxiang

  1. zhixingyigeSQLjiaobenwenjian

  SQL>start file_name

  SQL>@ file_name

  womenkeyijiangduotiaosqlyujubaocunzaiyigewenbenwenjianzhongzheyangdangyaozhixingzhegewenjianzhongdesuoyoudesqlyujushiyongshangmianderenyiminglingjikezheleisiyudoszhongdepichuli

  2. duidangqiandeshurujinxingbianji

  SQL>edit

  3. chongxinyunxingshangyiciyunxingdesqlyuju

  SQL>/

  4. jiangxianshideneirongshuchudaozhidingwenjian

  SQL> SPOOL file_name

  zaipingmushangdesuoyouneirongdoubaohanzaigaiwenjianzhongbaokuonishurudesqlyuju

  5. guanbispoolshuchu

  SQL> SPOOL OFF

  zhiyouguanbispoolshuchucaihuizaishuchuwenjianzhongkandaoshuchudeneirong

  6xianshiyigebiaodejiegou

  SQL> desc table_name

  7. COLmingling

  zhuyaogeshihualiedexianshixingshi

  gaiminglingyouxuduoxuanxiangjutiruxia

  COL[UMN] [{ column|expr} [ option ...]]

  Optionxuanxiangkeyishiruxiadeziju:

  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). gaibianqueshengdeliebiaoti

  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). jiangliemingENAMEgaiweixinliemingEMPLOYEE NAMEbingjiangxinliemingfangzailiangxingshang

  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). gaibianliedexianshichangdu

  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). shezhiliebiaotideduiqifangshi

  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

  duiyuNUMBERxingdelieliebiaotiqueshengzaiyoubianqitaleixingdeliebiaotiqueshengzaizuobian

  5). burangyigeliexianshizaipingmushang

  NOPRI[NT]|PRI[NT]

  SQL> col job noprint

  SQL> /

  EMPNO ENAME

  ---------- ----------------------------------------

  7369 SMITH

  7499 ALLEN

  7521 WARD

  6). geshihuaNUMBERleixingliedexianshi

  SQL> COLUMN SAL FORMAT $99,990

  SQL> /

  Employee

  Department Name Salary Commission

  ---------- ---------- --------- ----------

  30 ALLEN $1,600 300

  7). xianshiliezhishiruguoliezhiweiNULLzhiyongtextzhidaitiNULLzhi

  COMM NUL[L] text

  SQL>COL COMM NUL[L] text

  8). shezhiyigeliedehuiraofangshi

  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). xianshiliededangqiandexianshishuxingzhi

  SQL> COLUMN column_name

  10). jiangsuoyouliedexianshishuxingsheweiqueshengzhi

  SQL> CLEAR COLUMNS

  8. pingbidiaoyigeliezhongxianshidexiangtongdezhi

  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. zaishangmianpingbidiaoyigeliezhongxianshidexiangtongdezhidexianshizhongmeidangliezhibianhuashizaizhibianhuazhiqiancharungekongxing

  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. xianshiduiBREAKdeshezhi

  SQL> BREAK

  11. shanchu67deshezhi

  SQL> CLEAR BREAKS

  12. Set mingling

  gaiminglingbaohanxuduozimingling

  SET system_variable value

  system_variable value keyishiruxiadezijuzhiyi

  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). shezhidangqiansessionshifouduixiugaideshujujinxingzidongtijiao

  SQL>SET AUTO[COMMIT] {ON|OFF|IMM[EDIATE]| n}

  2)zaiyongstartminglingzhixingyigesqljiaobenshishifouxianshijiaobenzhongzhengzaizhixingdeSQLyuju

  SQL> SET ECHO {ON|OFF}

  3).shifouxianshidangqiansqlyujuchaxunhuoxiugaidexingshu

  SQL> SET FEED[BACK] {6|n|ON|OFF}

  morenzhiyoujieguodayu6xingshicaixianshijieguodexingshuruguoset feedback 1 zebuguanchaxundaoduoshaoxingdoufanhuidangweioff shiyilvbuxianshichaxundexingshu

  4).shifouxianshiliebiaoti

  SQL> SET HEA[DING] {ON|OFF}

  dangset heading off shizaimeiyedeshangmianbuxianshiliebiaotiershiyikongbaixingdaiti

  5).shezhiyixingkeyirongnadezifushu

  SQL> SET LIN[ESIZE] {80|n}

  ruguoyixingdeshuchuneirongdayushezhideyixingkerongnadezifushuzezhexingxianshi

  6).shezhiyeyuyezhijiandefenge

  SQL> SET NEWP[AGE] {1|n|NONE}

  dangset newpage 0 shihuizaimeiyedekaitouyouyigexiaodeheifangkuang

  dangset newpage n shihuizaiyeheyezhijiangezhengekongxing

  dangset newpage none shihuizaiyeheyezhijianmeiyourenhejiange

  7).xianshishiyongtextzhidaitiNULLzhi

  SQL> SET NULL text

  8).shezhiyiyeyouduoshaoxingshu

  SQL> SET PAGES[IZE] {24|n}

  ruguoshewei0zesuoyoudeshuchuneirongweiyiyebingqiebuxianshiliebiaoti

  9).shifouxianshiyongDBMS_OUTPUT.PUT_LINEbaojinxingshuchudexinxi

  SQL> SET SERVEROUT[PUT] {ON|OFF}

  zaibianxiecunchuguochengshiwomenyoushihuiyongdbms_output.put_linejiangbiyaodexinxishuchuyi便bianduicunchuguochengjinxingtiaoshizhiyoujiangserveroutputbianliangsheweionhouxinxicainengxianshizaipingmushang

  10).dangSQLyujudechangdudayuLINESIZEshishifouzaixianshishijiequSQLyuju

  SQL> SET WRA[P] {ON|OFF}

  dangshuchudexingdechangdudayushezhidexingdechangdushiyongset linesize nminglingshezhidangset wrap onshishuchuxingdeduoyudezifuhuilingqiyixingxianshifouzehuijiangshuchuxingdeduoyuzifuqiechubuyuxianshi

  11).shifouzaipingmushangxianshishuchudeneirongzhuyaoyongyuSPOOLjiehe使shiyong

  SQL> SET TERM[OUT] {ON|OFF}

  zaiyongspoolminglingjiangyigedabiaozhongdeneirongshuchudaoyigewenjianzhongshijiangneirongshuchuzaipingmushanghuihaofeidaliangdeshijianshezhiset termspool offhouzeshuchudeneirongzhihuibaocunzaishuchuwenjianzhongbuhuixianshizaipingmushangjidadetigaolespooldesudu

  12).jiangSPOOLshuchuzhongmeixinghoumianduoyudekonggequdiao

  SQL> SET TRIMS[OUT] {ON|OFF}

  13)xianshimeigesqlyujuhuafeidezhixingshijian

  set TIMING {ON|OFF}

  14xiugaisql bufferzhongdedangqianxingzhongdiyigechuxiandezifuchuan

  C[HANGE] /old_value/new_value

  SQL> l

  1* select * from dept

  SQL> c/dept/emp

  1* select * from emp

  15bianjisql bufferzhongdesqlyuju

  EDI[T]

  16xianshisql bufferzhongdesqlyujulist nxianshisql bufferzhongdedinxingbing使shidinxingchengweidangqianxing

  L[IST] [n]

  17zaisql bufferdedangqianxingxiamianjiayixinghuoduoxing

  I[NPUT]

  18jiangzhidingdewenbenjiadaosql bufferdedangqianxinghoumian

  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

  19jiangsql bufferzhongdesqlyujubaocundaoyigewenjianzhong

  SAVE file_name

  20jiangyigewenjianzhongdesqlyujudaorudaosql bufferzhong

  GET file_name

  21zaicizhixinggangcaiyijingzhixingdesqlyuju

  RUN

  or

  /

  22zhixingyigecunchuguocheng

  EXECUTE procedure_name

  23zaisql*pluszhonglianjiedaozhidingdeshujuku

  CONNECT user_name/passwd@db_alias

  24shezhimeigebaobiaodedingbubiaoti

  TTITLE

  25shezhimeigebaobiaodeyibubiaoti

  BTITLE

  26xieyigezhushi

  REMARK [text]

  27jiangzhidingdexinxihuoyigekongxingshuchudaopingmushang

  PROMPT [text]

  28jiangzhixingdeguochengzantingdengdaiyonghuxiangyinghoujixuzhixing

  PAUSE [text]

  Sql>PAUSE Adjust paper and press RETURN to continue.

  29jiangyigeshujukuzhongdeyixieshujukaobeidaolingwaiyigeshujukurujiangyigebiaodeshujukaobeidaolingyigeshujuku

  COPY {FROM database | TO database | FROM database TO database}

  {APPEND|CREATE|INSERT|REPLACE} destination_table

  [(column, column, column, ...)] USING query

  sql>COPY FROM SCOTT/TIGER@HQ TO JOHN/CHROME@WEST

  create emp_temp

  USING SELECT * FROM EMP

  30bu退tuichusql*pluszaisql*pluszhongzhixingyigecaozuoxitongmingling

  HOST

  Sql> host hostname

  gaiminglingzaiwindowsxiakenengbeizhichi

  31zaisql*pluszhongqiehuandaocaozuoxitongminglingtishifuxiayunxingcaozuoxitongminglinghoukeyizaiciqiehuanhuisql*plus

  !

  sql>!

  $hostname

  $exit

  sql>

  gaiminglingzaiwindowsxiabubeizhichi

  32xianshisql*plusminglingdebangzhu

  HELP

  ruheanzhuangbangzhuwenjian

  Sql>@ ?sqlplusadminhelphlpbld.sql ?sqlplusadminhelphelpus.sql

  Sql>help index

  33xianshisql*plusxitongbianliangdezhihuosql*plushuanjingbianliangdezhi

  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) . xianshidangqianhuanjingbianliangdezhi

  Show all

  2) . xianshidangqianzaichuangjianhanshucunchuguochengchufaqibaodengduixiangdecuowuxinxi

  Show error

  dangchuangjianyigehanshucunchuguochengdengchucuoshibiankeyiyonggaiminglingchakanzainagedifangchucuojixiangyingdechucuoxinxijinxingxiugaihouzaicijinxingbianyi

  3) . xianshichushihuashenshudezhi

  show PARAMETERS [parameter_name]

  4) . xianshishujukudebanben

  show REL[EASE]

  5) . xianshiSGAdedaxiao

  show SGA

  6). xianshidangqiandeyonghuming

  show user

  SQL>SET DEFINE ON

  jingchangyongyuyunxingSQL jiaobenzhong duibianliangdehongtihuan(&)

  SQL>DEFINE TSTEMP=TEMPTS

  jiangyunxingjiaobenzhongde&TSTEMP tihuancheng TEMPTS

  SQL>SET DEFINE OFF

  guanbihongtihuan jingchangyongyuyunxingPACKAGEhuocunchuguocheng

  Sql*pluszhongwencangzhehaoduojiqiaoruguozhangwozhexiejiqiaoduiyuzaioracleshujukuxiajinxingkuaisukaifayuyouxiaoweihushujukudoushiyouyide

  1使shiyongSQL*PLUSdongtaishengchengpiliangjiaoben

  jiangspoolyuselectminglingjieheqilai使shiyongkeyishengchengyigejiaobenjiaobenzhongbaohanyoukeyipiliangzhixingmouyirenwudeyuju

  li1

  shengchengyigejiaobenshanchuSCOTTyonghuxiadesuoyoudebiao

  a. chuangjiangen_drop_table.sqlwenjianbaohanruxiayuju

  SPOOL c:drop_table.sql

  SELECT 'DROP TABLE '|| table_name ||';' FROM user_tables;

  SPOOL OFF

  b. yiSCOTTyonghudenglushujuku

  SQLPLUS > @ ..gen_dorp_table.sql

  c. zaicpangenmuluxiahuishengchengwenjiandrop_table.sqlwenjianbaohanshanchusuoyoubiaodeyujuruxiasuoshi:

  SQL> SELECT 'DROP TABLE '|| table_name ||';' FROM user_tables;

  'DROPTABLE'||TABLE_NAME||';'

  --------------------------------------------------------------------------------

  DROP TABLE DEPT;

  DROP TABLE EMP;

  DROP TABLE PARENT;

  DROP TABLE STAT_VENDER_TEMP;

  DROP TABLE TABLE_FORUM;

  5 rows selected.

  SQL> SPOOL OFF

  d. duishengchengdedrop_table.sqlwenjianjinxingbianjiqudiaobubiyaodebufenzhiliuxiadrop table ..yuju

  e. zaiscottyonghuxiayunxingdorp_table.sqlwenjianshanchuscottyonghuxiasuoyoudebiao

  SQLPLUS > @ c:dorp_table.sql

  zaishangmiandecaozuozhongzaishengchengdejiaobenwenjianzhonghuiyouduoyudezifuruyunxingdesqlyujubiaotihuofanhuidexingshuxuyaowomenbianjigaijiaobenhouzaiyunxinggeishijidecaozuodailaizhuduobu便bianlanduoshirendebenxingzhecu使shiwomenyonggengjiandandebanfalaishixianshangmianderenwu

  a. chuangjiangen_drop_table.sqlwenjianbaohanruxiayuju

  set echo off

  set feedback off

  set newpage none

  set pagesize 5000

  set linesize 500

  set verify off

  set pagesize 0

  set term off

  set trims on

  set linesize 600

  set heading off

  set timing off

  set verify off

  set numwidth 38

  SPOOL c:drop_table.sql

  SELECT 'DROP TABLE '|| table_name ||';' FROM user_tables;

  SPOOL OFF

  b. yiSCOTTyonghudenglushujuku

  SQLPLUS > @ ..gen_dorp_table.sql

  c. zaicpangenmuluxiahuishengchengwenjiandrop_table.sqlwenjianbaohanshanchusuoyoubiaodeyujuruxiasuoshi:

  DROP TABLE DEPT;

  DROP TABLE EMP;

  DROP TABLE PARENT;

  DROP TABLE STAT_VENDER_TEMP;

  DROP TABLE TABLE_FORUM;

  d. zaiscottyonghuxiayunxingdorp_table.sqlwenjianshanchuscottyonghuxiasuoyoudebiao

  SQLPLUS > @ c:dorp_table.sql

  2jiangyigebiaozhongdeshujudaochushengchengyigewenbenwenjianlieyuliezhijianyi,gekai

  set echo off

  set feedback off

  set newpage none

  set pagesize 5000

  set linesize 500

  set verify off

  set pagesize 0

  set term off

  set trims on

  set linesize 600

  set heading off

  set timing off

  set verify off

  set numwidth 38

  SPOOL c:drop_table.sql

  select DEPTNO || ',' || DNAME FROM DEPT;

  SPOOL OFF

  jiangshangmiandeneirongbaocunweiyigewenbenwenjianhouyiscottdengluzhixinggaiwenjianhouxianshijieguo

  10,ACCOUNTING

  20,RESEARCH

  30,SALES

  40,OPERATIONS

  tongguoshangmiandelianggeliziwomenkeyijiang

  set echo off

  set feedback off

  set newpage none

  set pagesize 5000

  set linesize 500

  set verify off

  set pagesize 0

  set term off

  set trims on

  set linesize 600

  set heading off

  set timing off

  set verify off

  set numwidth 38

  SPOOL c:jutidewenjianming

  niyaoyunxingdesqlyuju

  SPOOL OFF

  zuoweiyigemobanzhiyaojiangbiyaodeyujujiaruzhegemobanjiukeyile

  zaioracledejiaoxinbanbenzhonghuankeyiyongset colsepminglinglaishixianshangmiandegongneng

  SQL> set colsep ,

  SQL> select * from dept;

  10,ACCOUNTING ,NEW YORK

  20,RESEARCH ,DALLAS

  30,SALES ,CHICAGO

  40,OPERATIONS ,BOSTON

  35,aa ,bb

  3dongtaishengchengspoolminglingsuoxudewenjianming

  zaiwomenshangmiandelizizhongspoolminglingsuoxuyaodewenjianmingdoushigudingdeyoushiwomenxuyaomeitianspoolyicibingqiemeicispooldewenjianmingdoubuxiangtongruwenjianmingbaohandangtianderiqigairuheshixianne

  column dat1 new_value filename;

  select to_char(sysdate,'yyyymmddhh24mi') dat1 from dual;

  spool c:&&filename..txt

  select * from dept;

  spool off;

  4ruhecongjiaobenwenjianzhongdedaoWINDOWShuanjingbianliangdezhi

  zaiwindoszhong

  spool c:temp%ORACLE_SID%.txt

  select * from dept;

  ...

  spool off

  zaishangmiandelizizhongtongguo%ORACLE_SID%defangshiyinyonghuanjingbianliangORACLE_SIDdezhiruguoORACLE_SIDdezhiweiorclzeshengchengdespoolwenjianmingweiorcl.txt

  zaiUNIXzhong

  spool c:temp$ORACLE_SID.txt

  select * from dept;

  ...

  spool off

  zaishangmiandelizizhongtongguo$ORACLE_SIDdefangshiyinyonghuanjingbianliangORACLE_SIDdezhiruguoORACLE_SIDdezhiweiorclzeshengchengdespoolwenjianmingweiorcl.txt

  5ruhezhidingqueshengdebianjijiaobendemulu

  zaisql*pluszhongkeyiyongsaveminglingjiangshangyitiaozhixingdesqlyujubaocundaoyigewenjianzhongdanshiruheshezhigaiwenjiandequeshengmulune

  tongguoSQL> set editfile c:tempfile.sql minglingkeyishezhiqiqueshengmuluweic:tmpequeshengwenjianmingweifile.sql

  6ruhechuqubiaozhongxiangtongdexing

  zhaodaoxiangtongdexing

  SELECT * FROM dept a

  WHERE ROWID <> (SELECT MAX(ROWID)

  FROM dept b

  WHERE a.deptno = b.deptno

  AND a.dname = b.dname -- Make sure all columns are compared

  AND a.loc = b.loc);

  zhushi

  ruguozhizhaodeptnoliexiangtongdexingshangmiandechaxunkeyigaiwei

  SELECT * FROM dept a

  WHERE ROWID <> (SELECT MAX(ROWID)

  FROM dept b

  WHERE a.deptno = b.deptno)

  shanchuxiangtongdexing

  DELETE FROM dept a

  WHERE ROWID <> (SELECT MAX(ROWID

  FROM dept b

  WHERE a.deptno = b.deptno

  AND a.dname = b.dname -- Make sure all columns are compared

  AND a.loc = b.loc);

  zhuyishangmianbingbushanchuliezhiweinulldexing

  7ruhexiangshujukuzhongcharulianggedanyinhao()

  Insert inot dept values(35,aabb,ab);

  zaicharushiyonglianggebiaoshiyige

  8ruheshezhisql*plusdesouxunlujingzheyangzaiyong@minglingshijiubuyongshuruwenjiandequanlujing

  shezhiSQLPATHhuanjingbianliang

  ru

  SQLPATH = C:ORANTDBS;C:APPSSCRIPTS;C:MYSCRIPTS

  9@yu@@dequbieshishenme

  @dengyustartminglingyonglaiyunxingyigesqljiaobenwenjian

  @minglingtiaoyongdangqianmuluxiadehuozhidingquanlujinghuokeyitongguoSQLPATHhuanjingbianliangsouxundaodejiaobenwenjian

  @@yongzaijiaobenwenjianzhongyonglaizhidingyong@@zhixingdewenjianyu@@suozaidewenjianzaitongyimuluerbuyongzhidingquanlujingyebucongSQLPATHhuanjingbianliangzhidingdelujingzhongxunzhaowenjiangaiminglingyibanyongzaiqiantaojiaobenwenjianzhong

  10&yu&&dequbie

  &yonglaichuangjianyigelinshibianliangmeidangyudaozhegelinshibianliangshidouhuitishinishuruyigezhi

  &&yonglaichuangjianyigechijiubianliangjiuxiangyongyongdefineminglinghuodainew_vlauezijudecolumnminglingchuangjiandechijiubianliangyiyangdangyong&&minglingyinyongzhegebianliangshibuhuimeiciyudaogaibianliangjiutishiyonghujianruzhierzhishizaidiyiciyudaoshitishiyici

  rujiangxiamiansanxingyujucunweiyigejiaobenwenjianyunxinggaijiaobenwenjianhuitishisancirangshurudeptnovaldezhi

  select count(*) from emp where deptno = &deptnoval;

  select count(*) from emp where deptno = &deptnoval;

  select count(*) from emp where deptno = &deptnoval;

  jiangxiamiansanxingyujucunweiyigejiaobenwenjianyunxinggaijiaobenwenjianzezhihuitishiyicirangshurudeptnovaldezhi

  select count(*) from emp where deptno = &deptnoval;

  select count(*) from emp where deptno = &deptnoval;

  select count(*) from emp where deptno = &deptnoval;

  11yinrucopydemude

  Copyminglingzailianggeshujukuzhijiankaobeishujushitebieyouyongtebieshigaiminglingkeyizailianggeshujukuzhijianchuandilongxingziduandeshuju

  quedian

  zailianggeshujukuzhijianchuandishujushiyoukenengdiushijingdu(lose precision)

  12wenshenmezaixiugaidaliangdexingshiwodejiaobenhuibiandehenman

  dangtongguoPL/SQLkuaixiugaiyigebiaozhongdexuduoxingshinihuichuangjianzaibiaoshangchuangjianyigecursordanshizhiyouzainiguanbicursorshicaihuishifangROLLBACK SEGMENTzheyangdangcursorrengrandakaishixiugaiguochenghuibianmanzheshiyinweishujukubudebusouxundaliangderollback segmentyi便bianyuweihuduyizhixingweilebimianzheyangqingkuangshizhezaibiaoshangjiayigebiaozhiziduanlaimiaoshugaixingshifouyijingbeixiugairanhouguanbigaicursorranhouzaidakaigaicursormeicikeyixiugai5000xing.原文
 
 
安装了oracle9i,进入sal*plus需要输入用户名、口令和主机标志符 system/manager sys/change_on_intall scott/tiger internal/oracle 以上为初始密码 主机字符串空或者是@+你的服务名,在本地登陆不需要本地服务名。 下面就介绍一下一些常用的sql*plus命令: 首先查看当前使用的数据库实例: select name from V$database; 切换两个数据库实例 在sqlplus里connect [url=mailto:username/password@sid]username/password@sid[/url] 或host set ORACLE_SID=sid 或cmd里set ORACLE_SID=sid 或修改注册表里ORACLE_SID项 ◆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 [url=mailto:user_name/passwd@db_alias]user_name/passwd@db_alias[/url] ◆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 [url=mailto:SCOTT/TIGER@HQ]SCOTT/TIGER@HQ[/url] TO [url=mailto:JOHN/CHROME@WEST]JOHN/CHROME@WEST[/url] 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>@ ?sqlplusadminhelphlpbld.sql ?sqlplusadminhelphelpus.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 SQL>SET DEFINE ON 经常用于运行SQL 脚本中, 对变量的宏替换(&) SQL>DEFINE TSTEMP=TEMPTS 将运行脚本中的&TSTEMP 替换成 TEMPTS SQL>SET DEFINE OFF 关闭宏替换, 经常用于运行PACKAGE或存储过程 Sql*plus中蕴藏着好多技巧,如果掌握这些技巧,对于在oracle数据库下进行快速开发与有效维护数据库都是有益的。 ◆1.使用SQL*PLUS动态生成批量脚本 将spool与select命令结合起来使用,可以生成一个脚本,脚本中包含有可以批量执行某一任务的语句。 例1: 生成一个脚本,删除SCOTT用户下的所有的表: a. 创建gen_drop_table.sql文件,包含如下语句: SPOOL c:drop_table.sql SELECT 'DROP TABLE '|| table_name ||';' FROM user_tables; SPOOL OFF b. 以SCOTT用户登录数据库 SQLPLUS > @ …..gen_dorp_table.sql c. 在c盘根目录下会生成文件drop_table.sql文件,包含删除所有表的语句,如下所示: SQL> SELECT 'DROP TABLE '|| table_name ||';' FROM user_tables; 'DROPTABLE'||TABLE_NAME||';' -------------------------------------------------------------------------------- DROP TABLE DEPT; DROP TABLE EMP; DROP TABLE PARENT; DROP TABLE STAT_VENDER_TEMP; DROP TABLE TABLE_FORUM; 5 rows selected. SQL> SPOOL OFF d. 对生成的drop_table.sql文件进行编辑去掉不必要的部分,只留下drop table …..语句 e. 在scott用户下运行dorp_table.sql文件,删除scott用户下所有的表。 SQLPLUS > @ c:dorp_table.sql 在上面的操作中,在生成的脚本文件中会有多余的字符,如运行的sql语句,标题,或返回的行数,需要我们编辑该脚本后再运行,给实际的操作带来诸多不便。懒惰是人的本性,这促使我们用更简单的办法来实现上面的任务。 a. 创建gen_drop_table.sql文件,包含如下语句: set echo off set feedback off set newpage none set pagesize 5000 set linesize 500 set verify off set pagesize 0 set term off set trims on set linesize 600 set heading off set timing off set verify off set numwidth 38 SPOOL c:drop_table.sql SELECT 'DROP TABLE '|| table_name ||';' FROM user_tables; SPOOL OFF b. 以SCOTT用户登录数据库 SQLPLUS > @ …..gen_dorp_table.sql c. 在c盘根目录下会生成文件drop_table.sql文件,包含删除所有表的语句,如下所示: DROP TABLE DEPT; DROP TABLE EMP; DROP TABLE PARENT; DROP TABLE STAT_VENDER_TEMP; DROP TABLE TABLE_FORUM; d. 在scott用户下运行dorp_table.sql文件,删除scott用户下所有的表。 SQLPLUS > @ c:dorp_table.sql ◆2.将一个表中的数据导出生成一个文本文件,列与列之间以”,”隔开 set echo off set feedback off set newpage none set pagesize 5000 set linesize 500 set verify off set pagesize 0 set term off set trims on set linesize 600 set heading off set timing off set verify off set numwidth 38 SPOOL c:drop_table.sql select DEPTNO || ',' || DNAME FROM DEPT; SPOOL OFF 将上面的内容保存为一个文本文件后,以scott登录,执行该文件后显示结果: 10,ACCOUNTING 20,RESEARCH 30,SALES 40,OPERATIONS 通过上面的两个例子,我们可以将: set echo off set feedback off set newpage none set pagesize 5000 set linesize 500 set verify off set pagesize 0 set term off set trims on set linesize 600 set heading off set timing off set verify off set numwidth 38 SPOOL c:具体的文件名 你要运行的sql语句 SPOOL OFF 作为一个模版,只要将必要的语句假如这个模版就可以了。 在oracle的较新版本中,还可以用set colsep命令来实现上面的功能: SQL> set colsep , SQL> select * from dept; 10,ACCOUNTING ,NEW YORK 20,RESEARCH ,DALLAS 30,SALES ,CHICAGO 40,OPERATIONS ,BOSTON 35,aa ,bb ◆3.动态生成spool命令所需的文件名 在我们上面的例子中,spool命令所需要的文件名都是固定的。有时我们需要每天spool一次,并且每次spool的文件名都不相同,如文件名包含当天的日期,该如何实现呢? column dat1 new_value filename; select to_char(sysdate,'yyyymmddhh24mi') dat1 from dual; spool c:&&filename..txt select * from dept; spool off; ◆4.如何从脚本文件中得到WINDOWS环境变量的值: 在windos中: spool c:temp%ORACLE_SID%.txt select * from dept; ... spool off 在上面的例子中,通过%ORACLE_SID%的方式引用环境变量ORACLE_SID的值,如果ORACLE_SID的值为orcl,则生成的spool文件名为:orcl.txt 在UNIX中: spool c:temp$ORACLE_SID.txt select * from dept; ... spool off 在上面的例子中,通过$ORACLE_SID的方式引用环境变量ORACLE_SID的值,如果ORACLE_SID的值为orcl,则生成的spool文件名为:orcl.txt ◆5.如何指定缺省的编辑脚本的目录 在sql*plus中,可以用save命令,将上一条执行的sql语句保存到一个文件中,但是如何设置该文件的缺省目录呢? 通过SQL> set editfile c:tempfile.sql 命令,可以设置其缺省目录为c:tmpe,缺省文件名为file.sql。 ◆6.如何除去表中相同的行 找到相同的行: SELECT * FROM dept a WHERE ROWID <> (SELECT MAX(ROWID) FROM dept b WHERE a.deptno = b.deptno AND a.dname = b.dname -- Make sure all columns are compared AND a.loc = b.loc); 注释: 如果只找deptno列相同的行,上面的查询可以改为: SELECT * FROM dept a WHERE ROWID <> (SELECT MAX(ROWID) FROM dept b WHERE a.deptno = b.deptno) 删除相同的行: DELETE FROM dept a WHERE ROWID <> (SELECT MAX(ROWID FROM dept b WHERE a.deptno = b.deptno AND a.dname = b.dname -- Make sure all columns are compared AND a.loc = b.loc); 注意:上面并不删除列值为null的行。 ◆7.如何向数据库中插入两个单引号(’’) Insert inot dept values(35,’aa’’’’bb’,’a’’b’); 在插入时,用两个’表示一个’。 ◆8.如何设置sql*plus的搜寻路径,这样在用@命令时,就不用输入文件的全路径。 设置SQLPATH环境变量。 如: SQLPATH = C:ORANTDBS;C:APPSSCRIPTS;C:MYSCRIPTS ◆9.@与@@的区别是什么? @等于start命令,用来运行一个sql脚本文件。 @命令调用当前目录下的,或指定全路径,或可以通过SQLPATH环境变量搜寻到的脚本文件。 @@用在脚本文件中,用来指定用@@执行的文件与@@所在的文件在同一目录,而不用指定全路径,也不从SQLPATH环境变量指定的路径中寻找文件,该命令一般用在嵌套脚本文件中。 ◆10.&与&&的区别 &用来创建一个临时变量,每当遇到这个临时变量时,都会提示你输入一个值。 &&用来创建一个持久变量,就像用用define命令或带new_vlaue字句的column命令创建的持久变量一样。当用&&命令引用这个变量时,不会每次遇到该变量就提示用户键入值,而只是在第一次遇到时提示一次。 如,将下面三行语句存为一个脚本文件,运行该脚本文件,会提示三次,让输入deptnoval的值: select count(*) from emp where deptno = &deptnoval; select count(*) from emp where deptno = &deptnoval; select count(*) from emp where deptno = &deptnoval; 将下面三行语句存为一个脚本文件,运行该脚本文件,则只会提示一次,让输入deptnoval的值: select count(*) from emp where deptno = &deptnoval; select count(*) from emp where deptno = &deptnoval; select count(*) from emp where deptno = &deptnoval; ◆11.引入copy的目的 Copy命令在两个数据库之间拷贝数据时特别有用,特别是该命令可以在两个数据库之间传递long型字段的数据。 缺点: 在两个数据库之间传递数据时,有可能丢失精度(lose precision)。 ◆12.问什么在修改大量的行时,我的脚本会变得很慢? 当通过PL/SQL块修改一个表中的许多行时,你会创建在表上创建一个cursor,但是只有在你关闭cursor时,才会释放ROLLBACK SEGMENT,这样,当cursor仍然打开时,修改过程会变慢,这是因为数据库不得不搜寻大量的rollback segment以便于维护读一致性。为了避免这样情况,试着在表上加一个标志字段来描述该行是否已经被修改,然后关闭该cursor,然后再打开该cursor。每次可以修改5000行.
󰈣󰈤
 
 
>>返回首页<<
 为你推荐
 
 
 
 转载本文
 UBB代码 HTML代码
复制到剪贴板...
 
 
 热帖排行
 
 
王朝网络微信公众号
微信扫码关注本站公众号wangchaonetcn
 
  免责声明:本文仅代表作者个人观点,与王朝网络无关。王朝网络登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述,其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
©2005- 王朝网络 版权所有