SQL/MX Programming Manual for C and COBOL (G06.24+, H06.03+)
COBOL Sample Programs
HP NonStop SQL/MX Programming Manual for C and COBOL—523627-004
C-7
Using Argument Lists in Dynamic SQL
* Move statement with input variable to statement variable.
MOVE "SELECT empnum, first_name, last_name, salary"
& " FROM samdbcat.persnl.employee"
& " WHERE empnum = CAST(? AS NUMERIC(4) UNSIGNED)"
TO hv-sql-stmt.
* Prepare the statement.
EXEC SQL PREPARE sqlstmt FROM :hv-sql-stmt END-EXEC.
* Initialize the input parameter in the WHERE clause.
DISPLAY "Enter the employee number to be retrieved: ".
ACCEPT in-empnum.
* Execute the prepared statement using the argument lists.
EXEC SQL EXECUTE sqlstmt
USING :in-empnum
INTO :hv-empnum, :hv-first-name, :hv-last-name,
:hv-salary INDICATOR :hv-salary-i END-EXEC.
* Process the output values.
IF sqlstate = sqlstate-ok
DISPLAY "Empnum is: " hv-empnum
DISPLAY "First name is: " hv-first-name
DISPLAY "Last name is: " hv-last-name
IF hv-salary-i < 0
DISPLAY "Salary is unknown"
ELSE
DIVIDE 100.0 INTO hv-salary GIVING hv-temp
DISPLAY "Salary is: " hv-temp
ELSE IF sqlstate = sqlstate-nodata
DISPLAY "No row with employee number: " in-empnum.
* Deallocate the prepared statement.
EXEC SQL DEALLOCATE PREPARE sqlstmt END-EXEC.
STOP RUN.
Example C-3. Using Argument Lists in Dynamic SQL (page 2 of 3)