begin tran
create table t
(id int identity(1,1) primary key,name varchar(10),fatherid int)
go
insert into t(name,fatherid)
select 'test',isnull(max(id),0)+1 from tinsert into t(name,fatherid)
select 'test',max(id)+1 from tselect * from t
rollback
create table t
(id int identity(1,1) primary key,name varchar(10),fatherid int)
go
insert into t(name,fatherid)
select 'test',isnull(max(id),0)+1 from tinsert into t(name,fatherid)
select 'test',max(id)+1 from tselect * from t
rollback
for insert
as update r set b=@@identity where id=@@identity
@@IDENTITY
返回最后插入的标识值
@@IDENTITY 的值不会被其他用户的插入语句影响,但会被触发器影响
insert into mytable values(name,title,fatherid )
select @id = @@identity
update mytable set fatherid = @id where id = @@id
insert into t(name,fatherid)
select 'test',max(id)+1 from t要不然你注就用楼上两位的方法,要插入数据,再更新fatherid的值,这梓用到两条语句,当然,如果数据量大的情况下,楼上两位的方法好过我的方法,我当时帮你考虑的是怎样用一条语句摘定
这违反了RDBMS的原则!