db2cli 実行可能ファイルは、データベース・インスタンス所有者のホーム・ディレクトリーの sqllib/samples/cli/ DB2® インストール・サブディレクトリーにあります。 IBM® Data Server Driver for ODBC and CLI の場合、db2cli 実行可能ファイルは clidriver/bin インストール・ディレクトリーにあります。
このトピックでは、db2cli コマンドの pureQuery 機能についてのみ説明します。 pureQuery 機能は、このコマンドの execsql オプションによって提供されます。 CLI コマンドについては、DB2 for Linux, UNIX and Windows インフォメーション・センターの『db2cli - DB2 対話機能 CLI コマンド』を参照してください。
なし
なし
>>-db2cli--+-| execsql-mode |-+-------------------------------->< '- -help-----------' execsql-mode (1) .- -execute-----. |--execsql------+---------------+--+-| server-option |-+--------> '- -prepareonly-' '- -connstring--str-' >-- -inputsql--sqlfile--+-----------------------+---------------> '- -outfile--outputfile-' >--+---------------------------------+--------------------------> '- -statementdelimiter--delimiter-' >--+----------------------------------+-------------------------> '- -commentstart--commentindicator-' >--+---------------------------+--------------------------------> '- -cursorhold--holdability-' >--+----------------------------------+-------------------------> '- -cursorconcurrency--concurrency-' >--+--------------------+--+--------+---------------------------| '- -cursortype--type-' '- -help-' server-option |-- -dsn--dsn_name--+--------------------+----------------------| '-| authentication |-' authentication |-- -user-- -username--+--------------------+-------------------| '- -passwd -password-'
図で使用されている規則については、『構文図の読み方』を参照してください。
出力をファイルに保存するためには、そのファイルの絶対パスまたは相対パスを指定して -output オプションを使用してください。
コメントが複数行にまたがる場合には、各行の開始位置に commentindicator 文字を組み合わせてください。
コメント行の最大長は 128 文字です。
入力 SQL ファイルでは、コメント文字の後のテキストにステートメントのカーソル属性を含めることができます。 カーソル属性は、コメントの直後の SQL ステートメントに適用されます。
デフォルト値は yes です。
このファイル内の SQL ステートメントは、区切り文字によって区切られます。 組み込まれるステートメントは、準備可能なステートメントのみです。
入力 SQL ファイルには、SQL ステートメントおよびコメントのみを含めることができます。このファイルには、CLI 固有の属性またはキーワードを含めることはできません。
入力 SQL ファイルに含めることができるのは、準備可能な SQL ステートメントのみです。 DESCRIBE TABLE や BIND などの DB2 コマンドは使用できません。
このファイル内のいずれかの SQL ステートメントが入力ファイル内の DDL に依存している場合、db2cli execsql -prepareonly コマンドを実行する前にすべての DDL ステートメントを実行する必要があります。
db2cli execsql コマンドで -execute オプションを使用するときには、SQL ステートメントでパラメーター・マーカーを使用することはできません。
バッチ SQL ステートメントはサポートされていません。
SQL ステートメントは DB2 CLI API SQLExecDirect() で実行されます。 db2cli コマンドは、データベースで SQL ステートメントを実行するときに、そのデータベースに関して設定された db2cli.ini または db2dsdriver.cfg で指定された設定値を認識します。
db2cli コマンドによって返されるエラー・メッセージ・ストリングは、CLI エラー処理 API SQLError() または SQLGetDiagRec() によって返されるストリングです。
create table employee(empid integer, empname varchar(100) CREATE PROCEDURE proc1 ( ) DYNAMIC RESULT SETS 1 P1: BEGIN DECLARE cursor1 CURSOR WITH RETURN FOR SELECT * FROM fprem; OPEN cursor1; END P1 CREATE PROCEDURE PROC2(IN ID1 INTEGER,OUT NAME VARCHAR(20)) BEGIN DECLARE CUR1 CURSOR WITH RETURN TO CALLER FOR SELECT * FROM EMPLOYEE1 WHERE ID=ID1; OPEN CUR1; END
この例では、SQL ファイル test.sql に次のテキストが含まれていることも前提としています。
--Populate table( employee ) insert into employee(empid, empname) values(1, 'Adam') insert into employee(empid, empname) values(2, 'Atul') select empid, empname from employee --Execute the stored procedure Call proc1( )
ファイル内の SQL ステートメントを実行するために、コンソール・ウィンドウで次の db2cli コマンドを入力します。
db2cli execsql –dsn sample –inputsql test.sql
コンソール・ウィンドウに次のテキストが表示されます。
IBM DATABASE 2 Interactive CLI Sample Program (C) COPYRIGHT International Business Machines Corp. 1993,1996 All Rights Reserved Licensed Materials - Property of IBM insert into employee(empid, empname) values(1, 'Adam') SQL コマンドの処理が正常に完了しました。 insert into employee(empid, empname) values(2, 'Atul') SQL コマンドの処理が正常に完了しました。 select empid, empname from employee EMPID EMPNAME 1, Adam 2, Atul Call proc1() EMPID EMPNAME 1, Adam 2, Atul
OUT 引数が指定されたストアード・プロシージャーに関する CALL ステートメントを実行することができます。 OUT パラメーターとして疑問符 (?) を使用することができます。
以下の例では、SQL スクリプト・ファイル test2.sql に次のテキストが含まれていることを前提としています。
CALL PROC2( 1, ?)
ファイル内の SQL ステートメントを実行するために、コンソール・ウィンドウで次の db2cli コマンドを入力します。
db2cli execsql –dsn sample –inputsql test2.sql
コンソール・ウィンドウに次のテキストが表示されます。
Value of output parameters -------------------------- Parameter Name : NAME Parameter Value : - ID ----------- 1
SQL ステートメントを準備するのみで実行しない場合には、-prepareonly オプションを指定してください。 -prepareonly オプションを指定して db2cli execsql コマンドを実行する前に、SQL ステートメントのために必要な DDL ステートメントを実行する必要があります。
以下の例では、SQL ファイル test3.sql に次のテキストが含まれていることを前提としています。
--populate table( employee ) insert into employee(empid, empname) values(1, 'Adam'); insert into employee(empid, empname) values(2, 'Atul'); select empid, empname from employee;
データベース内に EMPLOYEE 表が作成されていることも前提となっています。
このファイル内の SQL ステートメントを準備するために、コンソール・ウィンドウで次の db2cli コマンドを入力します。
db2cli execsql –prepareonly –dsn sample –inputsql test3.sql
コンソール・ウィンドウに次のテキストが表示されます。
IBM DATABASE 2 Interactive CLI Sample Program (C) COPYRIGHT International Business Machines Corp. 1993,1996 All Rights Reserved Licensed Materials - Property of IBM insert into employee(empid, empname) values(1, 'Adam') The SQL command prepared successfully. insert into employee(empid, empname) values(2, 'Atul') The SQL command prepared successfully. select empid, empname from employee The SQL command prepared successfully.
DML ステートメントのために必要な DDL ステートメントを同じファイルに配置すると、それらの DDL ステートメントを必要とする DML ステートメントは失敗します。 例えば、test4.sql ファイルに次のテキストが含まれていて、EMPLOYEE 表がデータベース内に作成されていないとします。
--create and populate table( employee ) create table employee(empid integer, empname varchar(100)); insert into employee(empid, empname) values(1, 'Adam'); insert into employee(empid, empname) values(2, 'Atul'); select empid, empname from employee; -- try to create another table with the same name create table employee(empid integer, empname varchar(100));
INSERT ステートメントと SELECT ステートメントを正常に実行するためには、その前に CREATE TABLE ステートメントを実行する必要があります。
このファイル内の SQL ステートメントを準備するために、コンソール・ウィンドウで次の db2cli コマンドを入力します。
db2cli execsql –prepareonly –dsn sample –inputsql test4.sql
コンソール・ウィンドウに次のテキストが表示されます。
IBM DATABASE 2 Interactive CLI Sample Program (C) COPYRIGHT International Business Machines Corp. 1993,1996 All Rights Reserved Licensed Materials - Property of IBM create table employee(empid integer, empname varchar(100)) The SQL command prepared successfully. insert into employee(empid, empname) values(1, 'Adam') The SQL command failed. During SQL processing it returned: [IBM][CLI Driver][DB2/6000] SQL0204N "EMPLOYEE" is an undefined name. SQLSTATE=42704 insert into employee(empid, empname) values(2, 'Atul') The SQL command failed. During SQL processing it returned: [IBM][CLI Driver][DB2/6000] SQL0204N "EMPLOYEE" is an undefined name. SQLSTATE=42704 select empid, empname from employee The SQL command failed. During SQL processing it returned: [IBM][CLI Driver][DB2/6000] SQL0204N "EMPLOYEE" is an undefined name. SQLSTATE=42704 create table employee(empid integer, empname varchar(100)) The SQL command prepared successfully.
この例では、2 つの CREATE SQL ステートメントは正常に準備されましたが、EMPLOYEE 表はデータベース内に作成されていません。 EMPLOYEE 表がデータベース内にないため、INSERT ステートメントと SELECT ステートメントは正常に準備されませんでした。