cmd="insert into DutyPlan (Title,Author,AuthorDept,AuthorDutyCode,timestart,DutyMan,DutyManDutyCode,DutyManDept,
DutyLeader,DutyLeaderDutyCode,DutyLeaderDept,DutyTopLeader,DutyTopLeaderDutyCode,DutyTopLeaderDept,StartTime,LinkPage,Cata)"
+"values ('"+title+"','"+author+"','"+authordept+"','"+authordutycode+"','"+timestart+"','"+dutyman+"','"+dutymandutyCode+"','"+dutymandept+"','"+dutyleader+"','"+dutyleaderdutyCode+"','"+dutyleaderdept+"','"+dutytopleader+"','"+dutytopleaderdutycode+"','"+dutytopleaderdept+"','"+starttime+"','"+LinkPage+"','通知')";
我在*.aspx.cs文件里写了一条插入语句
现在对DutyPlan 表写了一个触发器
我的目的是当这条插入语句执行的时候
分别给DutyMan,DutyLeader,DutyTopLeader三个人触发一条insert语句
我分别写了三个触发器:
第一个:
CREATE TRIGGER [dutyplan1] ON dbo.DutyPlan 
FOR INSERT
AS 
insert into PendingDoc (Title,StartTime,Author,AuthorDutyCode,Dept,CurrentOper,CurrentOperDept,CurrentOperDutyCode,LinkPage,LinkMatchID,Cata)
select title,timestart,Author,AuthorDutyCode,Authordept,DutyMan,DutyManDept,DutyManDutyCode,LinkPage,DutyPlanID,Cata from DutyPlan where DutyPlanID =@@identity
第二个:
CREATE TRIGGER [dutyplan2] ON dbo.DutyPlan 
FOR INSERT
AS 
insert into PendingDoc (Title,StartTime,Author,AuthorDutyCode,Dept,CurrentOper,CurrentOperDept,CurrentOperDutyCode,LinkPage,LinkMatchID,Cata)
select title,timestart,Author,AuthorDutyCode,Authordept,DutyLeader,DutyLeaderDept,DutyLeaderDutyCode,LinkPage,DutyPlanID,Cata from DutyPlan where DutyPlanID =@@identity
第三个:
CREATE TRIGGER [dutyplan3] ON dbo.DutyPlan 
FOR INSERT
AS 
insert into PendingDoc (Title,StartTime,Author,AuthorDutyCode,Dept,CurrentOper,CurrentOperDept,CurrentOperDutyCode,LinkPage,LinkMatchID,Cata)
select title,timestart,Author,AuthorDutyCode,Authordept,DutyTopLeader,DutyTopLeaderDept,DutyTopLeaderDutyCode,LinkPage,DutyPlanID,Cata from DutyPlan where DutyPlanID =@@identity但是结果只是对第一条有用
谁能指点我一下,真是万分感激!

