xeqtr1982(ShaKa) ( ) 信誉:100  2006-04-05 14:39:00   
create table a(a1 int,a2 int,a3 int)
create table b(b1 int,b2 int,b3 int)
gocreate trigger cfq on a
for insert,update
asif @@rowcount=0 return
declare @count_ins int
declare @count_del int
select @count_ins=count(*) from inserted
select @count_del=count(*) from deletedif(@count_ins>0 and @count_del>0)
begin
 if update(a1)
   update b set b1=a.a1 from inserted a,deleted c where b.b1=c.a1
 if update(a2)
   update b set b2=a.a2 from inserted a,deleted c where b.b2=c.a2
 if update(a3)
   update b set b3=a.a3 from inserted a,deleted c where b.b3=c.a3
endif(@count_ins>0 and @count_del=0)
begin
 insert into b select * from inserted
end
goinsert into a select 1,2,3
insert into a select 3,4,5
insert into a select 6,7,8
go
select * from a
select * from bupdate a set a1=100 where a1=1
update a set a2=500 where a2=4
go
select * from a
select * from bdrop trigger cfq
drop table a
drop table b
问题if @@rowcount=0 return
declare @count_ins int
declare @count_del int
select @count_ins=count(*) from inserted
select @count_del=count(*) from deleted@是什么意思?declare是什么命令? 嗨!这断不懂哦!解释 一下下?

解决方案 »

  1.   

    DECLARE 语句声明变量,并用 SET 或 SELECT 语句给其指派值
    @指的是变量,@@为全局变量
      

  2.   

    @是T-SQL内部声明变量的描述符,@表示局部变量,@@表示全局变量DECLARE @local_variable
    在批处理或过程的正文中用 DECLARE 语句声明变量,并用 SET 或 SELECT 语句给其指派值。
      

  3.   

    那count(*) from inserted 是获得的总列数?
      

  4.   

    那count(*) from inserted 是获得的总列数?
    ----------------------
    是的
      

  5.   

    对,是获得逻辑表inserted 的总行数
      

  6.   

    哦!!!!!!!!!!!列数还是行数?
    为!记录行数吧?理解!对比新的记录和老的记录?
    那@count_ins>0 and @count_del>0 是不是就是证明新添加的记录里,在老记录里有重复?》
      

  7.   

    @@rowcount要是全局变量!为什么在整段代码里面没有看到关于他的副职和定义?难道是用它来引发下面语句的运行?
      

  8.   

    哦!!!!!!!!!!!列数还是行数?
    ----------------------
    行数@@rowcount是全局变量!
    是由系统来维护的,每次批处理查询,更新,插入等后,都重置@@rowcount 的值
      

  9.   


    if(@count_ins>0 and @count_del>0)
    -------
    inserted 表和 deleted 表里都有数据 ---因此认为是update操作if(@count_ins>0 and @count_del=0)
    -------
    inserted 表有数据 deleted表没数据  ---因此认为是insert操作@@ROWCOUNT,sqlserver的系统函数
    返回受上一语句影响的行数。
      

  10.   

    建议楼主看一下基础知识和sqlserver的联机帮助
      

  11.   

    if(@count_ins>0 and @count_del>0)
    begin
     if update(a1)
       update b set b1=a.a1 from inserted a,deleted c where b.b1=c.a1
     if update(a2)
       update b set b2=a.a2 from inserted a,deleted c where b.b2=c.a2
     if update(a3)
       update b set b3=a.a3 from inserted a,deleted c where b.b3=c.a3
    end表C 是那里来的?
      

  12.   

    a 是 inserted 表的别名(inserted a)..
    c 是 deleted  表的别名(deleted c)-------------最后一次回答..
      

  13.   

    if @@rowcount=0 return
    ----@@rowcount 全局变量  返回上一次操作的数量declare @count_ins int
    declare @count_del int
    ----用户定义变量select @count_ins=count(*) from inserted
    ----inserted 插入操作数据集select @count_del=count(*) from deleted
    ----deleted 被删除的数据集----inserted、deleted 往往用在触发器中参考联机帮助
      

  14.   

    wangdehao(找找找(现在很幸福)) 是回答的最后一个 问题!哈哈!其实还想问的!算了!over!明天继续烦你!