【问题标题】: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】:
你错过了BEGIN和END:
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