I have a logging table that needs a log id, but I cannot use the id column because the log id is part of the combined key.
create table StuffLogs { StuffID int LogID int Note varchar(255) }
There is a combination key for StuffID and LogID .
I want to create an insert trigger that calculates the next LogID when inserting log entries. I can do this for one record at a time (see below to find out how the LogID calculated), but it is not very efficient, and I hope there is a way to do this without cursors.
select @NextLogID = isnull(max(LogID),0)+1 from StuffLogs where StuffID = (select StuffID from inserted)
The end result should allow me to insert any number of records into StuffLogs with automatic calculation of the LogID column.
StuffID LogID Note 123 1 foo 123 2 bar 456 1 boo 789 1 hoo
Inserting another record using StuffID: 123, Note: bop will result in the following record:
StuffID LogID Note 123 3 bop
source share