如何引发变量异常?

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

我在 Snowflake SQL 中创建了以下过程:

CREATE OR REPLACE PROCEDURE MAIN.PUBLIC.PROPERTY_CONSISTENCY_CHECK()
RETURNS STRING
LANGUAGE SQL
AS
$$
DECLARE
    variable_1 INTEGER;
    variable_2 STRING;
    variable_3 DATE;
    variable_4 STRING;
    error_messages STRING := ''; 
    UPDATED_AT_IS_NOT_CURRENT_DATE EXCEPTION(-20002,'Following errors occured: HIEREINFUEGEN');

BEGIN

    SELECT COUNT(*) 
    INTO variable_1 
    FROM TABLE1;

    SELECT LISTAGG(Property_ID, ', ') 
    INTO variable_2
    FROM (
        SELECT DISTINCT Property_ID 
        FROM TABLE1
        AT (TIMESTAMP => '2024-11-19 07:00:00'::TIMESTAMP)
        GROUP BY Customer_ID, Property_ID 
        HAVING COUNT(*) > 1
    );

    SELECT DISTINCT UPDATED_AT 
    INTO variable_3 
    FROM TABLE1 
    AT (TIMESTAMP => '2024-11-19 07:00:00'::TIMESTAMP);

    SELECT LISTAGG(Property_ID, ', ') 
    INTO variable_4
    FROM TABLE2 dim
    WHERE dim.Property_ID NOT IN (
        SELECT DISTINCT Property_ID 
        FROM TABLE1
    );

    IF (variable_1 = 0) THEN
        error_messages := error_messages || 'The Customer Property Table is empty;' || CHR(10) || CHR(10);  
    END IF;

    IF (variable_2 IS NOT NULL AND variable_2 != '') THEN
        error_messages := error_messages || 'Duplicate Property detected: ' || variable_2 || ';' || CHR(10) || CHR(10);  
    END IF;

    IF (variable_3 IS NULL OR variable_3 != CURRENT_DATE()) THEN
        error_messages := error_messages || 'The Updated_AT Column is not the Current Date;' || CHR(10) || CHR(10);
    END IF;

    IF (variable_4 IS NOT NULL AND variable_4 != '') THEN
        error_messages := error_messages || 'Missing Properties detected: ' || variable_4 || ';' || CHR(10) || CHR(10);  
    END IF;

    IF (error_messages != '') THEN
        RAISE UPDATED_AT_IS_NOT_CURRENT_DATE;  
    ELSE
        RETURN 'No errors detected';  
    END IF;

END;
$$;

CALL MAIN.PUBLIC.PROPERTY_CONSISTENCY_CHECK();

我想要做的是将变量

error_messages
添加到我的异常输出(
HERE_I_WANT_MY_VARIABLE
)中。我不想返回一个字符串。引发异常非常重要。我希望当我在 Jenkins 中调用过程并且出现异常时,管道也会被取消,并且我的变量文本会显示在错误消息中,但是我该怎么做?

sql variables jenkins snowflake-cloud-data-platform procedure
1个回答
0
投票

您可以按照您的方式从 if/else 块中引发异常,然后相应地创建一个 EXCEPTION 块。

我创建了一个示例过程,当我调用该过程时,我能够在输出中获取自定义错误消息,这也应该填充在您的 CI/CD 管道中。

CREATE OR REPLACE PROCEDURE PROPERTY_CONSISTENCY_CHECK()
RETURNS STRING
LANGUAGE SQL
AS
$$
DECLARE
    variable_1 INTEGER;
 
    variable_3 DATE;
    variable_4 STRING;
    error_messages STRING := ''; 
    
    -- Declare a custom exception
    updated_at_is_not_current_date EXCEPTION(-20002,'Following errors occured: HIEREINFUEGEN'); 

BEGIN
    -- Example queries to set up the values
    SELECT COUNT(*) 
    INTO variable_1 
    FROM TABLE1;
    
    -- Debugging step: Count the rows in TABLE1
  -- RETURN 'Debug: variable_1 = ' || variable_1;


    -- Get the most recent 'UPDATED_AT'
    SELECT DISTINCT UPDATED_AT 
    INTO variable_3 
    FROM TABLE1 limit 1;

    --RETURN 'Debug: variable_3 = ' || COALESCE(TO_CHAR(variable_3), 'NULL');

    -- Missing Property_IDs
    SELECT LISTAGG(Property_ID, ', ') 
    INTO variable_4
    FROM TABLE2 dim
    WHERE dim.Property_ID NOT IN (
        SELECT DISTINCT Property_ID 
        FROM TABLE1
    );

  --RETURN 'Debug: variable_4 = ' || variable_4 ;

    -- Check for issues and accumulate the error messages
    IF (variable_1 = 7) THEN
        error_messages := error_messages || 'The Customer Property Table is empty;' || CHR(10);  
    END IF;

  

    IF (variable_3 IS NULL OR variable_3 != CURRENT_DATE()) THEN
        error_messages := error_messages || 'The Updated_AT Column is not the Current Date;' || CHR(10);
    END IF;

    IF (variable_4 IS NOT NULL AND variable_4 != '') THEN
        error_messages := error_messages || 'Missing Properties detected: ' || variable_4 || ';' || CHR(10);  
    END IF;

   
    -- Raise the exception if there are errors
    IF (error_messages != '') THEN
        -- Raise the exception to be caught in the EXCEPTION block
        RAISE updated_at_is_not_current_date;
    ELSE
        RETURN 'No errors detected';  
    END IF;

EXCEPTION
    WHEN updated_at_is_not_current_date THEN
        -- Return the accumulated error messages if the exception is raised
        RETURN 'Following errors occurred: ' || error_messages;

    WHEN OTHER THEN
        -- Catch all other unexpected exceptions and provide context
        RETURN 'Unexpected error occurred. Please check the data and try again.';
END;
$$;

调用时的输出:

CALL PROPERTY_CONSISTENCY_CHECK() ;

enter image description here

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