DB2函数错误

时间:2014-07-15 12:34:36

标签: function db2

以下DB2函数有什么问题?

CREATE FUNCTION MIGRATION.determineToTaxService
(DIGI_NAAR_VLG CHAR, DIGI_VAN_VLG CHAR, NAARVLG CHAR)
RETURNS CHAR
BEGIN ATOMIC
    DECLARE returnValue CHAR;
    SET returnValue = '0';

    IF (DIGI_NAAR_VLG = '1') THEN
        SET returnValue = '1';

    ELSEIF (DIGI_VAN_VLG = '1') THEN
        SET returnValue = '1';

    ELSEIF (NAARVLG = '1') THEN
        SET returnValue = '1';
    END IF;

    RETURN returnValue;
END;

我收到了这个错误:

An unexpected token "CHAR" was found following " DECLARE returnValue".  Expected tokens may   include:  "END-OF-STATEMENT". SQL Code: -104, SQL State: 42601
Error occured in:
CREATE FUNCTION MIGRATION.determineToTaxService
   (DIGI_NAAR_VLG CHAR, DIGI_VAN_VLG CHAR,
    NAARVLG CHAR)
RETURNS CHAR
BEGIN ATOMIC
    DECLARE returnValue CHAR

我无法弄明白。如果我删除if和elsif语句,它也没有运行,所以问题不应该存在。

2 个答案:

答案 0 :(得分:1)

终止字符错误:

CREATE FUNCTION MIGRATION.determineToTaxService
(DIGI_NAAR_VLG CHAR, DIGI_VAN_VLG CHAR, NAARVLG CHAR)
RETURNS CHAR
BEGIN ATOMIC
    DECLARE returnValue CHAR;
    SET returnValue = '0';

    IF (DIGI_NAAR_VLG = '1') THEN
        SET returnValue = '1';

    ELSEIF (DIGI_VAN_VLG = '1') THEN
        SET returnValue = '1';

    ELSEIF (NAARVLG = '1') THEN
        SET returnValue = '1';
    END IF;

    RETURN returnValue;
END @

您可以从命令行调用它,如:

db2 -td@ -vf test

答案 1 :(得分:1)

一个技巧是用以下注释结束这些行:

CREATE FUNCTION MIGRATION.determineToTaxService
    (DIGI_NAAR_VLG CHAR, DIGI_VAN_VLG CHAR, NAARVLG CHAR)
RETURNS CHAR
BEGIN ATOMIC
    DECLARE returnValue CHAR; --
    SET returnValue = '0'; --

    IF (DIGI_NAAR_VLG = '1') THEN
        SET returnValue = '1'; --

    ELSEIF (DIGI_VAN_VLG = '1') THEN
        SET returnValue = '1'; --

    ELSEIF (NAARVLG = '1') THEN
        SET returnValue = '1'; --
    END IF; --

    RETURN returnValue; --
END ;

然后你可以继续使用;作为陈述终结者

您可能希望简化功能,例如:

CREATE FUNCTION MIGRATION.determineToTaxService
    (DIGI_NAAR_VLG CHAR, DIGI_VAN_VLG CHAR, NAARVLG CHAR)
RETURNS CHAR
RETURN
    CASE WHEN DIGI_NAAR_VLG = '1' THEN '1'
         WHEN DIGI_VAN_VLG = '1' THEN '1'
         WHEN NAARVLG = '1' THEN '1'
         ELSE '0'
    END

应该