Mike Viens writes "The record structure--------------------exhibCatID - (primary key)description - (description)parentExhibCatID - (parent exhibCatID or null)compositeID - (parent compositeID + "." + description)isActive - (0 or 1)
I need to be able to select and order fields by the correct parent/child order, then sorted alphabetically by description, which is only a small sub-select of a much larger, more complicated, multi-joined query.On insert/update, I need to take the value of a field, and create a composite ID out of it. The target field is a varchar(5000). When a field is inserted/updated (with no parent), then composite = description. When a field is inserted/updated (with a parent), then composite = parentDesc + description.I have this working as expected. However, it fails when the transaction contains more than one record. I understand why the code fails and am very willing to fix it, but I need an example of how to loop through all records in a transaction.Below is the version that works for a single record. Could you please advise me the best way to do this?CREATE TRIGGER exhib_categories_update_trig ON dbo.exhib_categoriesFOR INSERT, UPDATE ASDECLARE @currentRecord varchar(20), @currentRecordID int, @currentDescription varchar(100), @currentParentExhibCatID intDECLARE @parentCompositeID varchar(5000), @newCompositeID varchar(5000)SET @currentRecordID = (SELECT exhibCatID FROM inserted)SET @currentRecord = CAST(@currentRecordID AS varchar(20))SET @currentDescription = (SELECT description FROM inserted)SET @currentParentExhibCatID = (SELECT parentExhibCatID FROM inserted)SET @parentCompositeID = (SELECT ec.compositeID FROM inserted i, exhib_categories ec WHERE ec.exhibCatID = i.parentExhibCatID)IF @currentParentExhibCatID IS NULL BEGIN SET @newCompositeID = UPPER(@currentDescription) ENDELSE BEGIN SET @newCompositeID = @parentCompositeID + '.' + UPPER(@currentDescription) ENDUPDATE exhib_categories SET compositeID = @newCompositeID WHERE exhibCatID = @currentRecord
"