DB2 CASE/IF condition control statement

Source: Internet
Author: User
Tags db2 case

IF statement format (only used in DB2 processes, triggers, and functions)
IF (condition)
THEN statement;
ELSEIF (condition)
THEN statement;
ELSEIF (condition)
THEN statement;
......
ELSE
Statement;
End if;
 
Example:
[SQL]
BEGIN
-- Retrieve the maximum ID
SET NEW_ID = NULL;
-- (Select top 1 my_id from tempvoucherno order by my_id DESC)
SET NEW_ID = (SELECT my_id FROM "PIGGY". tempvoucherno fetch first 1 rows only );
-- If not
IF NEW_ID IS NULL
Then set NEW_ID = '123 ';
ELSE
BEGIN
Declare num varchar (7 );
-- Retrieve the number + 1 CONVERT (VARCHAR, (CONVERT (INTEGER, RIGHT (NEW_ID, 7) + 1 ));
-- CAST (RIGHT (NEW_ID, 7) as INTEGER) + 1) as VARCHAR (7 ));
-- Set num = CONVERT (VARCHAR (7), (CONVERT (INTEGER, RIGHT (NEW_ID, 7) + 1 ));

-- Set num = REPLICATE ('0', 7-LEN (NUM) + NUM;

SET NEW_ID = NUM;
END;
End if;
Insert into "PIGGY". TEMPVOUCHERNO (my_id) values (NEW_ID );
 
END


Author cuiran

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.