November 30, 2007 at 1:27 pm
I am looking at the possibility of writing a trigger, which will fire on insert and assign each record being inserted with a unique record ID by adding 1 to the max RecID that is present in the Table.
Is this possible? If so how do I increment and assign the RecID field for each record in case more than one records are being inserted?
I am new to T-SQL. I have written this much
CREATE TRIGGER Trigger1
ON dbo.Master_Account
FOR INSERT
AS
BEGIN
DECLARE @MaxID int
SELECT @MaxID=MAX(RecID)+1 FROM dbo.Master_Account
END
I am not sure how to proceed from here.
Any help would be appreciated
danprem
November 30, 2007 at 1:53 pm
Is there a technical reason not to use an IDENTITY column in this application ?
November 30, 2007 at 2:06 pm
I do have RecID defined as the Primary Key field and datatype Int.
I initially had it defined with the UniqueIdentifier datatype, which was the only identity type that I could find on SQL Serv 2005 express. But didnt know how to populate it. So I changed it to int.
Is there a different solution?
November 30, 2007 at 2:08 pm
An int datatype can be given the IDENTITY property and will auto-increment for you.
November 30, 2007 at 2:14 pm
Thanks! That solved my problem
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply