简体   繁体   English

当数据插入到同一表中具有相同值的另一个cloum时,如何在postgres中编写触发器以更新另一列?

[英]How To Write a trigger in postgres to update another column when data inserted into another cloum with same value in same table?

Please Find below Table structure:-请查找下表结构:-

CREATE TABLE emp (
    empname  text NOT NULL,
    salary  integer,
    salary1 integer
);

Whenever i will insert data into emp table using below query salary1 column should be filled automatically using trigger in postgres ?每当我使用以下查询将数据插入 emp 表时,应该使用 postgres 中的触发器自动填充salary1 列?

insert into emp(empname,salary) values('Tarik','1200');

I have tried below code,But it is not working for me.我试过下面的代码,但它对我不起作用。

CREATE OR REPLACE FUNCTION employee_insert_trigger_fnc()
  RETURNS trigger AS
$$
BEGIN
    update emp set salary1=NEW.salary where NEW.salary=NEW.salary;
RETURN NEW;
END;
$$
LANGUAGE 'plpgsql';

create TRIGGER employee_insert_trigger
AFTER Insert
 ON emp
FOR EACH ROW
EXECUTE PROCEDURE employee_insert_trigger_fnc();

Please help.. Thanks in advance.请帮助.. 提前致谢。

Issue resolved after my R&D:-我的研发后问题解决了:-

CREATE OR REPLACE FUNCTION employee_insert_trigger_fnc()
  RETURNS trigger AS
$$
BEGIN
        NEW.salary1 := NEW.salary;
RETURN NEW;
END;
$$
LANGUAGE 'plpgsql';

create TRIGGER employee_insert_trigger
BEFORE INSERT OR UPDATE
 ON ami_master.emp
FOR EACH ROW
EXECUTE PROCEDURE employee_insert_trigger_fnc();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 更新表上每个新插入的(同一行)记录的列取决于另一个没有触发器和默认约束的列值 - Update a column of each and every newly inserted (same row) record on a table depends on another column value without Trigger and default constraint 当另一个表被更新时触发更新同一表中的值 - Trigger to update a value in same table when another is updated 当数据从不同的数据库插入另一个表时触发更新表列 - Trigger to update table column when data is inserted in another table from a different database 如何更新一列以匹配同一表中另一列的数据 - How to update a column to match data from another column in the same table 更改另一个表中的同一列时如何更新列?(PostgreSQL) - How to update a column when the same column in another table is changed ?(postgresql) Oracle触发器更新列,包含来自同一表中另一列的信息 - Oracle Trigger update column with information from another column in the same table 触发更新同一表中另一列的插入或更新列 - trigger to update a column on insertion or updation of another column in same table 插入后使用来自另一个表的数据触发更新同一表 - Trigger update the same table with data from another table after insert 检查列值并更新同一表中的另一个列值 - Check column value and update another column value from same table 如何编写SQL语句从同一表中的另一列更新表中的列? - How can I write a SQL statement to update a column in a table from another column in the same table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM