PostgreSQL:比较 NULL 和正常值
PostgreSQL: compare NULL and normal value
我在 PostgreSQL 中有一个触发器
CREATE OR REPLACE FUNCTION table_update_func_pk1() RETURNS trigger AS $$
DECLARE
ri RECORD;
old_value TEXT;
new_value TEXT;
BEGIN
FOR ri IN
SELECT column_name FROM information_schema.columns
WHERE
table_schema = quote_ident('public')
AND table_name = quote_ident(TG_TABLE_NAME)
ORDER BY ordinal_position
LOOP
EXECUTE 'SELECT ().' || ri.column_name || '::text' INTO new_value USING NEW;
EXECUTE 'SELECT ().' || ri.column_name || '::text' INTO old_value USING OLD;
IF new_value <> old_value AND ri.column_name != 'update_by' THEN
INSERT INTO protokoll(datetime, operation, tabelle, field, pk1, old_value, new_value, update_by)
VALUES(now(), TG_OP, TG_TABLE_NAME, ri.column_name, NEW.cfg, old_value, new_value, NEW.update_by);
END IF;
END LOOP;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
有时如果 old_value 从 NULL 更改为正常值(或从正常值更改为 NULL),条件 "new_value <> old_value" 不为真但未知。想请问一下,有一个方法可以在case中得到true。谢谢
if new_value IS DISTINCT FROM old_value and ri.column_name <> 'update_by' then
...
end if;
我在 PostgreSQL 中有一个触发器
CREATE OR REPLACE FUNCTION table_update_func_pk1() RETURNS trigger AS $$
DECLARE
ri RECORD;
old_value TEXT;
new_value TEXT;
BEGIN
FOR ri IN
SELECT column_name FROM information_schema.columns
WHERE
table_schema = quote_ident('public')
AND table_name = quote_ident(TG_TABLE_NAME)
ORDER BY ordinal_position
LOOP
EXECUTE 'SELECT ().' || ri.column_name || '::text' INTO new_value USING NEW;
EXECUTE 'SELECT ().' || ri.column_name || '::text' INTO old_value USING OLD;
IF new_value <> old_value AND ri.column_name != 'update_by' THEN
INSERT INTO protokoll(datetime, operation, tabelle, field, pk1, old_value, new_value, update_by)
VALUES(now(), TG_OP, TG_TABLE_NAME, ri.column_name, NEW.cfg, old_value, new_value, NEW.update_by);
END IF;
END LOOP;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
有时如果 old_value 从 NULL 更改为正常值(或从正常值更改为 NULL),条件 "new_value <> old_value" 不为真但未知。想请问一下,有一个方法可以在case中得到true。谢谢
if new_value IS DISTINCT FROM old_value and ri.column_name <> 'update_by' then
...
end if;