请问我怎么获得所更新的那行所影响的行数的结果集
SQL Server 代码里怎么获得
我在写一个触发器Create trigger trig_update_Users
on Users
 for Update
 as
  Declare @BeginTime datetime,@Endtime datetime
  
  select @BeginTime=LastDate from deleted --获取更新前的时间
  select @Endtime=LastDate from inserted  --获取更新后的时间
  --LastDate 字段设了可以为"null"但是当它位"null"时就不能自动触发了只有更新后的"LastDate"字段有值时才能被触发
  
 if(@BeginTime<>@Endtime)
     update Users set VisiteTimes=VisiteTimes+1 where LastDate=@Endtime if(@Endtime<>null)
     update Users set VisiteTimes=VisiteTimes+1 where LastDate=@Endtime
请问这里该怎么写呢?
谢谢!

解决方案 »

  1.   

    @@ROWCOUNT
    返回受上一语句影响的行数。语法
    @@ROWCOUNT返回类型
    integer注释
    任何不返回行的语句将这一变量设置为 0 ,如 IF 语句。示例
    下面的示例执行 UPDATE 语句并用 @@ROWCOUNT 来检测是否有发生更改的行。UPDATE authors SET au_lname = 'Jones'
    WHERE au_id = '999-888-7777'
    IF @@ROWCOUNT = 0
       print 'Warning: No rows were updated'--------------------------------------
    IF @@ROWCOUNT > 0
       用原来的条件找到你的数据.
      

  2.   

    非常感谢dawugui!
    有了你我完成了很不错的功能!
     
      

  3.   

    呵呵,楼主记得触发器可不是你那么用的,如果是只修改一条数据。LZ的还凑合用。批量修改数据,LZ的可能有大问题
      

  4.   

    这个我早就知道了,insert用select @@Identity 而修改时用select @@rowCount获取影响的行数,但是对于批量修改怎么能够获取所影响的行数