パラメータ
パラメータを使用すると、SQL文の処理に使用される値をバインドできます。これらのパラメータは、?を使用するか、SQL文内で:IDENTIFIERを使用してマークされます。
ttIsqlは、SQL文のテキストを準備します。次に、パラメータ名に一致する変数を検索します。一致する変数がないパラメータの場合、ttIsqlは値の入力を求めます。?を使用する場合のパラメータ名はQMARK_Nで、Nは1で始まり、文で?が検出されるたびに増分されます。たとえば、QMARK_1、QMARK_2などです。:PARAMの名前はPARAMです。
動的パラメータによって、個別の行で各パラメータへの入力が求められます。パラメータの値は、SQLで1つのリテラルを指定するのと同じ方法で指定します。
パラメータ値はセミコロン文字で終了する必要があります。
指定可能な値の種類は次のとおりです。
-
数値リテラル。たとえば、
1234.5です -
一重引用符で囲まれた時刻リテラル、日付リテラルまたはタイムスタンプ・リテラル。例:
'12:30:00' '2000-10-29' '2000-10-29 12:30:00'
-
先頭が
「N」で始まる、一重引用符で囲まれたUnicode文字列リテラル。たとえば、N'abc'です -
NULL値。たとえば、
NULLです -
パラメータ入力処理が停止されることを示す「*」文字。たとえば、
*です -
パラメータ入力ヘルプ情報を出力する「?」文字。たとえば、
?です
この例は、
AおよびBが変数ではなく動的パラメータであることを示しています。
Command> select * from dual where :a > 100 and :b < 100;
Type '?' for help on entering parameter values.
Type '*' to end prompting and abort the command.
Type '-' to leave the parameter unbound.
Type '/;' to leave the remaining parameters unbound and execute the command.
Enter Parameter 1 'A' (NUMBER) > 110
Enter Parameter 2 'B' (NUMBER) > 99
< X >
1 row found.
Command> print;printコマンドでは、動的パラメータであるため結果は表示されません。
この例では、
:aおよび:bが変数として使用され、パラメータとして使用されるが動的ではないことを示しています。
Command> var a number;
Command> exec :a := 110;
PL/SQL procedure successfully completed.
Command> print a;
A : 110
Command> var b number;
Command> exec :b := 99;
PL/SQL procedure successfully completed.
Command> select * from dual where :a > 100 and :b < 100;
< X >
1 row found.
Command> print;
A : 110
B : 99この例では、
:aと:bの両方が変数であり、結合可能な動的パラメータ:cがあることを示しています。
Command> var a number;
Command> exec :a := 110;
Command> var b number;
Command> exec :b := 99;
Command> select * from dual where :a > 100 and :b < 100 and :c > 0;
Enter Parameter 3 'C' (NUMBER) > 1
< X >
1 row found.
Command> print;
A : 110
B : 99この例では、
_QMARK_1が宣言されていないため、ttIsqlはパラメータ値の入力を求めます。:aパラメータには、値が110のA変数が入力されます。:bには、値が99のB変数が入力されます。これらの値はエンジンに送信され、エンジンによって文が処理され、値abc11099が返されます。Command> select cast(? as varchar2(10)) || :a || :b from dual;
Type '?' for help on entering parameter values.
Type '*' to end prompting and abort the command.
Type '-' to leave the parameter unbound.
Type '/;' to leave the remaining parameters unbound and execute the command.
Enter Parameter 1 '_QMARK_1' (VARCHAR2) > 'abc';
< abc11099 >
1 row found.