2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

ASE SybaseのコマンドラインツールiSQLの基本的な使い方とよく使うSQL/コマンド

Last updated at Posted at 2022-09-13

iSQLの基本的な使い方

ログイン

isql -S <サーバ名> -U <ユーザ名> -X

  • 大体これでOKだが、他にも多くのオプションがある。
  • 他のオプションは iSQL起動オプション を参照。

SQL/コマンドの実行

go

  • SQL/コマンドを実行する際にはコマンド終端文字の入力が必要。
  • デフォルトでは「go」だがiSQLの起動オプション -c でコマンド終端文字を変更可能。
  • 複数のSQLを記述して最後にgoで実行するとまとめて実行される。
  • SQLだけでなくsp_*** のストアドプロシージャ実行時も必要。
select getdate()
go

データベースの切替

use DBNAME
ASE Sybaseのデータベース構成はSQL Serverと同じような構成になっており、操作対象のデータベースを切り替えながら操作を行う。

OSコマンドの実行

!!oscommand
実行したいOSコマンドの先頭に「!!」をつける

1> !!hostname
[sh:hostname]
testserver

入力内容を消去する

reset
入力を間違えた時にresetコマンドで入力バッファのクリアが可能。

1> select * From sysobjects
2> where type = 'U
3>
4> reset
1>

終了

quit
または
exit

iSQL起動オプション

主なオプションは以下の通り。詳細は公式マニュアルを参照のこと。

分類 オプション 説明
接続先指定 -S server_name 接続先の SAP ASE サーバの名前を指定します。
-D database isql セッションが開始されるデータベースを選択します
-U username ログイン名を指定します。ログイン名は大文字と小文字が区別されます。
-P password SAP ASE パスワードを指定します。 -P フラグを指定しない場合、isql はパスワードを求めるプロンプトを出します。
-X パスワードの暗号化を行います。サーバ側の接続設定でパスワード暗号化を指定している場合はこのオプションを使用しなければ接続エラーになります。詳細は備考1参照。
表示設定 -h header 列名を出力する間隔を行数で指定します。デフォルトでは結果セットごとに1回だけ出力されます。
-b 列ヘッダー出力を無効にします。
-e echoes input.
-s col_separator 列区切り文字を指定します。デフォルトでは空白。特殊文字 (「|」「;」「&」「<」「>」など) を使用する場合は引用符で囲むか前にバックスラッシュを付けます。列セパレータは、各行の各列の最初と最後に表示されます。
-w column-width 出力の画面幅を設定します。デフォルトは 80 文字です。出力行が最大画面幅に達すると、複数の行に分割されます。
ファイル指定 -i inputfile isqlで実行するSQLコマンドを記述したOSファイルの名前を指定します。ファイルにはコマンドターミネータが含まれている必要があります (デフォルトは「go」)。このオプションは「isql < inputfile」と同義です。
-o outputfile isql からの出力を格納するOSファイルの名前を指定します。このオプションは「isql > outputfile」と同義です。
動作制御 -c cmdend コマンド終端文字を変更します。デフォルトは「go」です。SQL予約文字と制御文字は使用できません。
--retserverror 重大度が 10 を超えるサーバ エラーが発生した場合isql は強制的に終了し、失敗コードを返します。isql が異常終了を検出するとSAP ASE エラー番号とともにラベル「Msg」を stderr に書き込み、呼び出し元に終了コード 2 を返します。 isql は、完全なサーバー エラー メッセージを stdout に出力します。
情報表示 -v isql のバージョンと著作権メッセージを出力して終了します。
--help 使用可能な引数のリストで構成される isql ユーティリティの構文と使用法の簡単な説明を表示します。

iSQL対話型コマンド

iSQLログイン後に使用出来るコマンド一覧

Command Description
:r filename Reads an operating system file into the command buffer.
Do not include the command terminator in the file; once you have finished editing, enter the terminator interactively on a line by itself.
:R filename Reads an operating system file into the command buffer then shows the command.
Do not include the command terminator in the file; once you have finished editing, enter the terminator interactively on a line by itself.
use database_name Changes the current database.
!! os_command Executes an operating system command. Place at the start of a line.
> file_name Redirects the output of the Transact-SQL command to file_name. This example inserts the server version into :
 select @@version
 go > file_name
>> file_name Appends the output of the Transact-SQL command to file_name. This example appends the server version to file_name:
 select @@version
 go >> file_name
| command Pipes the output of the Transact-SQL command to an external command. This example finds all instances of “sa” in the listing produced by sp_who:
 sp_who
 go | grep sa
vi (UNIX) or edit (Windows) Calls the default editor.
reset Clears the query buffer.
quit または exit Exits isql.

SETコマンド

あまり使わないかもだがTransact-SQLと同じような感じでSETコマンドが利用出来るようである。
SAP Adaptive Server Enterprise | Reference Manual: Commands - set

SET NOCOUNT {ON|OFF}
SQL実行結果の最後に表示される「1 row affected」のメッセージを表示させない。
Oracleの「SET FEEDBACK {ON|OFF}」と同じようなもの。

よく使うSQL/コマンド

ログイン先環境確認

取得項目 SQL
日付 select getdate()
DBバージョン select @@version
サーバ名 select @@servername
データベース名 select db_name()

テーブル一覧

sp_help
または
Select sysobjects.name From sysobjects where type = 'U' Order By sysobjects.name

テーブル情報詳細

sp_help TABLENAME

参考ページ

SAP Adaptive Server Enterprise | Utility Guide - isql
SAP Adaptive Server Enterprise | Reference Manual: Commands - set
UJP | isql for Sybaseを使ってみる
Sybase ASE 入門 isql 対話型 Utility


備考

  1. パスワード暗号化が設定されているサーバに -X をつけずに接続した場合は下記のようなエラーメッセージが表示され接続に失敗する。

    
    Msg 1640, Level 16, State 1:
    Adaptive Server requires encryption of the login password on the network.
    Msg 4002, Level 14, State 1:
    Login failed.
    CT-LIBRARY error:
    ct_connect(): protocol specific layer: external error: The attempt to connect to the server failed.

    サーバ側の接続設定の暗号化が設定されているかどうかは下記コマンドで確認可能。
    sp_configure 'net password encryption reqd'
    詳細は下記のSAP Note参照。
    1942379 - How to setup password encryption between isql and ASE - SAP ASE
2
0
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
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?