The following stored procedure includes a simple CASE statement.
CREATE PROCEDURE spSample(IN pANo INTEGER, IN pName CHARACTER(30), OUT pStatus CHARACTER(50)) BEGIN DECLARE vNoOfAccts INTEGER DEFAULT 0; SELECT COUNT(*) INTO vNoOfAccts FROM Accounts; CASE vNoOfAccts WHEN 0 THEN INSERT INTO Accounts (pANo, pName); WHEN 1 THEN UPDATE Accounts SET aName = pName WHERE aNo = pANo; ELSE SET pStatus = 'Total ' || vNoOfAccts || 'customer accounts'; END CASE; END;
In the preceding example, the appropriate SET statement of a WHEN clause is executed depending on the value of the local vNoAccts.
IF the value of vNoAccts is … | THEN it matches … | AND this statement is executed … |
---|---|---|
0 | the first WHEN clause | INSERT INTO Accounts (pANo, pName); |
1 | the second WHEN clause | UPDATE Accounts SET aName = pName WHERE aNo = pANo; |
any other number | the ELSE clause | SET pStatus = 'Total ' || vNoAccts || ' customer accounts'; |