SQL Server存储过程:在一对多关系上过滤表

时间:2018-01-23 20:41:43

标签: sql sql-server tsql stored-procedures

我正在尝试创建一个存储过程,该存储过程将对具有多个关系的表执行查询。

示例主表:

Build

示例一对多关系

dbo.Provider
+----+-----------+------------+
| Id | FirstName |  LastName  |
+----+-----------+------------+
|  1 | Lor       | Blair      |
|  2 | Wanda     | Montgomery |
|  3 | Inez      | Palmer     |
|  4 | Cecilia   | Adkins     |
|  5 | Jessica   | Swanson    |
+----+-----------+------------+

我有dbo.ProviderSpecialty +----+----------------+-----------+ | Id | Specialty | Type | +----+----------------+-----------+ | 1 | ANESTHESIOLOGY | Primary | | 1 | CARDIOLOGY | Secondary | | 2 | ANESTHESIOLOGY | Primary | | 3 | ANESTHESIOLOGY | Primary | | 3 | AMBULANCE | Secondary | | 4 | AMBULANCE | Primary | | 5 | CARDIOLOGY | Primary | | 5 | AMBULANCE | Secondary | +----+----------------+-----------+ dbo.ProviderAddress +----+-------+-----------+ | Id | Zip | Type | +----+-------+-----------+ | 1 | 11510 | Primary | | 2 | 11510 | Primary | | 2 | 13090 | Secondary | | 2 | 96814 | Previous | | 3 | 13090 | Primary | | 4 | 13090 | Primary | | 5 | 96814 | Primary | | 5 | 11510 | Secondary | +----+-------+-----------+ dbo.ProviderHealthPlan,但他们会采用相同的方法。

我的存储过程会接受dbo.ProviderHospitalFirstNameLastNameSpecialty作为参数。

如果参数为Zip,则不要对该参数进行过滤。 如果参数具有值,则仅显示参数匹配的提供者。

另外,我想在结果中显示所有Specialties和Zips。

例如,搜索null(所有其他人为null)将导致:

Specialty = 'AMBULANCE'

+----+-----------+----------+---------------------------+--------------+ | Id | FirstName | LastName | Specialties | Zips | +----+-----------+----------+---------------------------+--------------+ | 3 | Inez | Palmer | ANESTHESIOLOGY, AMBULANCE | 13090 | | 4 | Cecilia | Adkins | AMBULANCE | 13090 | | 5 | Jessica | Swanson | CARDIOLOGY, AMBULANCE | 96814, 11510 | +----+-----------+----------+---------------------------+--------------+ Specialty = 'AMBULANCE'上的文件管理器将返回:

Zip = '13090'

显然,对名字或姓氏的过滤器是不言自明的。我有4个不同的一对多关系可以过滤,这只是一些示例数据。

任何有关从哪里开始的帮助都很棒。

谢谢!

3 个答案:

答案 0 :(得分:1)

答案 1 :(得分:0)

如果要过滤传递的值,如果传递null则不过滤, 然后你可以构建这样的where子句:

where [someColumn] = ISNULL(@someVariable, [someColumn])
      and [someOtherCol] = ISNULL(@someOtherVar, [someOtherCol])

就将所有关系组合成单个值,您可以使用以XML形式重新计算结果的子查询,然后将其解析为字符串值。它不会很漂亮,但绝对有效。

答案 2 :(得分:0)

这样的事情应该有用,我没有测试过,但我认为你很容易理解发生的事情

CREATE PROC Show
@Firstname VARCHAR(50) = NULL
,@LastName VARCHAR(50) = NULL
,@Specialty VARCHAR(50) = NULL
,@zip INT = NULL

AS 
IF (@Firstname IS NOT NULL 
    AND @LastName IS NOT NULL
    AND @Specialty IS NOT NULL 
    AND @zip IS NOT NULL )

    Begin
            SELECT * FROM MainTable M
            JOIN Specialty s ON s.ID = m.id
            JOIN zip z ON z.ID = m.ID
            WHERE m.FIrstname = @Firstname AND m.lastname = @LastName AND s.specialty = @Specialty AND z.zip = @zip


    END 
        ELSE
           BEGIN
               IF( @Firstname IS NOT NULL OR @LastName IS NOT null)
                  Begin
                     SELECT * FROM MainTable M
                     JOIN Specialty s ON s.ID = m.id
                     JOIN zip z ON z.ID = m.ID
                     WHERE (@Firstname IS NULL OR m.Firstname = @Firstname AND @LastName IS NULL OR m.Lastname = @LastName)
                  END 
                     ELSE 
                             BEGIN
                               SELECT * FROM MainTable M
                               JOIN Specialty s ON s.ID = m.id
                               JOIN zip z ON z.ID = m.ID
                               WHERE( @Specialty IS NULL or s.specialty = @Specialty AND @zip IS NULL OR zip = @zip )
                             END
           END