I’m trying to create a snowflake procedure in SQL which takes two inputs (first is table_name and second is object where key is column_name and value is 1,2,3 etc.,). When the value is 1, then it should check if there are more than 10% null values in that column and return message based on successful and failed validations.
CREATE OR REPLACE PROCEDURE
GARDEN_PLANTS.VEGGIES.MY_PROC(table_name VARCHAR,
rule_object OBJECT)
RETURNS VARCHAR
LANGUAGE SQL
EXECUTE AS CALLER
AS
DECLARE
column_name VARCHAR;
rule_value INTEGER;
null_percent FLOAT;
total_rows FLOAT;
null_count FLOAT;
null_query CURSOR FOR SELECT COUNT(*) FROM table_name WHERE column_name IS NULL;
total_query CURSOR FOR SELECT COUNT(*) FROM table_name;
BEGIN
FOR column_name IN OBJECT_KEYS(rule_object)
DO
rule_value := GET(rule_object, column_name)::INT;
OPEN null_query;
FETCH null_query INTO null_count;
OPEN total_query;
FETCH total_query INTO total_rows;
null_percent := (null_count/total_rows)*100;
IF (rule_value = 1) THEN
IF (null_percent > 10) THEN
RETURN 'More than 10% null values.';
ELSE
RETURN 'More than 10% null values.';
END IF;
END IF;
CLOSE total_query;
CLOSE null_query;
END FOR;
RETURN 'No valid rule';
END;
CALL GARDEN_PLANTS.VEGGIES.MY_PROC(‘ROOT_DEPTH’, OBJECT_CONSTRUCT(‘ROOT_DEPTH_CODE’, 1))
Root depth tables has ROOT_DEPTH_CODE column where there are more than 50% nulls. But when I’m calling the procedure I’m getting key error.
Pandit Abilash is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.