0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

SQLcl独自の機能を試す(2)

0
Last updated at Posted at 2021-07-07

ALIAS

ALIAS コマンドはコマンドの別名を作成/管理する機能です。
エイリアスを作成するには「ALIAS 別名=SQL文」コマンドを実行します。以下の例ではエイリアス NOW を実行すると「SELECT SYSTIMESTAMP FROM DUAL;」文が実行されます。

ALIAS コマンド
SQL> ALIAS NOW=SELECT SYSTIMESTAMP FROM DUAL;
SQL> NOW

                         SYSTIMESTAMP
-------------------------------------
21-07-06 20:49:43.494044000 +09:00

定義済みのエイリアスの一覧は「ALIAS」コマンドまたは「ALIAS LIST」コマンドで出力できます。作成したエイリアスは ALIAS SAVEコマンドで保存できます。Linux 環境では${HOME}/.sqlcl/aliases.xml ファイルに保存されます。

UNLOAD

テーブルの内容を CSV ファイル等に出力するコマンドが UNLOAD です。「UNLOAD TABLE {スキーマ名.}テーブル名」の構文で実行します。
出力フォーマットは SET LOADFORMAT コマンドで指定します。デフォルトではカンマ区切り、ダブル・クォーテーションで囲まれた CSV ファイルです。出力ディレクトリはカレント・ディレクトリ(CD コマンドで変更可能)ですが、UNLOAD コマンドでも指定できます。出力ファイル名は自動的に決定されます。DIRECTORY 句で行末にセミコロン(;)を入力するとセミコロンまでディレクトリ名と認識します。

UNLOAD コマンド
SQL> UNLOAD TABLE data1 DIRECTORY /tmp
format csv

column_names on
delimiter ,
enclosure_left "
enclosure_right "
encoding UTF8
row_terminator default

** UNLOAD Start ** at 2021.07.06-21.04.23
Export Separate Files to /tmp
DATA TABLE DATA1
File Name: /tmp/DATA1_DATA_TABLE.csv
Number of Rows Exported: 2
** UNLOAD End ** at 2021.07.06-21.04.23

UNLOAD コマンドは OCI クラウド・ストレージに対しても実行することができますが、ここでは触れません。

REPEAT

直前に実行した SQL 文を繰り返し実行したい場合があります。REPEAT コマンドは実行回数と間隔(秒)を指定することで SQL 文を繰り返し実行できます。構文は「REPEAT 回数 間隔」です。間隔の最大値は 120 秒、繰り返し回数の最大値は 2,147,483,647 です。

REPEAT コマンド
SQL> SELECT SYSTIMESTAMP FROM DUAL;
                         SYSTIMESTAMP
-------------------------------------
21-07-06 14:52:00.523906000 +09:00
SQL> REPEAT 10 1
Running 10 of 10  @ 2:52:32.571 with a delay of 1s

                         SYSTIMESTAMP
-------------------------------------
21-07-06 14:52:32.572359000 +09:00

秒数は小数点以下の値も指定できます。このため1秒未満の間隔でもSQL文を実行できます。

SET

SET HIGHLIGHTING

 端末入出力の文字列のうちキーワードの色を自動的に変更する機能です。色を変更する対象、文字色(FOREGROUND)、背景色(BACKGROUND)を変更できます。SET HIGHLIGHTING ON コマンドを実行すると、デフォルトでは SQL キーワードがシアンに変更されます。

SET HIGHLIGHTING ON

色の変更を行うには「SET HIGHLIGHTING 対象 タイプ 設定」コマンドを実行します。対象に指定できるのは以下の通りです。

対象 説明 デフォルト
DEFAULT 標準色
COMMENT コメント
STRING 文字列
NUMBER 数値
PUNCTUATION 句読点
KEYWORD SQLキーワード foreground: cyan
IDENTIFIER 識別子

タイプに指定できる項目は以下の通りです。

タイプ 説明 設定値
FOREGROUND 文字色 RED, BLUE, BLACK, CYAN, GREEN, MAGENTA, WHITE
BACKGROUND 背景色 RED, BLUE, BLACK, CYAN, GREEN, MAGENTA, WHITE
BOLD 強調 ON, OFF, RESET
ULINE アンダーライン ON, OFF, RESET
INVERSE 反転 ON, OFF, RESET

キーワードを赤に変更する場合には以下のコマンドを実行します。

SET HIGHLIGHTING コマンド
SQL> SET HIGHLIGHTING KEYWORD FOREGROUND RED

SET AUTOTRACE

自動トーレス機能(AUTOTRACE)は SQL*Plus にも存在しますが、出力される統計情報が増えています。

SET AUTOTRACE コマンド
SQL> SET AUTOTRACE ON
Autotrace Enabled
Shows the execution plan as well as statistics of the statement.
SQL> 
SQL> SELECT * FROM scott.data1 WHERE c1=100;

    C1       C2
------ --------
   100 data1

                                                                   PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------
SQL_ID  7xw9uvjxbzxxg, child number 0                                           
-------------------------------------                                           
SELECT * FROM scott.data1 WHERE c1=100                                          
                                                                                
Plan hash value: 436265523                                                      
                                                                                
------------------------------------------------------------                    
| Id  | Operation                   | Name        | E-Rows |                    
------------------------------------------------------------                    
|   0 | SELECT STATEMENT            |             |        |                    
|   1 |  TABLE ACCESS BY INDEX ROWID| DATA1       |      1 |                    
|*  2 |   INDEX UNIQUE SCAN         | SYS_C007705 |      1 |                    
------------------------------------------------------------                    
                                                                                
Predicate Information (identified by operation id):                             
---------------------------------------------------                             
                                                                                
   2 - access("C1"=100)                                                         
                                                                                
Note                                                                            
-----                                                                           
   - Warning: basic plan statistics not available. These are only collected when:
       * hint 'gather_plan_statistics' is used for the statement or             
       * parameter 'statistics_level' is set to 'ALL', at session or system level

Statistics
-----------------------------------------------------------
               1  CPU used by this session
               1  CPU used when call started
              43  Requests to/from client
              43  SQL*Net roundtrips to/from client
              59  buffer is not pinned count
            1293  bytes received via SQL*Net from client
           89680  bytes sent via SQL*Net to client
              32  calls to get snapshot scn: kcmgss
               3  calls to kcmgcs
               6  cluster key scan block gets
               5  cluster key scans
              67  consistent gets
              32  consistent gets examination
              32  consistent gets examination (fastpath)
              67  consistent gets from cache
              35  consistent gets pin
              35  consistent gets pin (fastpath)
            6932  cumulative DB time in requests
               5  enqueue releases
               5  enqueue requests
              23  execute count
              12  index fetch by key
              11  index range scans
          548864  logical read bytes from cache
              33  no work - consistent read gets
              58  non-idle wait count
              23  opened cursors cumulative
               1  opened cursors current
               7  parse count (hard)
               8  parse count (total)
               1  parse time cpu
               1  process last non-idle time
              80  recursive calls
               1  recursive cpu usage
               5  rows fetched via callback
               3  session cursor cache count
              17  session cursor cache hits
              67  session logical reads
               6  sorts (memory)
            2014  sorts (rows)
              13  table fetch by rowid
               7  table scan blocks gotten
             401  table scan disk non-IMC rows gotten
             401  table scan rows gotten
               1  table scans (short tables)
              44  user calls
SQL>
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?