ACTIVITY_COUNT inside SELECT/SET INTO VARIABLE requires manual fix
Severity
Low
Description
The ACTIVITY_COUNT status variable returns the number of rows affected by an SQL DML statement in an embedded SQL or stored procedure application. For more information check here.
As explained in its translation specification, there is a workaround to emulate ACTIVITY_COUNT's behavior through:
SELECT $1FROMTABLE(RESULT_SCAN(LAST_QUERY_ID()));
When using ACTIVITY_COUNT in a SELECT/SET INTO VARIABLE statement, it can not be simply replaced by the workaround mentioned above.
Example Code
Teradata
IN -> Teradata_01.sql
REPLACEPROCEDURE InsertEmployeeSalaryAndLog_4 ()BEGINDECLARE rowCount INT;DECLAREmessageVARCHAR(100);INSERT INTO employees (employee_id, first_name, last_name, department_id, salary)VALUES (101, 'Alice', 'Smith', 10, 70000.00);SELECT ACTIVITY_COUNT INTO rowCount;SETmessage='ROWS INSERTED: '|| rowCount;-- Insert the ACTIVITY_COUNT into the activity_log tableINSERT INTO activity_log (operation, row_count)VALUES (message, rowCount);END;
Instead of using the complete query, it needs to be adapted manually to Snowflake's SELECT INTO VARIABLE syntax.
Furthermore, if RESULT_SCAN(LAST_QUERY_ID()) is giving incorrect results, check SSC-FDM-TD0033 for how to handle possible limitations of using LAST_QUERY_ID.
Recommendations
Manually adapt the proposed workaround.
Check SSC-FDM-TD0033 for how to handle possible limitations of using LAST_QUERY_ID.