<?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: Inline stored procedure/function in a query in Technology Q&amp;A</title>
    <link>https://community.sap.com/t5/technology-q-a/inline-stored-procedure-function-in-a-query/qaa-p/13836922#M4867765</link>
    <description>&lt;P&gt;Thank you nmelion - after adding the semicolons (and defining the function at the top as CREATE TEMPORARY), my query worked great, calling the temporary function and returning data.  &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
    <pubDate>Fri, 31 Mar 2017 10:10:22 GMT</pubDate>
    <dc:creator>Former Member</dc:creator>
    <dc:date>2017-03-31T10:10:22Z</dc:date>
    <item>
      <title>Inline stored procedure/function in a query</title>
      <link>https://community.sap.com/t5/technology-q-a/inline-stored-procedure-function-in-a-query/qaq-p/13836916</link>
      <description>&lt;P&gt;Hello, I want to write a query used on several versions of a database.  The later versions have defined a stored procedure/function that I can use, (but it is missing in the earlier versions).  If it does not exist, is it possible to copy/paste the code into the query and use it directly?&lt;/P&gt;
&lt;P&gt;Here is the abbreviated stored function:&lt;/P&gt;
&lt;DIV&gt;&lt;PRE&gt;&lt;SPAN&gt;&lt;/SPAN&gt;ALTER FUNCTION "AdminGroup"."MakeInternalId" (IN recordId INTEGER, IN createdTimeStamp INTEGER)
RETURNS VARCHAR(65)
DETERMINISTIC 
BEGIN
    DECLARE id VARCHAR(65); 
    DECLARE highest INTEGER;
    DECLARE prefix VARCHAR(65);
    DECLARE byteOffset INTEGER;

    SET highest = 0;

    SET byteOffset = (2 * 4) + 1;

    SELECT CAST(CAST(STRING(
       BYTE_SUBSTR(CAST(blob AS VARCHAR), byteOffset + 3, 1),
       BYTE_SUBSTR(CAST(blob AS VARCHAR), byteOffset + 2, 1),
       BYTE_SUBSTR(CAST(blob AS VARCHAR), byteOffset + 1, 1),
       BYTE_SUBSTR(CAST(blob AS VARCHAR), byteOffset, 1)) AS BINARY) AS INTEGER ) INTO highest
    FROM metadata
    WHERE  external_key = 'highest';

    SET highest = ISNULL (highest, 0);

    IF (recordId &amp;lt;= highest) THEN
       SET prefix = INTTOHEX ((recordId * power(2, 16)));
    ELSE
       SET prefix = INTTOHEX (0x80000000 | recordId); 
    END IF;

    SET prefix = UPPER(REPLACE(LTRIM(REPLACE(prefix,'0',' ')),' ','0'));    
    SET id = STRING (prefix, '-', createdTimeStamp);
    RETURN id;
END
&lt;/PRE&gt;&lt;/DIV&gt;


&lt;P&gt;I tried copy/pasting the above code into Sybase Central - Interactive SQL window, just above my query and got a syntax error where my query starts.&lt;/P&gt;
&lt;P&gt;Thanks,
David&lt;/P&gt;</description>
      <pubDate>Thu, 30 Mar 2017 09:57:10 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/inline-stored-procedure-function-in-a-query/qaq-p/13836916</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2017-03-30T09:57:10Z</dc:date>
    </item>
    <item>
      <title>Re: Inline stored procedure/function in a query</title>
      <link>https://community.sap.com/t5/technology-q-a/inline-stored-procedure-function-in-a-query/qaa-p/13836917#M4867760</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;Is it possible to copy/paste the code into the query and use it directly?&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;No, AFAIK a query cannot contain a &lt;A href="http://dcx.sap.com/index.html#sqla170/en/html/3bef50536c5f10148610ce61184a9667.html"&gt;compound statement&lt;/A&gt; or &lt;A href="http://dcx.sap.com/index.html#sqla170/en/html/3bec29c76c5f1014a91fa4cd3ffedef7.html"&gt;control statements&lt;/A&gt;, it can only directly call stored functions and select from stored procedures.&lt;/P&gt;
