Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

insert triggers urgent

Status
Not open for further replies.

mkey

Programmer
Oct 3, 2001
288
CA
Hi all,
i need to create an insert trigger where everytime there is an insert on the table i want the recordid column to go old value plus one.
my trigger look something like this but its not working properly!

CREATE TRIGGER TRGname ON table_name
FOR INSERT
AS
declare @id int
select @id = max(recordid) from table_name
update table_name set recordid = @id + 1

Any input will help!
 
You dont need to generate your own record id. If you recordID as an identity field, SQL Server will do automatic increments.
 
well I knew that. But my requirement is such that i need to write a trigger instead of identity!
 
Please do not use a trigger to do this. An identity column would work better, and makes the trigger unnecesary.
see BOL.



 
try this:

CREATE TRIGGER TRGname ON table_name
FOR INSERT
AS
declare @old int,
@new int
select @old = recordid from inserted
select @new = @old+1
update table_name set recordid = @new where Recordid = @old
 
I'm not sure why they want a trigger instead of identity.From what I understood was they are having some difficulty when accessing through another application.

Thank you for your help! I tried the trigger its still doesn't work!

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top