以 Trigger 实现 ID 自动编号
Sql Server 的自动编号用的是16位的长整形字段,我欲以一个4为长的整形数来实现自动编号,用触发器实现之。
Qxk 表有一个字段 ID,就以它实现自动编号。
CREATE TRIGGER atuo_id ON dbo.qxk
FOR INSERT
AS
declare @m int
declare get_last cursor scroll dynamic for
select id from qxk where [id] is null
for update
select @m = max([id]) from qxk
if(@m is null ) set @m=1
else set @m = @m + 1
open get_last
fetch last from get_last
update qxk set [id] = @m
where current of get_last
close get_last
deallocate get_last
FOR INSERT
AS
declare @m int
declare get_last cursor scroll dynamic for
select id from qxk where [id] is null
for update
select @m = max([id]) from qxk
if(@m is null ) set @m=1
else set @m = @m + 1
open get_last
fetch last from get_last
update qxk set [id] = @m
where current of get_last
close get_last
deallocate get_last