简体   繁体   English

更新表上每个新插入的(同一行)记录的列取决于另一个没有触发器和默认约束的列值

[英]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

I have a table with an identity column (say Column1 ) and an integer column Column2 (nullable) 我有一个带有标识列(例如Column1 )和整数列Column2 (可为空)的表

While inserting a row into this table, if the value passed to the Column2 is null, copy the Column1 value. 在向该表中插入一行时,如果传递给Column2值为null,则复制Column1值。 If not null value is passed as argument for column2 , retain the given value. 如果不将null值作为column2参数传递,则保留给定值。

Only after the every insert, this check and update should be done. 仅在每次插入之后,才应进行此检查和更新。

But as per the instruction given to me, should not use default constraint and trigger (after insert). 但是按照给我的指示,不应使用默认约束和触发器(插入后)。

This is for audit purpose. 这是出于审计目的。 The record from this table will be moved/switched from one table to another table. 该表中的记录将从一个表移动/切换到另一表。 The volume and transaction is very high (billions of records). 交易量和交易量很高(数十亿条记录)。

Can you help me on this? 你能帮我吗?

Thanks, Baskaran 谢谢,Baskaran

Insert into tablename (column1, columns2) Values (value1, ISNULL(Value2,value1))

我认为这是您所期望的。

If you really want to do this in a trigger, try something like this: 如果您真的想在触发器中执行此操作,请尝试以下操作:

CREATE TRIGGER TrgAfterINsert
ON dbo.YourTable
AFTER INSERT
AS
    UPDATE t
    SET Column2 = i.Column1
    FROM dbo.YourTable t
    INNER JOIN Inserted i ON i.Column1 = t.Column1
    WHERE t.Column2 IS NULL

This basically updates the YourTable table after an INSERT , and any rows that were inserted with a NULL in column2 are updated so that column2 is equal to the column1 value for that row 这基本上是在INSERT之后更新YourTable表,并且在column2中插入了NULL任何行都将更新,以使column2等于该行的column1

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

相关问题 Oracle触发器-新插入记录的更新列 - Oracle trigger - update column on newly inserted record 当数据插入到同一表中具有相同值的另一个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? 使用同一表中另一列的值更新每一行 - Update every row with a value from another column from same table 如何使用来自同一表中另一特定行的列值更新sql表中每一行的列 - How to update a column of every row in a sql table with the value of that column from another specific row in the same table 使用另一个表上的值更新新添加的列 - Update newly added column with value on another table Oracle中每个记录更新的默认值列 - Column with default value for every record update in Oracle 插入表中,而不必为每一行指定每个列值 - Insert into table without having to specify every column value for each row 我们可以插入一张表并通过查看其他表的值来更新插入行的一列吗? - Can we insert in one table and trigger to update the inserted row's one column by looking at other table's value for the id being inserted? 如何使用触发器更新新插入元组的列 - How to use trigger to update column for newly inserted tuple 每次插入一行时的SQL更新列 - Sql update column every time a row is inserted
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM