I am trying to create a UDTF in Snowflake but I get an error on RETURNS TABLE even though it matches the syntax

huangapple go评论51阅读模式
英文:

I am trying to create a UDTF in Snowflake but I get an error on RETURNS TABLE even though it matches the syntax

问题

以下是您提供的代码的中文翻译:

创建或替换函数 DQ_IS_NULL ("SCHEMA_NAME" VARCHAR(500), "TABLE_NAME" VARCHAR(500), "COLUMN_NAME" VARCHAR(500))
返回表 (TABLE_NME VARCHAR(500), COLUMN_NME VARCHAR(500), ISSUE_CODE VARCHAR(100), PARAMETERS VARCHAR(500), VALUE VARCHAR (500), ROW_HASH NUMBER(19,0), EXECUTION_TS TIMESTAMP)
语言 SQL
作为
'
    选择 
    表名,
    列名,
    CASE 
        当 V 不为空时
            THEN 'NULLed'
    END AS ISSUE_CODE,
    CASE 
        当列名不为空时
            THEN '不允许为空'
    END AS PARAMETERS,
    HASH(*) AS ROW_HASH,
    当前时间() AS EXECUTION_TS
    从 连接(SCHEMA_NAME ,'.',TABLE_NAME)
    其中列名为NULL

这个函数的目的是返回给定表中所有为空的行。

英文:
CREATE OR REPLACE FUNCTION DQ_IS_NULL ("SCHEMA_NAME" VARCHAR(500), "TABLE_NAME" VARCHAR(500), "COLUMN_NAME" VARCHAR(500))
RETURNS TABLE (TABLE_NME VARCHAR(500), COLUMN_NME VARCHAR(500), ISSUE_CODE VARCHAR(100), PARAMETERS VARCHAR(500), VALUE VARCHAR (500), ROW_HASH NUMBER(19,0), EXECUTION_TS TIMESTAMP)
LANGUAGE SQL
AS
'
    SELECT 
    TABLE_NAME,
    COLUMN_NAME,
    CASE 
        WHEN V IS NOT NULL
            THEN ''NULLed''
    END AS ISSUE_CODE,
    CASE 
        WHEN COLUMN_NAME IS NOT NULL
            THEN ''NULLS not permitted''
    END AS PARAMETERS,
    HASH(*) AS ROW_HASH,
    CURRENT_TIME() AS EXECUTION_TS
    FROM CONCAT(SCHEMA_NAME ,''.'',TABLE_NAME)
    WHERE COLUMN_NAME IS NULL

The function is intended to return all rows in a given table that are null

答案1

得分: 1

问题在于您使用'来表示函数定义的开始,但在函数体内也有一个'。您可以使用$$来解决此问题:

CREATE OR REPLACE TEMP FUNCTION DQ_IS_NULL ("SCHEMA_NAME" VARCHAR(500), "TABLE_NAME" VARCHAR(500), "COLUMN_NAME" VARCHAR(500))
RETURNS TABLE (TABLE_NME VARCHAR(500), COLUMN_NME VARCHAR(500), ISSUE_CODE VARCHAR(100), PARAMETERS VARCHAR(500), VALUE VARCHAR (500), ROW_HASH NUMBER(19,0), EXECUTION_TS TIMESTAMP)
LANGUAGE SQL
AS
$$
    SELECT 
    TABLE_NAME,
    COLUMN_NAME,
    CASE 
        WHEN V IS NOT NULL
            THEN 'NULLed'
    END AS ISSUE_CODE,
    CASE 
        WHEN COLUMN_NAME IS NOT NULL
            THEN 'NULLS not permitted'
    END AS PARAMETERS,
    HASH(*) AS ROW_HASH,
    CURRENT_TIME() AS EXECUTION_TS
    FROM CONCAT(SCHEMA_NAME ,'',TABLE_NAME)
    WHERE COLUMN_NAME IS NULL
$$;

请注意,然而还有另一个问题;在UDTF体内,您需要使用真实的表名,而不是CONCAT(SCHEMA_NAME ,''.'',TABLE_NAME)

英文:

The problem is that you're using ' to denote the start of the function definition, but there is a ' within the body as well. You can use $$ to work around the issue:

CREATE OR REPLACE TEMP FUNCTION DQ_IS_NULL ("SCHEMA_NAME" VARCHAR(500), "TABLE_NAME" VARCHAR(500), "COLUMN_NAME" VARCHAR(500))
RETURNS TABLE (TABLE_NME VARCHAR(500), COLUMN_NME VARCHAR(500), ISSUE_CODE VARCHAR(100), PARAMETERS VARCHAR(500), VALUE VARCHAR (500), ROW_HASH NUMBER(19,0), EXECUTION_TS TIMESTAMP)
LANGUAGE SQL
AS
$$
    SELECT 
    TABLE_NAME,
    COLUMN_NAME,
    CASE 
        WHEN V IS NOT NULL
            THEN 'NULLed'
    END AS ISSUE_CODE,
    CASE 
        WHEN COLUMN_NAME IS NOT NULL
            THEN 'NULLS not permitted'
    END AS PARAMETERS,
    HASH(*) AS ROW_HASH,
    CURRENT_TIME() AS EXECUTION_TS
    FROM CONCAT(SCHEMA_NAME ,''.'',TABLE_NAME)
    WHERE COLUMN_NAME IS NULL
$$;

Note that there's another issue still, however; you need to use a real table name in the UDTF body, as opposed to CONCAT(SCHEMA_NAME ,''.'',TABLE_NAME).

huangapple
  • 本文由 发表于 2023年2月14日 22:13:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75449090.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定