PreviousSQL Engine Reference (v9 SP2 (9.5) revision 1) Next

IF

Chapter contents

Remarks

IF statements provide conditional execution based on the value of a condition. The IF . . . THEN . . . ELSE . . . END IF construct controls flow based on which of two statement blocks will be executed. You may also use the IF . . . ELSE syntax.

You may use IF statements in the body of both stored procedures and triggers.

There is no limit to the number of nested IF statements allowed, although the query remains subject to the usual total length limitation and other applicable limitations.


Note
You cannot use a mixed syntax containing Pervasive PSQL and T.SQL. You may use either the IF...THEN...ELSE...END IF syntax or the IF...ELSE syntax. If you are using multiple statements with IF or ELSE conditions, you must use BEGIN and END to indicate the beginning and ending of the statement blocks.

Syntax

IF ( Boolean_condition ) 
BEGIN 
Sql-statements 
END 
ELSE
BEGIN 
Sql-statements 
END

Examples

The following example uses the IF statement to set the variable Negative to either 1 or 0, depending on whether the value of vInteger is positive or negative.

IF (:vInteger < 0) THEN 
SET :Negative = '1'; 
ELSE 
SET :Negative = '0'; 
END IF; 


The following example uses the IF statement to test the loop for a defined condition (SQLSTATE = '02000'). If it meets this condition, then the WHILE loop is terminated.

FETCH_LOOP: 
WHILE (:counter < :NumRooms) DO 
FETCH NEXT FROM cRooms into :CurrentCapacity; 
IF (SQLSTATE = '02000') THEN 
LEAVE FETCH_LOOP; 
END IF; 
SET :counter = :counter + 1; 
SET :TotalCapacity = :TotalCapacity + 
:CurrentCapacity; 
END WHILE; 


IF(:vInteger >50) 
BEGIN 
set :vInteger = :vInteger + 1;  
INSERT INTO test VALUES('Test'); 
END; 
ELSE
set :vInteger = :vInteger - 1; 

See Also

CREATE PROCEDURE

CREATE TRIGGER


Chapter contents
Book contents

Prev topic: HAVING
Next topic: IN