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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need to create a counter trigger

Status
Not open for further replies.
Jul 25, 2000
33
0
0
US
Need to create a trigger that automatically increases the value in the flx_objid column by 1 when an insert occurs. Not quite sure how I would go about doing this as this will be my first trigger. Any help would be appreciated.
 

This is what I have so far.... I have tried several different codes and none will fire.



CREATE TRIGGER FLXTrans_ObjidCount
ON FLX_Transaction
FOR INSERT

AS

DECLARE @NewCount INT, @MaxCount int


IF (SELECT Count(*) FROM inserted WHERE flx_objid IS NULL) > 0
BEGIN
SELECT @MaxCount= (SELECT Max(flx_objid) FROM FLX_Transaction)

SELECT @NewCount = @MaxCount + 1

INSERT INTO FLX_Transaction
SELECT i.*
FROM Insereted i
WHERE flx_objid IS NULL


UPDATE FLX_Transaction SET flx_objid = @NewCount WHERE flx_objid IS NULL
END
GO
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top