0

我想提高错误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'
4

2 回答 2

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
于 2014-08-05T13:16:11.870 回答
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
于 2014-08-05T14:00:47.077 回答