在company表上建一个Insert触发器

解决方案 »

  1.   

    做个触发器,这个@@identity应该管用insert into log (cid) select @@identity
      

  2.   

    NewLog 是哪个表的视图?( Name)是哪个表的字段?
      

  3.   

    CREATE TRIGGER [myTrig] ON [dbo].[company] 
    FOR INSERT
    AS
    INSERT INTO [log](cid)
    select cid from Inserted
      

  4.   

    不好意思,log这张表应是这样的log(oid,cid,Uid),而其中的Uid是session("Uid")中得来的,若用楼主的方法做触发器,那请问Uid如何插入到log中?
      

  5.   

    如果你是用ASP的话,你可以用
    set nocount on;Insert into company(name) values (session("Uid")); select @@identity as cid用这句话得到刚Insert的记录的cid,接下来就可以把cid和session("Uid")再把都写到log表里了。
      

  6.   

    --示例--数据表
    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
    --*/
      

  7.   

    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)