Sql server 在SQL查询中返回@@rowcount结果
我想找出错误Sql server 在SQL查询中返回@@rowcount结果,sql-server,rowcount,raiserror,Sql Server,Rowcount,Raiserror,我想找出错误如果存在,然后打印匹配的行数。但这不起作用。请帮忙 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(@
如果存在
,然后打印匹配的行数。但这不起作用。请帮忙
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'
您错过了
开始
和结束
:
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
这是您原始帖子的替代方案…不确定您是否有特定要求,但这应该会得到您想要的错误输出:
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
谢谢,但它返回零,但我可以看到有匹配的记录。