&lt;P&gt;Some alternatives:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Use expressions like the CASE or &lt;A href="http://dcx.sap.com/index.html#sqla170/en/html/8170fd656ce2101484578af3b0843edf.html"&gt;IF expression&lt;/A&gt; (which are different from the CASE or IF statements!) to define some conditional, expresssion-based logic which is allowed in queries&lt;/LI&gt;
&lt;LI&gt;Use a &lt;A href="http://dcx.sap.com/index.html#sqla170/en/html/816bdf316ce210148d3acbebf6d39b18.html"&gt;temporary function&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;Check whether the (permanent) function exists, and create it if not (see your other question...)&lt;/LI&gt;
&lt;LI&gt;Use CREATE OR REPLACE function as a handy alternative.&lt;/LI&gt;
&lt;/UL&gt;
&lt;HR /&gt;
&lt;P&gt;If your actual issue has to do with a builtin function that only exists in newer version of the database software, you could write a wrapper function that&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;either calls the builtin function (if that is available)&lt;/LI&gt;
&lt;LI&gt;or calls a homebrown one.&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;(I'm not sure whether "older version of the database" refers to different versions of SQL Anywhere or different versions of your database schema...)&lt;/P&gt;</description>
      <pubDate>Thu, 30 Mar 2017 10:32:22 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/inline-stored-procedure-function-in-a-query/qaa-p/13836917#M4867760</guid>
      <dc:creator>VolkerBarth</dc:creator>
      <dc:date>2017-03-30T10:32:22Z</dc:date>
    </item>
    <item>
      <title>Re: Inline stored procedure/function in a query</title>
      <link>https://community.sap.com/t5/technology-q-a/inline-stored-procedure-function-in-a-query/qaa-p/13836919#M4867762</link>
      <description>&lt;P&gt;Does it work if you simply change the word "&lt;STRONG&gt;&lt;EM&gt;ALTER&lt;/EM&gt;&lt;/STRONG&gt;" into "&lt;STRONG&gt;&lt;EM&gt;CREATE&lt;/EM&gt;&lt;/STRONG&gt;"?
Depending upon the versions involved, an if &lt;A href="http://dcx.sap.com/index.html#1201/en/dbreference/create-procedure-user-defined-statement.html?"&gt;&lt;STRONG&gt;&lt;EM&gt;"CREATE OR REPLACE PROCEDURE ... " syntax&lt;/EM&gt;&lt;/STRONG&gt;&lt;/A&gt; is supported you could also resort to that.&lt;/P&gt;
&lt;P&gt;Or is your issue some else like ... Maybe pasting a query with the BYTE_SUBSTR( ) into a pre-8 version of the software?&lt;/P&gt;</description>
      <pubDate>Thu, 30 Mar 2017 10:32:33 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/inline-stored-procedure-function-in-a-query/qaa-p/13836919#M4867762</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2017-03-30T10:32:33Z</dc:date>
    </item>
    <item>
      <title>Re: Inline stored procedure/function in a query</title>
      <link>https://community.sap.com/t5/technology-q-a/inline-stored-procedure-function-in-a-query/qaa-p/13836918#M4867761</link>
      <description>&lt;P&gt;Let me guess: Syntax error near (the first keyword of your next statement).&lt;/P&gt;
&lt;P&gt;Did you remember to add a semicolon after the END? That's a Watcom-SQL dialect procedure. When working in Watcom-SQL, semi-colons are required between each statement.&lt;/P&gt;</description>
      <pubDate>Thu, 30 Mar 2017 11:02:31 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/inline-stored-procedure-function-in-a-query/qaa-p/13836918#M4867761</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2017-03-30T11:02:31Z</dc:date>
    </item>
    <item>
      <title>Re: Inline stored procedure/function in a query</title>
      <link>https://community.sap.com/t5/technology-q-a/inline-stored-procedure-function-in-a-query/qaa-p/13836920#M4867763</link>
      <description>&lt;P&gt;Thank you Nick, changing ALTER to CREATE TEMPORARY was exactly what I needed!&lt;/P&gt;</description>
      <pubDate>Fri, 31 Mar 2017 10:08:02 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/inline-stored-procedure-function-in-a-query/qaa-p/13836920#M4867763</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2017-03-31T10:08:02Z</dc:date>
    </item>
    <item>
      <title>Re: Inline stored procedure/function in a query</title>
      <link>https://community.sap.com/t5/technology-q-a/inline-stored-procedure-function-in-a-query/qaa-p/13836921#M4867764</link>
      <description>&lt;P&gt;Thank you Volker - yes, I did use CREATE TEMPORARY.  The SQL Anywhere version is sufficient, it was my database scehema that had changed to include the stored function in later versions.&lt;/P&gt;</description>
      <pubDate>Fri, 31 Mar 2017 10:09:01 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/inline-stored-procedure-function-in-a-query/qaa-p/13836921#M4867764</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2017-03-31T10:09:01Z</dc:date>
    </item>
    <item>
      <title>Re: Inline stored procedure/function in a query</title>
      <link>https://community.sap.com/t5/technology-q-a/inline-stored-procedure-function-in-a-query/qaa-p/13836922#M4867765</link>
      <description>&lt;P&gt;Thank you nmelion - after adding the semicolons (and defining the function at the top as CREATE TEMPORARY), my query worked great, calling the temporary function and returning data.  &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 31 Mar 2017 10:10:22 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/inline-stored-procedure-function-in-a-query/qaa-p/13836922#M4867765</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2017-03-31T10:10:22Z</dc:date>
    </item>
  </channel>
</rss>

