SQLPLUS小技巧带行号SQL的粘贴

介绍一个SQLPLUS中粘贴SQL语句的小技巧。
除了Windows环境下的SQLPLUSW之外,当在sqlplus中运行了一个很长的语句后,得到的是一个带有行号的SQL:

SQL> SELECT ksppinm name,
2 ksppstvl VALUE, 
3 ksppdesc des
4 FROM x$ksppi x, x$ksppcv y 
5 WHERE (x.indx = y.indx) 
6 AND bitand(ksppiflg,268435456) = 0 
7 AND ksppinm LIKE 'max%size';
NAME               VALUE         DES
------------------ ------------- -------------------------------
max_dump_file_size UNLIMITED     Maximum SIZE (blocks) OF dump file

对于上面的结果,如果想要复制粘贴到SQLPLUS中会显得非常麻烦,必需要找到一个支持列选的编辑器,将前面的行号编辑掉,否则虽然也可以粘贴到SQLPLUS中,但是还要通过行编辑方式去掉每行的行号,整个操作显得十分烦琐:

SQL> SELECT ksppinm name,
2 2 ksppstvl VALUE, 
3 3 ksppdesc des
4 4 FROM x$ksppi x, x$ksppcv y 
5 5 WHERE (x.indx = y.indx) 
6 6 AND bitand(ksppiflg,268435456) = 0 
7 7 AND ksppinm LIKE 'max%size' 
8 
SQL> 2
2* 2 ksppstvl VALUE,
SQL> c/2
2* ksppstvl VALUE,
SQL> 3
3* 3 ksppdesc des
SQL> c/3
3* ksppdesc des
SQL> 4
4* 4 FROM x$ksppi x, x$ksppcv y
SQL> c/4
4* FROM x$ksppi x, x$ksppcv y
SQL> 5
5* 5 WHERE (x.indx = y.indx)
SQL> c/5
5* WHERE (x.indx = y.indx)
SQL> 6
6* 6 AND bitand(ksppiflg,268435456) = 0
SQL> c/6
6* AND bitand(ksppiflg,268435456) = 0
SQL> 7 
7* 7 AND ksppinm LIKE 'max%size'
SQL> c/7
7* AND ksppinm LIKE 'max%size'
SQL> l
1 SELECT ksppinm name,
2 ksppstvl VALUE,
3 ksppdesc des
4 FROM x$ksppi x, x$ksppcv y
5 WHERE (x.indx = y.indx)
6 AND bitand(ksppiflg,268435456) = 0
7* AND ksppinm LIKE 'max%size'
SQL> r
1 SELECT ksppinm name,
2 ksppstvl VALUE,
3 ksppdesc des
4 FROM x$ksppi x, x$ksppcv y
5 WHERE (x.indx = y.indx)
6 AND bitand(ksppiflg,268435456) = 0
7* AND ksppinm LIKE 'max%size'
NAME               VALUE         DES
------------------ ------------- -------------------------------
max_dump_file_size UNLIMITED     Maximum SIZE (blocks) OF dump file

显然如果没有列式编辑,处理这种情况会非常麻烦,其实有一个方法可以很方便的处理这种情况,先将第一行粘贴到SQLPLUS中,然后输入两次回车退出当前语句输入模式,然后就可以直接将剩下的所有包含行号的行粘贴到SQLPLUS中:

SQL> SELECT ksppinm name,
2 
SQL> 2 ksppstvl VALUE, 
SQL> 3 ksppdesc des
SQL> 4 FROM x$ksppi x, x$ksppcv y 
SQL> 5 WHERE (x.indx = y.indx) 
SQL> 6 AND bitand(ksppiflg,268435456) = 0 
SQL> 7 AND ksppinm LIKE 'max%size';
SQL> l
1 SELECT ksppinm name,
2 ksppstvl VALUE,
3 ksppdesc des
4 FROM x$ksppi x, x$ksppcv y
5 WHERE (x.indx = y.indx)
6 AND bitand(ksppiflg,268435456) = 0
7* AND ksppinm LIKE 'max%size'
SQL> r
1 SELECT ksppinm name,
2 ksppstvl VALUE,
3 ksppdesc des
4 FROM x$ksppi x, x$ksppcv y
5 WHERE (x.indx = y.indx)
6 AND bitand(ksppiflg,268435456) = 0
7* AND ksppinm LIKE 'max%size'
NAME               VALUE         DES
------------------ ------------- -------------------------------
max_dump_file_size UNLIMITED     Maximum SIZE (blocks) OF dump file

Oracle在这种情况下会将粘贴语句中的行号真正的作为当前语句的行号。这句话比较难理解,其实在Oracle已经缓冲区中包含多个记录行时,可以通过语句前面添加行号的方式,指定当前的内容替换缓冲区中的第几行:

SQL> 8 abc
SQL> l
1 SELECT ksppinm name,
2 ksppstvl VALUE,
3 ksppdesc des
4 FROM x$ksppi x, x$ksppcv y
5 WHERE (x.indx = y.indx)
6 AND bitand(ksppiflg,268435456) = 0
7 AND ksppinm LIKE 'max%size'
8* abc
SQL> 5 DESC
SQL> l
1 SELECT ksppinm name,
2 ksppstvl VALUE,
3 ksppdesc des
4 FROM x$ksppi x, x$ksppcv y
5 DESC
6 AND bitand(ksppiflg,268435456) = 0
7 AND ksppinm LIKE 'max%size'
8* abc

使用这个方法烦人的行号问题就可以方便的解决了。

This entry was posted in ORACLE and tagged , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *