How to know if anything was updated ?
Various options. In a plpgsql function, you can check the special variable FOUND
to see if the last SQL command affected any rows.
IF FOUND THEN ...
However, For dynamic queries with EXECUTE
use GET DIAGNOSTICS
instead. The manual:
Note in particular that EXECUTE
changes the output of GET DIAGNOSTICS
,
but does not change FOUND
.
Related:
Aside:
I see lingering problems with properly escaped identifiers (especially with upper case table names like you have), possibly even SQL injection. Fix with:
DECLARE
v_table_name text;
v_column_name text;
v_id_lock_usr integer; -- guessing the data type
i integer;
BEGIN
SELECT nm_table_name, replace(nm_table_name, 'CDB_', 'ID_')
INTO strict v_table_name, v_column_name
FROM m_entity e
WHERE id = 12;
EXECUTE format('UPDATE %I SET id_lock_usr = null, dt_lock = null
WHERE %I = $1 RETURNING id_lock_usr'
, v_table_name,
, v_column_name)
INTO v_id_lock_usr; -- to store the *single* result from RETURNING
GET DIAGNOSTICS i = ROW_COUNT;
IF i > 0 THEN
-- do something
END IF;
END
Note %I
instead of %s
.
In your case, if id_lock_usr
returned by your query is NOT NULL
(reliably), you might just test the result directly instead:
IF v_id_lock_usr IS NOT NULL ...
And you might want to schema-qualify table names to avoid ambiguities. But you must escape schema_name.table_name
as two separate identifiers ..
Related: