I am trying to create an auto sequence trigger for my UniqueID field in SQL Server that is dependent on multiple fields. For example I have a table called animalswith columns Species, Sex, UniqueID. I am trying to create a trigger for when species and sex is entered the UniqueID field is auto populated based on the last sequential number for that species and sex. Needs to look like DeerM0001.This is what I have so far but it doesn't work.
CREATE TRIGGER tr_animalID
ON animal
AFTER INSERT, UPDATE
AS
BEGIN
SET NOCOUNT ON;
DECLARE @ID varchar(8)
SET @ID = (SELECT max(Species) FROM animal);
UPDATE animal
SET UniqueID = CONCAT(Species, Sex) +0001
WHERE UniqueID = @ID;
END
GO