我在dbo.Members中有两行,但我的存储过程没有返回计数。我可以像SELECT COUNT(*) FROM dbo.Members WHERE MemberID = 1234
一样单独运行查询,并将计数返回为2,这是正确的。
为什么它不会返回存储过程中的行?
SELECT
ValidCount,
InvalidCount,
(SELECT COUNT(*) FROM dbo.Members WHERE MemberID = @pMemberID) AS 'TotalMembers'
FROM
dbo.Reporting
WHERE
MemberID = @pMemberID
答案 0 :(得分:4)
可能是因为Reporting
中的条目没有MemberId = 1234
。
试试这个:
SELECT COALESCE(validCount, 0) AS validCount,
COALESCE(invalidCount, 0) AS invalidCount,
(
SELECT COUNT(*)
FROM members m
WHERE m.memberId = p.memberId
) AS totalMembers
FROM (
SELECT @pMemberId AS memberId
) p
LEFT JOIN
reporting r
ON r.memberId = p.memberId