【问题标题】:return @@rowcount result on SQL query在 SQL 查询中返回 @@rowcount 结果
【发布时间】:2014-08-05 13:11:10
【问题描述】:

我想提高if exists 然后print 匹配的行数。但这不起作用。请帮忙 。

if exists (select * from [rto] a
     inner join rt b
     on a.NUM=b.TABLE_NAME
     where a.START_YEAR between b.YEAR_START and b.YEAR_STOP)
     Raiserror ('Matched recs found',16,1)
     print 'There are' +  cast(@@rowcount as varchar(20)) + 'matched rows'

【问题讨论】:

    标签: sql-server rowcount raiserror


    【解决方案1】:

    这是您原始帖子的替代品...不确定您是否有特定要求,但这应该会为您提供所需的错误输出:

    DECLARE @rowcount INT
    SET @rowcount = (select COUNT(*) from [rto] a
        inner join rt b
        on a.NUM=b.TABLE_NAME
        where a.START_YEAR between b.YEAR_START and b.YEAR_STOP)
    IF @rowcount > 0
    BEGIN
        Raiserror ('Matched recs found',16,1)
        print 'There are ' +  cast(@rowcount as varchar(20)) + ' matched rows'
    END
    

    【讨论】:

      【解决方案2】:

      你错过了BEGINEND

      if exists (select * from [rto] a
          inner join rt b
          on a.NUM=b.TABLE_NAME
          where a.START_YEAR between b.YEAR_START and b.YEAR_STOP)
      BEGIN
          Raiserror ('Matched recs found',16,1)
          print 'There are ' +  cast(@@rowcount as varchar(20)) + ' matched rows'
      END
      

      【讨论】:

      • 谢谢,但它返回零,但我可以看到有匹配的记录。
      猜你喜欢
      • 1970-01-01
      • 2012-05-08
      • 1970-01-01
      • 2021-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多