trigger instead of identity
-
i have a column its values must started with 1 and incrementing by 1 this column repeatly looses his rows so i can not use identity here so i tried to raise a trigger to do the job but i have some probelems with the following statments create trigger dbo.insertNum on testIcreTrigger --table name for insert AS begin declare @insVal int --value to insert declare @MaxVal int select @MaxVal= Max(Num) from hitTheatre select @insVal=@MaxVal+1 insert into testIcreTrigger (Num) values (@insVal) End Go
-
i have a column its values must started with 1 and incrementing by 1 this column repeatly looses his rows so i can not use identity here so i tried to raise a trigger to do the job but i have some probelems with the following statments create trigger dbo.insertNum on testIcreTrigger --table name for insert AS begin declare @insVal int --value to insert declare @MaxVal int select @MaxVal= Max(Num) from hitTheatre select @insVal=@MaxVal+1 insert into testIcreTrigger (Num) values (@insVal) End Go
-
i have a column its values must started with 1 and incrementing by 1 this column repeatly looses his rows so i can not use identity here so i tried to raise a trigger to do the job but i have some probelems with the following statments create trigger dbo.insertNum on testIcreTrigger --table name for insert AS begin declare @insVal int --value to insert declare @MaxVal int select @MaxVal= Max(Num) from hitTheatre select @insVal=@MaxVal+1 insert into testIcreTrigger (Num) values (@insVal) End Go
-
i have a column its values must started with 1 and incrementing by 1 this column repeatly looses his rows so i can not use identity here so i tried to raise a trigger to do the job but i have some probelems with the following statments create trigger dbo.insertNum on testIcreTrigger --table name for insert AS begin declare @insVal int --value to insert declare @MaxVal int select @MaxVal= Max(Num) from hitTheatre select @insVal=@MaxVal+1 insert into testIcreTrigger (Num) values (@insVal) End Go
You should replace *
for insert
* with *INSTEAD OF INSERT
*. WhenFOR
is the only keyword specified,AFTER
is used by default.CREATE TRIGGER dbo.insertNum
ON testIcreTrigger
INSTEAD OF INSERT
AS
BEGIN
INSERT INTO testIcreTrigger (Num)
SELECT MAX(Num)+1 FROM hitTheatre
END