如果你是用ASP的话,你可以用 set nocount on;Insert into company(name) values (session("Uid")); select @@identity as cid用这句话得到刚Insert的记录的cid,接下来就可以把cid和session("Uid")再把都写到log表里了。
--示例--数据表 create table company(cid int identity(1,1),name varchar(10)) create table [log](oid int identity(1,1),cid int,Uid int) go--视图 create view v_company_log as select a.name,b.uid from company a join [log]b on a.cid=b.cid go--处理的触发器 create trigger tr_insert on v_company_log instead of insert as declare @name varchar(10),@uid int declare tb cursor local for select name,uid from inserted open tb fetch tb into @name,@uid while @@fetch_status=0 begin insert company(name) values(@name) insert [log](cid,uid) values(scope_identity(),@uid) fetch tb into @name,@uid end close tb deallocate tb go--插入数据就要向视图中插入了 insert v_company_log select '张三',1 union all select '李四',2 go--显示插入结果 select * from v_company_log go--删除测试 drop table company,[log] drop view v_company_log/*--测试结果name uid ----- ----- 张三 1 李四 2 --*/
create table company (cid int identity(1,1),name varchar(30)) create table [log](oid int identity(1,1),cid int,uid int)insert into company select 'test'insert into [log] select @@identity,33select * from company select * from [log]--结果cid name ----------- ------------------------------ 1 test(1 row(s) affected)oid cid uid ----------- ----------- ----------- 1 1 33(1 row(s) affected)
FOR INSERT
AS
INSERT INTO [log](cid)
select cid from Inserted
set nocount on;Insert into company(name) values (session("Uid")); select @@identity as cid用这句话得到刚Insert的记录的cid,接下来就可以把cid和session("Uid")再把都写到log表里了。
create table company(cid int identity(1,1),name varchar(10))
create table [log](oid int identity(1,1),cid int,Uid int)
go--视图
create view v_company_log
as
select a.name,b.uid
from company a join [log]b on a.cid=b.cid
go--处理的触发器
create trigger tr_insert on v_company_log
instead of insert
as
declare @name varchar(10),@uid int
declare tb cursor local for
select name,uid from inserted
open tb
fetch tb into @name,@uid
while @@fetch_status=0
begin
insert company(name) values(@name)
insert [log](cid,uid) values(scope_identity(),@uid)
fetch tb into @name,@uid
end
close tb
deallocate tb
go--插入数据就要向视图中插入了
insert v_company_log select '张三',1
union all select '李四',2
go--显示插入结果
select * from v_company_log
go--删除测试
drop table company,[log]
drop view v_company_log/*--测试结果name uid
----- -----
张三 1
李四 2
--*/
create table [log](oid int identity(1,1),cid int,uid int)insert into company select 'test'insert into [log] select @@identity,33select * from company
select * from [log]--结果cid name
----------- ------------------------------
1 test(1 row(s) affected)oid cid uid
----------- ----------- -----------
1 1 33(1 row(s) affected)