在SQL查询中返回@@ rowcount结果

时间:2014-08-05 13:11:11

标签: sql-server rowcount raiserror

我希望raiserror 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'

2 个答案:

答案 0 :(得分:0)

您错过了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

答案 1 :(得分:0)

这是您原始帖子的替代方案......不确定您是否有特定要求,但这可以为您提供所需的错误输出:

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