It is necessary when adding an entry to the table, calculate the last column.

For example, if inserted

(ID | UserName | CreatedTime | AsText)

then insert value set

DECLARE @new_str = 'Новое сообщение: ' + UserName + ', создано: '+CreatedTime; AsText = @new_str 

The remaining values ​​remain the same. Maybe there is an example! Thank!



    1 answer 1

     CREATE TRIGGER SomeTrigger ON SomeTable AFTER INSERT AS DECLARE @new_str nvarchar(32); SET @new_str = 'Новое сообщение: '+ (SELECT UserName FROM inserted) + ', создано: '+(SELECT CreatedTime FROM inserted); UPDATE SomeTable SET AsText = @new_str FROM inserted WHERE SomeTable.Id = inserted.Id; 
    • Maybe there will be comments. - michael
    • one
      + (SELECT UserName FROM inserted) + will cause an error when inserting multiple records. it will be easier in my opinion UPDATE SomeTable SET AsText = LEFT ('New message:' + SUSER_SNAME () + ', created:' + CAST (GETDATE () AS VARCHAR, 32) FROM inserted WHERE SomeTable. renegator
    • one
      yeah, missed closing bracket on CAST - renegator
    • Yes, really, it just confused me, inserted returns a collection of inserted values, you are right. Just the first option would suit me too, since inserting multiple values ​​should not be possible. But this is not the right approach for setting limits. Thank. - michael