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