使用sql*plus編輯sql文字(r4筆記第53天)
工作中可能會經常實用工具來編輯sql 文字,實用sql*plus來編輯的機會比較少,但是這些也是硬功夫,一旦有需要手工編輯,其實發現也是很容易的。 關於編輯使用的命令如下,其實看起來一大堆,主要的命令還是增(input)刪(del)改(change)查(list),按照這個思路來看就會容易很多,有些命令也是選擇性的使用。
Command |
Abbreviation |
Purpose |
---|---|---|
APPEND text |
A text |
adds text at the end of the current line |
CHANGE/old/new |
C/old/new |
changes old to new in the current line |
CHANGE/text |
C/text |
deletes text from the current line |
CLEAR BUFFER |
CL BUFF |
deletes all lines |
DEL |
(none) |
deletes the current line |
DEL n |
(none) |
deletes line n |
DEL * |
(none) |
deletes the current line |
DEL n * |
(none) |
deletes line n through the current line |
DEL LAST |
(none) |
deletes the last line |
DEL m n |
(none) |
deletes a range of lines (m to n) |
DEL * n |
(none) |
deletes the current line through line n |
INPUT |
I |
adds one or more lines |
INPUT text |
I text |
adds a line consisting of text |
LIST |
; or L |
lists all lines in the SQL buffer |
LIST n |
L n or n |
lists line n |
LIST * |
L * |
lists the current line |
LIST n * |
L n * |
lists line n through the current line |
LIST LAST |
L LAST |
lists the last line |
LIST m n |
L m n |
lists a range of lines (m to n) |
LIST * n |
L * n |
lists the current line through line n |
最後使用一個例項來說明一下,我們有下面的sql語句。
select c from
(select * from
(select 'oracle' cc, level no from dual connect by level <= length('oracle'))
model return updated rows
dimension by (no)
measures (cc c, no n)
rules (
c[any] = substr(c[cv()],n[cv()],1)
));
我們需要做的就是把它修改成為一個不可執行的結構,然後把它再改回去。
需要修改成如下的樣子,標黃的部分是需要刪除的。
select c from
(select * from
(select 'oracle' cc, level no from dual connect by level <= length('oracle'))
model return updated rows
dimension by (no)
measures (cc c, no n)
rules (
c[any] = substr(c[cv()],n[cv()],1)
));
可以使用下面的命令來完成。
SQL> select c from
2 (select * from
3 (select 'oracle' cc, level no from dual connect by level <= length('oracle'))
4 model return updated rows
5 dimension by (no)
6 measures (cc c, no n)
7 rules (
8 c[any] = substr(c[cv()],n[cv()],1)
9 ));
C
------
o
r
a
c
l
e
6 rows selected.
SQL> del 1 --刪除第1行,第2行變成了第1行
SQL> c/(sel/sel --把第1行的(sel變成sel
1* select * from
SQL> l --列出修改後的語句情況
1 select * from
2 (select 'oracle' cc, level no from dual connect by level <= length('oracle'))
3 model return updated rows
4 dimension by (no)
5 measures (cc c, no n)
6 rules (
7 c[any] = substr(c[cv()],n[cv()],1)
8* ))
SQL> del 3 --我們嘗試刪除第3行
SQL> l --列出修改後的語句情況
1 select * from
2 (select 'oracle' cc, level no from dual connect by level <= length('oracle'))
3 dimension by (no)
4 measures (cc c, no n)
5 rules (
6 c[any] = substr(c[cv()],n[cv()],1)
7* ))
SQL> l last --列出最後的一行語句
7* ))
SQL> c/))/) --把))替換為)
7* )
SQL> l --列出修改後的sql情況
1 select * from
2 (select 'oracle' cc, level no from dual connect by level <= length('oracle'))
3 dimension by (no)
4 measures (cc c, no n)
5 rules (
6 c[any] = substr(c[cv()],n[cv()],1)
7* )
SQL> / --這個時候執行,語句按照期望是不能執行的。
dimension by (no)
*
ERROR at line 3:
ORA-00933: SQL command not properly ended
我們預期的結果達到了,然後我們需要把結果改回去,讓它可執行。
SQL> 0 select c from --我們在第1行前增加一行
SQL> l --列出修改後的sql情況
1 select c from
2 select * from
3 (select 'oracle' cc, level no from dual connect by level <= length('oracle'))
4 dimension by (no)
5 measures (cc c, no n)
6 rules (
7 c[any] = substr(c[cv()],n[cv()],1)
8* )
SQL> l 1 2 --列出第1-2行,然後定位在第2行
1 select c from
2* select * from
SQL> c/sel/(sel --把第2行的sel修改為(sel
2* (select * from
SQL> l --列出修改後的sql情況
1 select c from
2 (select * from
3 (select 'oracle' cc, level no from dual connect by level <= length('oracle'))
4 dimension by (no)
5 measures (cc c, no n)
6 rules (
7 c[any] = substr(c[cv()],n[cv()],1)
8* )
SQL> c/)/)) --把最後1行的)修改為))
8* ))
SQL> l --把修改後的sql語句列出來
1 select c from
2 (select * from
3 (select 'oracle' cc, level no from dual connect by level <= length('oracle'))
4 dimension by (no)
5 measures (cc c, no n)
6 rules (
7 c[any] = substr(c[cv()],n[cv()],1)
8* ))
SQL> l 3 --列出第3行的sql 語句
3* (select 'oracle' cc, level no from dual connect by level <= length('oracle'))
SQL> append model return updated rows --在第3行後追加一行內容
3* (select 'oracle' cc, level no from dual connect by level <= length('oracle'))model return updated rows
SQL> l --列出修改後的sql語句情況
1 select c from
2 (select * from
3 (select 'oracle' cc, level no from dual connect by level <= length('oracle'))model return updated rows
4 dimension by (no)
5 measures (cc c, no n)
6 rules (
7 c[any] = substr(c[cv()],n[cv()],1)
8* ))
SQL> / --執行sql語句的結果
C
------
o
r
a
c
l
e
6 rows selected.
使用sql*plus所做的修改就這樣完成了,其實很多操作還是可控的,修改的過程是一個互動式的過程,和vi操作略有不同,但是還是比較實用的。