简体   繁体   English

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

[英]return @@rowcount result on SQL query

I wanted to raiserror if exists and then print the number of rows that are matching .But this doesnt work . 我想提高if exists ,然后print匹配的行数。但这不起作用。 Please help . 请帮忙 。

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'

You missed BEGIN and END : 您错过了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

This is an alternative to your original post...not sure if you have specific requirements, but this should get you the error output that you desire: 这是您原始帖子的一种替代方法...不确定您是否有特定要求,但这应该为您提供所需的错误输出:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM