最喜欢的 SQL*Plus 提示和技巧

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1439203/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 03:40:08  来源:igfitidea点击:

Favorite SQL*Plus tips and tricks

sqloraclesqlplus

提问by ericp

So many times I just need a quick connection to an Oracle DB, where SQLPLUS handles the job.

很多时候我只需要快速连接到 SQLPLUS 处理工作的 Oracle 数据库。

I imagine when people start using Oracle, the first thing they are told to do is to install Toad or SQLDeveloper. Even so, sometimes you don't want to wait for those tools to load, if you are performing some simple queries.

我想当人们开始使用 Oracle 时,他们被告知要做的第一件事就是安装 Toad 或 SQLDeveloper。即便如此,如果您正在执行一些简单的查询,有时您也不想等待这些工具加载。

I have a script that I run when I start my shell so I get a better experience:

我有一个在启动 shell 时运行的脚本,以便获得更好的体验:

SET pagesize 2000
SET LONG 10000
SET linesize 1000
COLUMN last_name format a20
COLUMN total format 999,999,999
SET feedback ON
alter session set nls_date_format = 'yyyy-mm-dd hh:mi:ssPM';

I trimmed my "COLUMN" settings for this example, but basically, that helps the data fit on the screen.

我为这个例子修剪了我的“COLUMN”设置,但基本上,这有助于数据适合屏幕。

Setting the date format really simplifies dealing with dates.

设置日期格式确实简化了处理日期。

When the command window opens in Windows, I set the window layout properties so I can scroll, have a wider window, etc. and save the settings for future windows.

当命令窗口在 Windows 中打开时,我设置了窗口布局属性,以便我可以滚动、拥有更宽的窗口等,并为以后的窗口保存设置。

Does anybody else use SQL*Plus daily? Any tips?

有其他人每天使用 SQL*Plus 吗?有小费吗?

回答by Dana

You can use rlwrapto add readline support to sqlplus. Run sqlplus like this:

您可以使用rlwrap为 sqlplus 添加 readline 支持。像这样运行 sqlplus:

$ rlwrap -c sqlplus username@database

Now up/down will scroll through command history. Use ctrl-r to search backwards through history, etc. This makes sqlplus bearable.

现在向上/向下将滚动命令历史记录。使用 ctrl-r 向后搜索历史记录等。这使得 sqlplus 可以忍受。

Also, add this to your login.sql to set the linesize to whatever the width of your terminal is:

此外,将此添加到您的 login.sql 以将 linesize 设置为终端的宽度:

HOST echo "set linesize" $(stty -a|head -n1|cut -f7 -d' '|cut -f1 -d';') > .tmp.sql
@.tmp.sql
HOST rm -f .tmp.sql

Both of these tips only work on unix.

这两个技巧仅适用于 unix。

回答by Tony Andrews

Yes, I use SQL Plus every day in preference to Toad or SQL Developer (though I also use SQL Developer to browse the database).

是的,我每天都使用 SQL Plus,而不是 Toad 或 SQL Developer(尽管我也使用 SQL Developer 浏览数据库)。

I have the following in my login.sql script (which SQL Plus runs automatically):

我的 login.sql 脚本中有以下内容(SQL Plus 自动运行):

1) Replace default editor (Notepad) with one of my choice:

1)用我的选择之一替换默认编辑器(记事本):

define _editor = "C:\Program Files\TextPad 5\TextPad.exe"

2) Make SQL prompt show database name so I know where I am (thanks to Tom Kyte for this):

2) 让 SQL 提示符显示数据库名称,以便我知道我在哪里(感谢 Tom Kyte):

COLUMN global_name new_value gname
SET TERMOUT OFF
SELECT LOWER(USER) || '@' || global_name||CHR(10)||'SQL> ' AS global_name
FROM   global_name;
SET SQLPROMPT '&gname'
SET TERMOUT ON

... plus other setting similar to yours.

...加上与您类似的其他设置。

I also find Tom Kyte's print_tableprocedure very useful.

我还发现 Tom Kyte 的print_table过程非常有用。

回答by APC

Remember that we can put these settings in the login.sql script which will be run automatically whenever we start SQL*Plus. Find out more.

请记住,我们可以将这些设置放在 login.sql 脚本中,该脚本将在我们启动 SQL*Plus 时自动运行。 了解更多。

