Adding Record IDs to records being inserted

  • 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

  • Is there a technical reason not to use an IDENTITY column in this application ?

  • 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?

  • An int datatype can be given the IDENTITY property and will auto-increment for you.

  • 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