使用参数

时间:2016-04-27 09:16:21

标签: sql-server tsql stored-procedures sql-server-2014

我有两张这样的表:

tabSubject

+-----------+--------------------+--------------------+----------------------+
| SubjectId | SubjectDescription | MeetingIdImportant | MeetingIdUnimportant |
+-----------+--------------------+--------------------+----------------------+
|    INT    |    NVARCHAR(100)   |      INT NULL      |       INT NULL       |
+-----------+--------------------+--------------------+----------------------+

tabMeeting

+-----------+-------------+
| MeetingId | MeetingDate |
+-----------+-------------+
|    INT    |  DATETIME   |
+-----------+-------------+

在我的应用程序中,我有一个调用存储过程的过滤器(带有SubjectIds的下拉列表)。

存储过程用于在网格中显示数据,如下所示:

+-------------+------------------------------+--------------------------------+
| MeetingDate | Number of important subjects | Number of unimportant subjects |
+-------------+------------------------------+--------------------------------+
| 01.05.2016  |              5               |               3                |
+-------------+------------------------------+--------------------------------+

如果没有选择主题,则必须显示所有可能的会议日期(即使他们还没有指定主题)。如果选择了一个主题,那么只有那些会议才会出现,将该主题作为重要或不重要的主题。

示例:

我选择了主题"测试"。该主题在2016年4月4日的会议中非常重要,在2016年5月11日的会议中并不重要。现在我期待这样的结果:

+-------------+------------------------------+--------------------------------+
| MeetingDate | Number of important subjects | Number of unimportant subjects |
+-------------+------------------------------+--------------------------------+
| 04.05.2016  |              6               |               2                |
| 11.05.2016  |              2               |               4                |
+-------------+------------------------------+--------------------------------+

这是在我的存储过程中使用此代码:

SELECT meeting.MeetingId,
    meeting.MeetingDate,
    (SELECT COUNT(MeetingIdMS1) FROM dbo.tabSubject WHERE MeetingIdImportant = meeting.MeetingId) AS NumberOfImportantSubjects,
    (SELECT COUNT(MeetingIdMS8) FROM dbo.tabSubject WHERE MeetingIdUnimportant = meeting.MeetingId) AS NumberOfUnimportantSubjects
FROM dbo.tabMeeting meeting
INNER JOIN
(
    SELECT MeetingIdImportant MeetingId
    FROM dbo.tabSubject
    WHERE (@SubjectId IS NOT NULL AND SubjectId = @SubjectId)
    UNION ALL
    SELECT MeetingIdUnimportant
    FROM dbo.tabSubject
    WHERE (@SubjectId IS NOT NULL AND SubjectId = @SubjectId)
) t ON
    CASE
        WHEN @SubjectId IS NOT NULL THEN t.MeetingId
        ELSE meeting.MeetingId
    END = meeting.MeetingId

但是当我没有选择任何主题时,不会显示任何数据。我错过了什么?

1 个答案:

答案 0 :(得分:1)

如果@SubjectId为空且INNER JOIN未返回数据,则表t没有数据

SELECT meeting.MeetingId,
    meeting.MeetingDate,
    (SELECT COUNT(MeetingIdMS1) FROM dbo.tabSubject WHERE MeetingIdImportant = meeting.MeetingId) AS NumberOfImportantSubjects,
    (SELECT COUNT(MeetingIdMS8) FROM dbo.tabSubject WHERE MeetingIdUnimportant = meeting.MeetingId) AS NumberOfUnimportantSubjects
FROM dbo.tabMeeting meeting
WHERE 
    @SubjectId IS NULL 
    OR EXISTS
    (
        SELECT * FROM tabSubject 
        WHERE 
        (
            MeetingIdImportant = meeting.MeetingId 
            OR MeetingIdUnimportant = meeting.MeetingId
        ) AND SubjectId = @SubjectId
    )