plpgsql 查询 - 我如何将 if , then 与 update 结合起来?
plpgsql query - how can i combine if , then with update?
我有以下查询:
create or replace function test_function()
returns trigger as $body$
begin
if (tg_table_name = 'mytable' and tg_op='insert') then
INSERT into another_table(id, name) values(new.id, new.name);
return new;
else if (tg_table_name = 'mytable' and tg_op='update') then
INSERT into another_table(id, name) values(new.id, new.name);
return new;
end if;
return null;
end;
$body$ language plpsql;
编辑:
我需要做一个 :
update table mytable set name = 'test';
EDIT2:这没有用,value2 没有更新为 null;
BEGIN
IF (TG_OP = 'INSERT' AND TG_TABLE_NAME='tableA') THEN
INSERT INTO tableB(
columnA,
columnB)
VALUES(
new.value1,
new.value2);
new.value2 := null;
RETURN NEW;
ELSEIF (TG_OP = 'UPDATE' AND
TG_TABLE_NAME='tableA') THEN
INSERT INTO tableB(
columnA,
columnB)
VALUES(
new.value1,
new.value2);
new.value2 := null;
RETURN NEW;
END IF;
RETURN null;
END;
编辑:4
create trigger add_notif_trigger after insert or update on
mytable for each row when (new.name > 100) execute
procedure function test_function();
我需要根据任一情况进行更新。我不知道该怎么做。
亲切的问候,
如果要修改基础 table 的列值,则不需要再次更新,只需分配 NEW.column_name
if
..
NEW.name := 'test';
else if
..
NEW.name := 'test';
如果那么
如果 new.value = 真 那么
如果结束;
如果结束;
小心,
当你使用 update(TG_OP ='UPDATE') 你有旧值和新值,在插入期间你只有新值
我有以下查询:
create or replace function test_function()
returns trigger as $body$
begin
if (tg_table_name = 'mytable' and tg_op='insert') then
INSERT into another_table(id, name) values(new.id, new.name);
return new;
else if (tg_table_name = 'mytable' and tg_op='update') then
INSERT into another_table(id, name) values(new.id, new.name);
return new;
end if;
return null;
end;
$body$ language plpsql;
编辑: 我需要做一个 :
update table mytable set name = 'test';
EDIT2:这没有用,value2 没有更新为 null;
BEGIN
IF (TG_OP = 'INSERT' AND TG_TABLE_NAME='tableA') THEN
INSERT INTO tableB(
columnA,
columnB)
VALUES(
new.value1,
new.value2);
new.value2 := null;
RETURN NEW;
ELSEIF (TG_OP = 'UPDATE' AND
TG_TABLE_NAME='tableA') THEN
INSERT INTO tableB(
columnA,
columnB)
VALUES(
new.value1,
new.value2);
new.value2 := null;
RETURN NEW;
END IF;
RETURN null;
END;
编辑:4
create trigger add_notif_trigger after insert or update on
mytable for each row when (new.name > 100) execute
procedure function test_function();
我需要根据任一情况进行更新。我不知道该怎么做。
亲切的问候,
如果要修改基础 table 的列值,则不需要再次更新,只需分配 NEW.column_name
if
..
NEW.name := 'test';
else if
..
NEW.name := 'test';
如果那么 如果 new.value = 真 那么 如果结束;
如果结束;
小心, 当你使用 update(TG_OP ='UPDATE') 你有旧值和新值,在插入期间你只有新值