SQL Server - after insert trigger - update another column in the same table

后端 未结 7 850
一向
一向 2020-12-04 22:27

I\'ve got this database trigger:

CREATE TRIGGER setDescToUpper
ON part_numbers
 AFTER INSERT,UPDATE
AS
DECLARE @PnumPkid int, @PDesc nvarchar(128)

SET @Pnum         


        
7条回答
  •  孤街浪徒
    2020-12-04 23:13

    Use a computed column instead. It is almost always a better idea to use a computed column than a trigger.

    See Example below of a computed column using the UPPER function:

    create table #temp (test varchar (10), test2 AS upper(test))
    insert #temp (test)
    values ('test')
    select * from #temp
    

    And not to sound like a broken record or anything, but this is critically important. Never write a trigger that will not work correctly on multiple record inserts/updates/deletes. This is an extremely poor practice as sooner or later one of these will happen and your trigger will cause data integrity problems asw it won't fail precisely it will only run the process on one of the records. This can go a long time until someone discovers the mess and by themn it is often impossible to correctly fix the data.

提交回复
热议问题