问题
What's wrong with the following DB2 function?
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;
I'm getting this error:
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
I can't really figure it out. It's also not running if I remove the if and elsif statements so the problem shouldn't be there.
回答1:
The terminating character is wrong:
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 @
You can call it from the command line like:
db2 -td@ -vf test
回答2:
A trick is to end the lines with a comment like:
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 ;
Then you can continue using ; as a statement terminator
You might want to simplify the function, something like:
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
should do
来源:https://stackoverflow.com/questions/24758349/db2-function-error