I have a table that constantly has rows inserted, and one of the fields is always empty. I want to create an Insert trigger on the table and populate the empty field with data extracted from several of the other fields.
CREATE TRIGGER tr_PutInValue ON MyTable FOR INSERT AS
update INSERTEDset EmptyField = DataField1 + ':' + DataField2 + ':' + GetDate()
But when I try to create this trigger, SQL tells me
Server: Msg 286, Level 16, State 1, Procedure trI_syslog, Line 119The logical tables INSERTED and DELETED cannot be updated.
If I can't modify values in the Inserted table, then how can I achieve what I want?
inserted/updated tables are just views of the log. when you want to update the value in the record, update against the real table name joining to "inserted" to identify which rows were modified.
But it would be better to do what you want done in the insert statement.
Thanks for the advice, John. The inserts are being done by a 3rd-party application that I have no control over, so hence using the trigger.
Unfortunately the table has no indexes, so to join INSERTED with the main table in an insert trigger I believe would cause a huge performance hit (the main table will have hundred of thousands of rows, and has no distinguishable unique fields to use for a primary key)
Most likely candidate for an index is the DateTime field called DateTimeLocal. It is not unique, but the most rows with identical values I've seen so far is less than 100, so a non-unique index on this field could work.
Wish me luck!
Why don't you try to add an identity column?
For best performance make it a clustered primary key.