DB2 Create function (i)

Source: Internet
Author: User
Tags db2 create function string format

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 CREATE FUNCTION "FAS"."GET_ALL_NAME" ( "A_ID" BIGINT )   RETURNS VARCHAR(9000)   SPECIFIC "SQL131107190046233"--SPECIFIC名称不能与其他FUNCTION的重复   LANGUAGE SQL   NOT DETERMINISTIC   EXTERNAL ACTION   READS SQL DATA   CALLED ON NULL INPUT   INHERIT ISOLATION LEVEL WITHOUT LOCK REQUEST   INHERIT SPECIAL REGISTERS BEGIN ATOMIC   --声明变量   DECLARE A_CURR_ID BIGINT DEFAULT 0 ;   DECLARE A_CURR_NAME VARCHAR(50) DEFAULT ‘‘ ;   DECLARE SRETURN VARCHAR(1000) DEFAULT ‘‘ ;   --设置变量值   SET A_CURR_NAME= (SELECT NAME FROM FA_DEPARTMENT WHERE ID=A_ID);--当前名称   SET A_CURR_ID= (SELECT PARENT_ID FROM FA_DEPARTMENT WHERE ID=A_ID);--当前名称   SET SRETURN=‘_‘||A_CURR_NAME;   --WHILE循环   WHILE A_CURR_ID>0 DO   SET A_CURR_NAME= (SELECT NAME FROM FA_DEPARTMENT WHERE ID=A_CURR_ID);--当前名称   SET A_CURR_ID= (SELECT PARENT_ID FROM FA_DEPARTMENT WHERE ID=A_CURR_ID);--当前名称    SET SRETURN=‘_‘||A_CURR_NAME||SRETURN;   END WHILE;   --返回值   RETURN SRETURN; END;

The example passes in the parameter a_id and queries the relevant data by A_ID. and returns the result in string format.

This method can be called in an SQL statement. As follows

?
1 select GET_ALL_NAME(21) fromSYSIBM.SYSDUMMY1

The

can be used as a column field value for a SQL query statement, or in the WHERE clause

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.