埋め込み SQL 文でホスト変数を使用するには、その前にホスト変数を宣言する必要があります。
埋め込み SQL 文からホスト変数を参照します。埋め込み SQL 文内でホスト変数名を記述する場合は、先頭にコロン (:) を付け、コンパイラがホスト変数と同じ名前のテーブルまたはカラムを区別できるようにします。
EXEC SQL
BEGIN DECLARE SECTION
END-EXEC
01 id pic x(4).
01 name pic x(30).
01 book-title pic x(40).
01 book-id pic x(5).
EXEC SQL
END DECLARE SECTION
END-EXEC
. . .
display "Type your identification number: "
accept id.
* The following statement retrieves the name of the
* employee whose ID is the same as the contents of
* the host variable "id". The name is returned in
* the host variable "name".
EXEC SQL
SELECT emp_name INTO :name FROM employees
WHERE emp_id=:id
END-EXEC
display "Hello " name.
* In the following statement, :book-id is an input
* host variable that contains the ID of the book to
* search for, while :book-title is an output host
* variable that returns the result of the search.
EXEC SQL
SELECT title INTO :book-title FROM titles
WHERE title_id=:book-id
END-EXEC