如何将Snowflake中的SQL语句转换为SQL用户自定义函数?

问题描述 投票:0回答:1

我有一个 SQL 语句在雪花中运行良好,但是当我将其转换为用户定义函数时,它抛出了意外错误。

这是在雪花中运行良好的 SQL 语句:

Declare originalcontent String default('"ezyVetReminderFuture6weeksPostcard"
<br />  &quot;AnimalNextVaccinationDate_Start&quot;08-31-2026&quot;AnimalNextVaccinationDate_End&quot;
<br />  &quot;AnimalNextVaccinationName_Start&quot;DAP&quot;AnimalNextVaccinationName_End&quot;
<br />  &quot;AnimalLastVisitDate_Start&quot;09-01-2023&quot;AnimalLastVisitDate_End&quot;
<br />  &quot;AnimalNextAppointmentDate_Start&quot;&quot;AnimalNextAppointmentDate_End&quot;
<br />  &quot;AnimalSOCDueInNextYear_Start&quot;
<ul><li>Wellness Blood and Urine Screen Due on 03-13-2024</li>
<li>Wellness Exam Due on 05-13-2024</li>
</ul>&quot;AnimalSOCDueInNextYear_End&quot;');
extracttext String;
sol String;
sol_refined String;
content String;
Begin
    content := originalcontent;
    while (REGEXP_INSTR(content, '<li>')>1) do
        extracttext := SUBSTRING(content, REGEXP_INSTR(content, '<li>'), REGEXP_INSTR(content,'</li>')-REGEXP_INSTR(content, '<li>'))||'</li>';
        IF (sol IS NULL) THEN
          sol := extracttext;
        ELSE
          sol := sol || extracttext;
        END IF;

        IF (sol_refined IS NULL) THEN
            sol_refined := CASE WHEN 
            DATEDIFF('month', GETDATE(), TRY_TO_DATE(SUBSTRING(RIGHT(extracttext, 15), 1, 10),'MM-DD-YYYY')) <= 3 
            THEN extracttext ELSE '' END;
        ELSE
            sol_refined := sol_refined || CASE WHEN 
            DATEDIFF('month', GETDATE(), TRY_TO_DATE(SUBSTRING(RIGHT(extracttext, 15), 1, 10),'MM-DD-YYYY')) <= 3 
            THEN extracttext ELSE '' END;
        END IF;  
    
        content := replace(content,extracttext,'');
    
    end while;
    return replace(replace(replace(sol_refined,'</li><li>','|'),'<li>',''),'</li>','');
End;

现在这被转换为雪花中的用户定义函数,我收到错误:

CREATE OR REPLACE FUNCTION FN_PARSE(originalcontent STRING)
  RETURNS STRING
  LANGUAGE SQL
  AS
  $$
  Declare extracttext String;
sol String;
sol_refined String;
content String;
Begin
    content := originalcontent;
    while (REGEXP_INSTR(content, '<li>')>1) do
        extracttext := SUBSTRING(content, REGEXP_INSTR(content, '<li>'), REGEXP_INSTR(content,'</li>')-REGEXP_INSTR(content, '<li>'))||'</li>';
        IF (sol IS NULL) THEN
          sol := extracttext;
        ELSE
          sol := sol || extracttext;
        END IF;

        IF (sol_refined IS NULL) THEN
            sol_refined := CASE WHEN 
            DATEDIFF('month', GETDATE(), TRY_TO_DATE(SUBSTRING(RIGHT(extracttext, 15), 1, 10),'MM-DD-YYYY')) <= 3 
            THEN extracttext ELSE '' END;
        ELSE
            sol_refined := sol_refined || CASE WHEN 
            DATEDIFF('month', GETDATE(), TRY_TO_DATE(SUBSTRING(RIGHT(extracttext, 15), 1, 10),'MM-DD-YYYY')) <= 3 
            THEN extracttext ELSE '' END;
        END IF;  
    
        content := replace(content,extracttext,'');
    
    end while;
    return replace(replace(replace(sol_refined,'</li><li>','|'),'<li>',''),'</li>','');
End;
$$;

我非常感谢您对此的帮助。谢谢你

sql snowflake-cloud-data-platform user-defined-functions
1个回答
0
投票

Snowflake SQL 用户定义函数仅限于根据 SQL UDF 执行单个 SQL 表达式。比如:

CREATE FUNCTION area_of_circle(radius FLOAT)
  RETURNS FLOAT
  AS
  $$
    pi() * radius * radius
  $$
  ;

对于更复杂的事情,您将需要 SQL 存储过程,或者使用 JavaScript、Python、Java 或 Scala 定义的 UDF。

© www.soinside.com 2019 - 2024. All rights reserved.