存储过程中未返回行

时间:2011-07-20 16:26:54

标签: sql sql-server

我在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

1 个答案:

答案 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