create trigger IN_T on T after insert as update T set ID2=i.ID from inserted i on t.ID=i.ID and t.ID2 is null
--参考, 不知对不对 create table T ( [ID] int identity, [Name] nvarchar(10), [ID2] as @@identity )insert T([Name]) values('A') insert T([Name]) values('B')select * from T
可用计算列 create table #tb ( id int identity(1,1), tname varchar(30), id2 as id ) insert into #tb select 'test' select * from #tb
if object_id('test')is not null drop table test go create table test (ID int identity(1,1),Name varchar(10),Num as ID) insert test(Name) values('Stone') insert test(name) values('Happy') go select * from test所影响的行数为 1 行) (所影响的行数为 1 行)ID Name Num 1 Stone 1 2 Happy 2(所影响的行数为 2 行)
為null時,更新為主鍵
after insert
as
update T
set ID2=i.ID
from
inserted i on t.ID=i.ID and t.ID2 is null
--参考, 不知对不对
create table T
(
[ID] int identity,
[Name] nvarchar(10),
[ID2] as @@identity
)insert T([Name])
values('A')
insert T([Name])
values('B')select * from T
create table #tb
(
id int identity(1,1),
tname varchar(30),
id2 as id
)
insert into #tb select 'test'
select * from #tb
------------------------------------------------
多了触发器,肯定是要花费一些cpu时间的,就是插入时。
drop table test
go
create table test (ID int identity(1,1),Name varchar(10),Num as ID)
insert test(Name) values('Stone')
insert test(name) values('Happy')
go
select * from test所影响的行数为 1 行)
(所影响的行数为 1 行)ID Name Num
1 Stone 1
2 Happy 2(所影响的行数为 2 行)