<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>Question Re: CASE statement inside a function in Technology Q&amp;A</title>
    <link>https://community.sap.com/t5/technology-q-a/case-statement-inside-a-function/qaa-p/13844405#M4875248</link>
    <description>&lt;P&gt;In addition to Thomas's answer, you might also use a CASE EXPRESSION instead of a CASE STATEMENT - that will omit all the "SET @RoleType = " statements:&lt;/P&gt;
&lt;PRE class="codehilite"&gt;&lt;CODE class="language-sql"&gt;DROP FUNCTION IF EXISTS RoleType;
CREATE TEMPORARY FUNCTION RoleType(typeId INT)
RETURNS CHAR(30)
BEGIN
    DECLARE @RoleType CHAR(30);
    SET @RoleType =
        CASE typeId
            WHEN 0 THEN 'Own'
            WHEN 1 THEN 'Child'
            WHEN 2 THEN 'Root'
            WHEN 3 THEN 'Shortcut'
                   ELSE '(unspecified)'
        END CASE;
    RETURN @RoleType;
END;

SELECT RoleType(1);
SELECT RoleType(null);&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 29 Aug 2014 04:23:02 GMT</pubDate>
    <dc:creator>VolkerBarth</dc:creator>
    <dc:date>2014-08-29T04:23:02Z</dc:date>
    <item>
      <title>CASE statement inside a function</title>
      <link>https://community.sap.com/t5/technology-q-a/case-statement-inside-a-function/qaq-p/13844403</link>
      <description>&lt;P&gt;Hi all, I'm trying to make a convenience function that will expose an integer field as a human readable string, but for some reason I can't seem to get the syntax correct. I have tried many different approaches, but none seem to work. I am sure it is something fairly simple, but for the life of me I just can't seem to make a CASE statement work inside a FUNCTION. From my research, this should work&lt;/P&gt;
&lt;PRE class="codehilite"&gt;&lt;CODE&gt;CREATE TEMPORARY FUNCTION RoleType(typeId INT)
RETURNS CHAR(30)
BEGIN
    DECLARE RoleType CHAR(30)
    CASE typeId
            WHEN 0 THEN SET RoleType = 'Own'
            WHEN 1 THEN SET RoleType = 'Child'
        WHEN 2 THEN SET RoleType = 'Root'
        WHEN 3 THEN SET RoleType = 'Shortcut'
    -- etc...
    END
    return RoleType
END


&lt;P&gt;I know I could store these values in a look-up table, but honestly I'd rather just call the function than have to inner join a table every time I wanted to look up the role type, besides, my inability to make this work has frustrated me enough that I won't be happy until I've figured out what I'm doing wrong.&lt;/P&gt;
&lt;P&gt;thanks&lt;/P&gt;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 28 Aug 2014 19:58:25 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/case-statement-inside-a-function/qaq-p/13844403</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2014-08-28T19:58:25Z</dc:date>
    </item>
    <item>
      <title>Re: CASE statement inside a function</title>
      <link>https://community.sap.com/t5/technology-q-a/case-statement-inside-a-function/qaa-p/13844404#M4875247</link>
      <description>&lt;P&gt;It works this way.&lt;/P&gt;
&lt;PRE class="codehilite"&gt;&lt;CODE class="language-sql"&gt;CREATE TEMPORARY FUNCTION RoleType(typeId INT)
RETURNS CHAR(30)
BEGIN
    DECLARE @RoleType CHAR(30);
    CASE typeId
    WHEN 0 THEN 
        SET @RoleType = 'Own';
    WHEN 1 THEN 
        SET @RoleType = 'Child';
    WHEN 2 THEN 
        SET @RoleType = 'Root';
    WHEN 3 THEN 
        SET @RoleType = 'Shortcut';
    END;
    return @RoleType;
END


&lt;P&gt;It seams that you can't declare a variable with the same name as the function. I did not realize this before. Thanks for letting me know ;-)&lt;/P&gt;
&lt;P&gt;Thomas&lt;/P&gt;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 29 Aug 2014 02:09:56 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/case-statement-inside-a-function/qaa-p/13844404#M4875247</guid>
      <dc:creator>thomas_duemesnil</dc:creator>
      <dc:date>2014-08-29T02:09:56Z</dc:date>
    </item>
    <item>
      <title>Re: CASE statement inside a function</title>
      <link>https://community.sap.com/t5/technology-q-a/case-statement-inside-a-function/qaa-p/13844405#M4875248</link>
      <description>&lt;P&gt;In addition to Thomas's answer, you might also use a CASE EXPRESSION instead of a CASE STATEMENT - that will omit all the "SET @RoleType = " statements:&lt;/P&gt;
&lt;PRE class="codehilite"&gt;&lt;CODE class="language-sql"&gt;DROP FUNCTION IF EXISTS RoleType;
CREATE TEMPORARY FUNCTION RoleType(typeId INT)
RETURNS CHAR(30)
BEGIN
    DECLARE @RoleType CHAR(30);
    SET @RoleType =
        CASE typeId
            WHEN 0 THEN 'Own'
            WHEN 1 THEN 'Child'
            WHEN 2 THEN 'Root'
            WHEN 3 THEN 'Shortcut'
                   ELSE '(unspecified)'
        END CASE;
    RETURN @RoleType;
END;

SELECT RoleType(1);
SELECT RoleType(null);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 29 Aug 2014 04:23:02 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/case-statement-inside-a-function/qaa-p/13844405#M4875248</guid>
      <dc:creator>VolkerBarth</dc:creator>
      <dc:date>2014-08-29T04:23:02Z</dc:date>
    </item>
  </channel>
</rss>

