sql触发器问题——自动增长

来源:百度知道 编辑:UC知道 时间:2024/06/30 04:40:08
表test1(autoid,A)
表test2(autoid,A,B)

写一触发器:
当表test2的B值修改成1的时候,把表test2的A值插入表test1中

问题:
表test1的 autoid 是主键、int型,但自动增长没有打开,
插入的时候不好插入

此触发器如何写??
数据库为sql server 2000
autoid 取最大值 然后加1 除了此种方法 还有别的方法吗??
有什么办法 用代码打开 自动增长

huzi01245朋友的count(autoid)+1 同时更改多条记录b=1的时候 这样行吗?

oracle中不存在这种情况,你使用的何种数据库?

create trigger test on test2
for update
as
declare @oldcode int
declare @newcode int
declare @newno int
select @oldcode=B from deleted
select @newcode=B,@newno=A from inserted
if(@newcode=1 and @oldcode<>@newcode)
begin
insert into test1
select count(autoid)+1,@newno from test2
end
go

每次取test1表的autoid的最大值,然后添加不就行了?