SQL/MX Programming Manual for C and COBOL (G06.24+, H06.03+)
Simple and Compound Statements
HP NonStop SQL/MX Programming Manual for C and COBOL—523627-004
5-16
IF Statement
Example
This example retrieves order information from the database and then updates the 
quantity on hand for that part number. You can use the SET statement as an 
alternative to the SELECT INTO statement:
EXEC SQL BEGIN;
 SET :unit_price,:qty_ordered = SELECT UNIT_PRICE, QTY_ORDERED 
 FROM ODETAIL
 WHERE ORDERNUM = :ordernum AND PARTNUM = :partnum;
 UPDATE PARTLOC 
 SET QTY_ON_HAND = QTY_ON_HAND - :qty_ordered
 WHERE PARTNUM = :partnum AND LOC_CODE = :loc_code;
END; 
EXEC SQL BEGIN 
 SET :unit-price, :qty-ordered = SELECT UNIT_PRICE, QTY_ORDERED 
 FROM ODETAIL 
 WHERE ORDERNUM = :ordernum AND PARTNUM = :partnum;
 UPDATE PARTLOC 
 SET QTY_ON_HAND = QTY_ON_HAND - :qty-ordered 
 WHERE PARTNUM = :partnum AND LOC_CODE = :loc-code; 
 END END-EXEC.
IF Statement
An IF statement provides branching inside compound statements. An IF statement is a 
compound statement that provides conditional execution based on the truth value of a 
conditional expression. Use this general syntax: 
For the complete syntax and semantics, see the IF statement in the SQL/MX 
Reference Manual. 
Example
In this example, INSERT and SELECT statements execute sequentially only for new 
orders. Otherwise, the SELECT statement returns information on the current customer: 
...
EXEC SQL 
BEGIN
IF :hv_new_ordernum <> 0 
THEN 
 INSERT INTO SALES.ORDERS 
IF conditional-expression THEN 
 SQL-statement;[SQL-statement;]...
 [ELSEIF conditional-expression THEN 
 SQL-statement;[SQL-statement;]...]...
 [ELSE SQL-statement;[SQL-statement;]...]
END IF 
C
COBOL
C