The neat thing about this is, that since 10g, this script is run every time we connect rather just the first time we fire up SQL*Plus...

关于这个的巧妙之处在于,从 10g 开始,这个脚本在我们每次连接时运行,而不是在我们第一次启动 SQL*Plus 时运行......

SQL> conn apc
Enter password:
Connected.
Running login script

Session altered.

SQL> conn scott
Enter password:
Connected.
Running login script

Session altered.

SQL>

回答by Rob van Wijk

I use SQL*Plus exclusively to work with Oracle. Other answers already give some very handy login.sql contents.

我只使用 SQL*Plus 来处理 Oracle。其他答案已经提供了一些非常方便的 login.sql 内容。

This is my login.sql. I copied some suggestions from Tom Kyte and William Robertson in there. Maybe you find some things you want to use as well.

这是我的login.sql。我在那里复制了 Tom Kyte 和 William Robertson 的一些建议。也许你也找到了一些你想使用的东西。

set termout off
set serveroutput on size unlimited
set pagesize 50000
set linesize 135
set long 50000
set trimspool on
set tab off
def _editor = "C:\Progra~1\Notepad++\Notepad++.exe"

define gname=idle
column global_name new_value gname
select lower(user) || '@' ||
       substr(global_name,1,decode(dot,0,length(global_name),dot-1)) global_name
from (select global_name,instr(global_name,'.') dot from global_name);
set sqlprompt '&gname> '

alter session set nls_date_format = 'dd-mm-yyyy hh24:mi:ss'
/
var sid number
var serial# number
var tracefile VARCHAR2(200)

DECLARE
    v_audsid v$session.audsid%TYPE;
BEGIN
    SELECT sid, serial#, audsid
    INTO   :sid, :serial#, v_audsid
    FROM   v$session
    WHERE  audsid = SYS_CONTEXT('USERENV','SESSIONID');

    SELECT par.value ||
           CASE WHEN par.value LIKE '%/%' THEN '/' ELSE '\' END ||
           LOWER(th.instance) ||
           '_ora_' || LTRIM(TO_CHAR(pro.spid,'fm99999')) || '.trc' AS filename
    INTO   :tracefile
    FROM   v$process   pro
         , v$session   se
         , v$parameter par
         , v$thread    th
    WHERE  se.audsid = v_audsid
    AND    pro.addr = se.paddr
    AND    par.NAME = 'user_dump_dest';
END;
/

BEGIN
    IF :sid IS NULL THEN
        SELECT sid
        INTO   :sid
        FROM   v$mystat
        WHERE  rownum = 1;
    END IF;
END;
/

set termout on
set feedback off
exec DBMS_OUTPUT.PUT_LINE('Sessie: ' || :sid || CASE WHEN :serial# IS NULL THEN ' (no access to V$ tables)' ELSE ',' || :serial# END)
exec IF :tracefile IS NOT NULL THEN DBMS_OUTPUT.PUT_LINE('Eventueel trace-bestand: ' || :tracefile); END IF
prompt
set feedback on

回答by Jonathan

I like to use sqlplus in off-line.

我喜欢离线使用sqlplus。

sqlplus -S user/password @query.sql> file.txt

where query.sql is

query.sql 在哪里

set feedback off verify off heading off pagesize 0
...here goes a query...
quit;
/

So i can get info from the database in my bat/script files in windows or unix.

因此,我可以从 Windows 或 unix 中的 bat/script 文件中的数据库中获取信息。

回答by dpbradley

I find it is handy to use SQL*Plus column variables within directives - for example, I'm often in a session and want to spool to a new file name to avoid overwriting another log that may already exist and do this (first three statements through an @file):

我发现在指令中使用 SQL*Plus 列变量很方便 - 例如,我经常在一个会话中并且想要假脱机到一个新的文件名以避免覆盖另一个可能已经存在的日志并执行此操作(前三个语句通过@file):

SQL> column spr new_value spoolref
SQL> select user||'_'||abs(dbms_random.random) spr from dual;
SQL> spool &spoolref
         ... do work here ...
SQL> spool off

I'll then find the new log by sorting by time - you could always use some strategy other than the random number if you prefer.

然后我将通过按时间排序来找到新日志 - 如果您愿意,您始终可以使用随机数以外的其他策略。