I have this table in SnowFlake:
What I want to do is to incrementally update the row_id by internal_id. E.g. for internal_id = CHE20220708134003004472, the row_id should take the values from 1 to 3 respectively, and so on for the other ids.
Here is an example of the desired output:
I've tried to do that by using the following code:
execute immediate $$
declare
counter integer default 1;
total_rows integer default (SELECT COUNT(*) FROM "DB_MX_DEV"."STAGING"."stg_chedraui_inv_day" WHERE internall_id = 'CHE20220708134003004472');
begin
for i in 1 to total_rows do
counter := counter + 1;
UPDATE "DB_MX_DEV"."STAGING"."stg_chedraui_inv_day" SET row_id = counter where internall_id = 'CHE20220708134003004472';
end for;
return counter;
end;
$$;
However, I got this error:
Uncaught exception of type 'STATEMENT_ERROR' on line 8 at position 4 : SQL compilation error: error line 1 at position 65 invalid identifier 'COUNTER'
Note: At the moment, the code above is only trying to update the row_id for a specific internal_id, I'm still trying to figure out how to do it for all ids.


UPDATEstatement, and set the ids usingROW_NUMBER() OVER().