db2 - CASE WHEN or IF Statement in CREATE FUNCTION
Hi I've got a problem managing CASE WHEN or IF Statements in a CREATE FUNCTION Call in DB2
I tried this Statement:
CREATE OR REPLACE FUNCTION NAECHSTES_DATUM(PARAM1 CHAR(6), PARAM2 DATE)
RETURNS DATE
LANGUAGE SQL
BEGIN
DECLARE BASEDATE DATE;
DECLARE THATDATE DATE;
SET BASEDATE = TO_DATE(CONCAT(PARAM1,CAST(YEAR(PARAM2) AS CHAR(4))),'DD.MM.YYYY');
IF (BASEDATE >= PARAM2)
THEN SET THATDATE = BASEDATE;
ELSE SET THATDATE = BASEDATE + 1 YEAR;
END IF;
RETURN THATDATE;
END
I get this error
[-104] Auf "+ 1 YEAR" folgte das unerwartete Token "END-OF-STATEMENT". Mögliche Tokens: "
END IF".. SQLCODE=-104, SQLSTATE=42601, DRIVER=4.14.113
Similar result when I use CASE WHEN.
Do you know where the problem could be?
function if-statement db2 case
add a comment |
Hi I've got a problem managing CASE WHEN or IF Statements in a CREATE FUNCTION Call in DB2
I tried this Statement:
CREATE OR REPLACE FUNCTION NAECHSTES_DATUM(PARAM1 CHAR(6), PARAM2 DATE)
RETURNS DATE
LANGUAGE SQL
BEGIN
DECLARE BASEDATE DATE;
DECLARE THATDATE DATE;
SET BASEDATE = TO_DATE(CONCAT(PARAM1,CAST(YEAR(PARAM2) AS CHAR(4))),'DD.MM.YYYY');
IF (BASEDATE >= PARAM2)
THEN SET THATDATE = BASEDATE;
ELSE SET THATDATE = BASEDATE + 1 YEAR;
END IF;
RETURN THATDATE;
END
I get this error
[-104] Auf "+ 1 YEAR" folgte das unerwartete Token "END-OF-STATEMENT". Mögliche Tokens: "
END IF".. SQLCODE=-104, SQLSTATE=42601, DRIVER=4.14.113
Similar result when I use CASE WHEN.
Do you know where the problem could be?
function if-statement db2 case
Possible duplicate of An unexpected token "CREATE TRIGGER
– mustaccio
Dec 31 '18 at 15:47
add a comment |
Hi I've got a problem managing CASE WHEN or IF Statements in a CREATE FUNCTION Call in DB2
I tried this Statement:
CREATE OR REPLACE FUNCTION NAECHSTES_DATUM(PARAM1 CHAR(6), PARAM2 DATE)
RETURNS DATE
LANGUAGE SQL
BEGIN
DECLARE BASEDATE DATE;
DECLARE THATDATE DATE;
SET BASEDATE = TO_DATE(CONCAT(PARAM1,CAST(YEAR(PARAM2) AS CHAR(4))),'DD.MM.YYYY');
IF (BASEDATE >= PARAM2)
THEN SET THATDATE = BASEDATE;
ELSE SET THATDATE = BASEDATE + 1 YEAR;
END IF;
RETURN THATDATE;
END
I get this error
[-104] Auf "+ 1 YEAR" folgte das unerwartete Token "END-OF-STATEMENT". Mögliche Tokens: "
END IF".. SQLCODE=-104, SQLSTATE=42601, DRIVER=4.14.113
Similar result when I use CASE WHEN.
Do you know where the problem could be?
function if-statement db2 case
Hi I've got a problem managing CASE WHEN or IF Statements in a CREATE FUNCTION Call in DB2
I tried this Statement:
CREATE OR REPLACE FUNCTION NAECHSTES_DATUM(PARAM1 CHAR(6), PARAM2 DATE)
RETURNS DATE
LANGUAGE SQL
BEGIN
DECLARE BASEDATE DATE;
DECLARE THATDATE DATE;
SET BASEDATE = TO_DATE(CONCAT(PARAM1,CAST(YEAR(PARAM2) AS CHAR(4))),'DD.MM.YYYY');
IF (BASEDATE >= PARAM2)
THEN SET THATDATE = BASEDATE;
ELSE SET THATDATE = BASEDATE + 1 YEAR;
END IF;
RETURN THATDATE;
END
I get this error
[-104] Auf "+ 1 YEAR" folgte das unerwartete Token "END-OF-STATEMENT". Mögliche Tokens: "
END IF".. SQLCODE=-104, SQLSTATE=42601, DRIVER=4.14.113
Similar result when I use CASE WHEN.
Do you know where the problem could be?
function if-statement db2 case
function if-statement db2 case
asked Dec 31 '18 at 9:46
Matthias HerbstMatthias Herbst
81
81
Possible duplicate of An unexpected token "CREATE TRIGGER
– mustaccio
Dec 31 '18 at 15:47
add a comment |
Possible duplicate of An unexpected token "CREATE TRIGGER
– mustaccio
Dec 31 '18 at 15:47
Possible duplicate of An unexpected token "CREATE TRIGGER
– mustaccio
Dec 31 '18 at 15:47
Possible duplicate of An unexpected token "CREATE TRIGGER
– mustaccio
Dec 31 '18 at 15:47
add a comment |
1 Answer
1
active
oldest
votes
Use an alternative statement delimiter after the END of the function.
Inside the function the statement delimiter is the semicolon (;)
But Db2 needs to know an additional delimiter to indicate the end of the block.
For the Db2 command line (in shell scripts, batch files) you can use the "-td@" command line option and terminate the block with the @ character. Inside a file that contains the function you can use:
--#SET TERMINATOR @
anywhere before the function block and then terminate the block with @
If you are use a GUI tool to submit the DDL or SQL, each GUI tool has its own way to specify alternative statement delimiters. Look at the settings and properties. It's always wise to fully describe your toolset (which programs, which versions, which operating system etc) in your question.
Thank you! The hint about the delimiter made it clear to me. I got it working running it within a sql scipt in dbeaver and also in liquibase. When I set "Ignore native delimiter" it worked with dbeaver. In Liquibase you need to use the <createProcedure> tag. Otherwise it won't work.
– Matthias Herbst
Jan 3 at 7:36
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53985959%2fdb2-case-when-or-if-statement-in-create-function%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Use an alternative statement delimiter after the END of the function.
Inside the function the statement delimiter is the semicolon (;)
But Db2 needs to know an additional delimiter to indicate the end of the block.
For the Db2 command line (in shell scripts, batch files) you can use the "-td@" command line option and terminate the block with the @ character. Inside a file that contains the function you can use:
--#SET TERMINATOR @
anywhere before the function block and then terminate the block with @
If you are use a GUI tool to submit the DDL or SQL, each GUI tool has its own way to specify alternative statement delimiters. Look at the settings and properties. It's always wise to fully describe your toolset (which programs, which versions, which operating system etc) in your question.
Thank you! The hint about the delimiter made it clear to me. I got it working running it within a sql scipt in dbeaver and also in liquibase. When I set "Ignore native delimiter" it worked with dbeaver. In Liquibase you need to use the <createProcedure> tag. Otherwise it won't work.
– Matthias Herbst
Jan 3 at 7:36
add a comment |
Use an alternative statement delimiter after the END of the function.
Inside the function the statement delimiter is the semicolon (;)
But Db2 needs to know an additional delimiter to indicate the end of the block.
For the Db2 command line (in shell scripts, batch files) you can use the "-td@" command line option and terminate the block with the @ character. Inside a file that contains the function you can use:
--#SET TERMINATOR @
anywhere before the function block and then terminate the block with @
If you are use a GUI tool to submit the DDL or SQL, each GUI tool has its own way to specify alternative statement delimiters. Look at the settings and properties. It's always wise to fully describe your toolset (which programs, which versions, which operating system etc) in your question.
Thank you! The hint about the delimiter made it clear to me. I got it working running it within a sql scipt in dbeaver and also in liquibase. When I set "Ignore native delimiter" it worked with dbeaver. In Liquibase you need to use the <createProcedure> tag. Otherwise it won't work.
– Matthias Herbst
Jan 3 at 7:36
add a comment |
Use an alternative statement delimiter after the END of the function.
Inside the function the statement delimiter is the semicolon (;)
But Db2 needs to know an additional delimiter to indicate the end of the block.
For the Db2 command line (in shell scripts, batch files) you can use the "-td@" command line option and terminate the block with the @ character. Inside a file that contains the function you can use:
--#SET TERMINATOR @
anywhere before the function block and then terminate the block with @
If you are use a GUI tool to submit the DDL or SQL, each GUI tool has its own way to specify alternative statement delimiters. Look at the settings and properties. It's always wise to fully describe your toolset (which programs, which versions, which operating system etc) in your question.
Use an alternative statement delimiter after the END of the function.
Inside the function the statement delimiter is the semicolon (;)
But Db2 needs to know an additional delimiter to indicate the end of the block.
For the Db2 command line (in shell scripts, batch files) you can use the "-td@" command line option and terminate the block with the @ character. Inside a file that contains the function you can use:
--#SET TERMINATOR @
anywhere before the function block and then terminate the block with @
If you are use a GUI tool to submit the DDL or SQL, each GUI tool has its own way to specify alternative statement delimiters. Look at the settings and properties. It's always wise to fully describe your toolset (which programs, which versions, which operating system etc) in your question.
edited Dec 31 '18 at 10:20
answered Dec 31 '18 at 9:52
maomao
3,8901316
3,8901316
Thank you! The hint about the delimiter made it clear to me. I got it working running it within a sql scipt in dbeaver and also in liquibase. When I set "Ignore native delimiter" it worked with dbeaver. In Liquibase you need to use the <createProcedure> tag. Otherwise it won't work.
– Matthias Herbst
Jan 3 at 7:36
add a comment |
Thank you! The hint about the delimiter made it clear to me. I got it working running it within a sql scipt in dbeaver and also in liquibase. When I set "Ignore native delimiter" it worked with dbeaver. In Liquibase you need to use the <createProcedure> tag. Otherwise it won't work.
– Matthias Herbst
Jan 3 at 7:36
Thank you! The hint about the delimiter made it clear to me. I got it working running it within a sql scipt in dbeaver and also in liquibase. When I set "Ignore native delimiter" it worked with dbeaver. In Liquibase you need to use the <createProcedure> tag. Otherwise it won't work.
– Matthias Herbst
Jan 3 at 7:36
Thank you! The hint about the delimiter made it clear to me. I got it working running it within a sql scipt in dbeaver and also in liquibase. When I set "Ignore native delimiter" it worked with dbeaver. In Liquibase you need to use the <createProcedure> tag. Otherwise it won't work.
– Matthias Herbst
Jan 3 at 7:36
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53985959%2fdb2-case-when-or-if-statement-in-create-function%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Possible duplicate of An unexpected token "CREATE TRIGGER
– mustaccio
Dec 31 '18 at 15:47