解决方案 »

  1.   

    trigger的声明都一样,为什么不写在一起??
      

  2.   

    因为我的CurrentOper,CurrentOperDept,CurrentOperDutyCode对应的是:
    第一条的:DutyMan,DutyManDept,DutyManDutyCode
    第二条的:DutyLeader,DutyLeaderDept,DutyLeaderDutyCode
    第三条的:DutyTopLeader,DutyTopLeaderDept,DutyTopLeaderDutyCode
    怎么写在一起阿??
    每天要排值班计划表,包括值班员,值班处长,值班部首长
    每排一条计划,就给这三个人各自触发一条通知
    怎么写阿?
      

  3.   

    不需要数据库触发器。你可以建立一个事务,排了一条计划,就马上进行发通知的操作。----------------
    如果要写触发器的话,你就是三条简单的insert么
    完全可以写在一个触发器里面:
    insert ....;
    insert ...;
    insert ...;
      

  4.   

    也不建议用触发器,同意 jiezhi(風依舊) 、brightheroes(闭关|那一剑的风情)
      

  5.   

    我想好的办法是在后台代码中不要直接使用sql语句,使用存储过程既方便又快捷,并且在存储过程中可以实现你所想要做的事情,就是往其他表插入数据。 。。不是很简单的吗?
      

  6.   

    我也是刚刚开始用.NET,哪位高手可以告诉我事物怎么写阿?能不能具体点,感谢!!
      

  7.   

    尽量不要使用触发器,使用事务就可以处理了,而且你的触发器写一个里就行了.ASP.NET里的事务处理:
    http://www.yesky.com/20030402/1660720.shtml
      

  8.   

    在一个触发器里面写三个insert好像不行,还是只触发了第一条阿
      

  9.   

    CREATE TRIGGER [dutyplan1] ON dbo.DutyPlan 
    FOR INSERT
    AS 
    insert into PendingDoc (Title,StartTime,Author,AuthorDutyCode,Dept,CurrentOper,CurrentOperDept,CurrentOperDutyCode,LinkPage,LinkMatchID,Cata)
    select title,timestart,Author,AuthorDutyCode,Authordept,DutyMan,DutyManDept,DutyManDutyCode,LinkPage,DutyPlanID,Cata from DutyPlan where DutyPlanID =@@identity;insert into PendingDoc (Title,StartTime,Author,AuthorDutyCode,Dept,CurrentOper,CurrentOperDept,CurrentOperDutyCode,LinkPage,LinkMatchID,Cata)
    select title,timestart,Author,AuthorDutyCode,Authordept,DutyLeader,DutyLeaderDept,DutyLeaderDutyCode,LinkPage,DutyPlanID,Cata from DutyPlan where DutyPlanID =@@identity;insert into PendingDoc (Title,StartTime,Author,AuthorDutyCode,Dept,CurrentOper,CurrentOperDept,CurrentOperDutyCode,LinkPage,LinkMatchID,Cata)
    select title,timestart,Author,AuthorDutyCode,Authordept,DutyTopLeader,DutyTopLeaderDept,DutyTopLeaderDutyCode,LinkPage,DutyPlanID,Cata from DutyPlan where DutyPlanID =@@identity;好像还是不对的
      

  10.   

    先定义N个变量;再用一条SELECT语句给这些变量赋值;最后再写三条INSERT,VALUES用开头定义的变量的值。
      

  11.   

    上面的方法有点笨,这样做好点:
    ----------
    CREATE TRIGGER [dutyplan1] ON dbo.DutyPlan 
    FOR INSERT
    AS Declare @intTmp int
    Set @intTmp = @@identity insert into PendingDoc (Title,StartTime,Author,AuthorDutyCode,Dept,CurrentOper,CurrentOperDept,CurrentOperDutyCode,LinkPage,LinkMatchID,Cata)
    select title,timestart,Author,AuthorDutyCode,Authordept,DutyMan,DutyManDept,DutyManDutyCode,LinkPage,DutyPlanID,Cata from DutyPlan where DutyPlanID =@intTmp;insert into PendingDoc (Title,StartTime,Author,AuthorDutyCode,Dept,CurrentOper,CurrentOperDept,CurrentOperDutyCode,LinkPage,LinkMatchID,Cata)
    select title,timestart,Author,AuthorDutyCode,Authordept,DutyLeader,DutyLeaderDept,DutyLeaderDutyCode,LinkPage,DutyPlanID,Cata from DutyPlan where DutyPlanID =@intTmp;insert into PendingDoc (Title,StartTime,Author,AuthorDutyCode,Dept,CurrentOper,CurrentOperDept,CurrentOperDutyCode,LinkPage,LinkMatchID,Cata)
    select title,timestart,Author,AuthorDutyCode,Authordept,DutyTopLeader,DutyTopLeaderDept,DutyTopLeaderDutyCode,LinkPage,DutyPlanID,Cata from DutyPlan where DutyPlanID =@intTmp;
      

  12.   

    用事务怎么得到没一条记录的ID,用触发器就可以通过DutyPlanID =@@identity得到,并在程序中接受,用事务怎么得到?????
      

  13.   

    事务中在每一条INSERT之后也用@@identity取ID