如何在字符串变量的基础上在WHERE子句中使用IF ELSE?

时间:2011-06-02 13:29:28

标签: sql-server-2008 stored-procedures

我有一个SP。它返回一些行数。这是SP:

ALTER PROCEDURE [dbo].[spGetClients]

( @orderBy varchar(50), @startAge int, @endAge int, @sex char, @staffId nvarchar(1024), @statusId nvarchar(1024), @ethnicityId nvarchar(1024), @treatmentProviderId nvarchar(1024) ) AS BEGIN SET NOCOUNT ON;

IF(@orderBy = 'Consumer Name')
BEGIN
    SELECT c.Id, dbo.GetClientFullName(c.FirstName, c.MiddleInit, c.LastName) AS ClientName, 
    c.DateOfBirth, dbo.GetAge(c.DateOfBirth, GETDATE()) AS Age, c.Sex, 
    dbo.GetClientStatus(c.Id, @statusId) AS Status, ca.Address, co.Phone, 
    dbo.GetEthnicity(c.Id, @ethnicityId) AS Ethnicity, dbo.GetDevelopmentalDisabilities(c.Id) AS Disabilities, 
    dbo.GetClientStaffContacts(c.Id, @staffId) AS Staff, dbo.GetClientContacts(c.Id) AS Contact, 
    dbo.GetClientInsuranceProviders(c.Id) AS HealthProvider, 
    dbo.GetClientTreatmentProviders(c.Id, @treatmentProviderId) AS TreatmentProvider
    FROM Client c  
    LEFT OUTER JOIN(        
        SELECT ca.ParentEntityId, ca.Address
        FROM ContactAddress ca
        INNER JOIN EntityName en ON en.Id = ca.EntityNameId AND en.Name = 'Client' 
        INNER JOIN GeneralLookup gl ON ca.glAddressTypeId = gl.Id AND gl.LookupItem = 'Primary'    
    ) ca ON c.Id = ca.ParentEntityId 
    LEFT OUTER JOIN(        
        SELECT co.ParentEntityId, co.ContactData Phone
        FROM ContactOther co
        INNER JOIN EntityName en ON en.Id = co.EntityNameId AND en.Name = 'Client' 
        INNER JOIN GeneralLookup gl ON co.glContactTypeId = gl.Id AND gl.LookupItem = 'Home'    
    ) co ON c.Id = co.ParentEntityId 
    LEFT OUTER JOIN GeneralStatus gs on gs.Id = c.StatusId
    where gs.Name <> 'Deleted' 
    and (dbo.GetAge(c.DateOfBirth, GETDATE()) BETWEEN @startAge and @endAge)
    and ((@sex = 'M' and c.sex = 'M') or (@sex = 'F' and c.Sex = 'F') or (@sex = '' and (c.Sex in ('M', 'F', ''))))
    and dbo.GetClientStaffContacts(c.Id, @staffId) is not null
    and dbo.GetClientStaffContacts(c.Id, @staffId) is not null
    and dbo.GetClientStatus(c.Id, @statusId) is not null 
    and dbo.GetEthnicity(c.Id, @ethnicityId) is not null 
    and dbo.GetClientTreatmentProviders(c.Id, @treatmentProviderId) is not null
    ORDER BY ClientName
END
END

UDF(用户定义的函数)是dbo.GetClientStaffContacts(c.Id,@ staffId)等。它们返回一些具有null的行。这就是我在where子句中使用最后5个语句的原因,这样只能收集非空值。一切正常,但问题是,我希望只有当这些变量不是空字符串时才检查非null:

@staffId nvarchar(1024), 
@statusId nvarchar(1024), 
@ethnicityId nvarchar(1024), 
@treatmentProviderId nvarchar(1024)

如何解决此问题? 我想要这样的东西:

If @staffId <> '' then dbo.GetClientStaffContacts(c.Id, @staffId) is not null

2 个答案:

答案 0 :(得分:0)

 AND  ((@staffId = "") OR (dbo.GetClientStaffContacts(c.Id, @staffId) is not null))

答案 1 :(得分:0)

您无法在CASE语句中进行逻辑比较。您必须从案例表达式生成一个值,并且生成的每个可能值的类型必须相同或可转换:

CASE @staffId
    WHEN "" THEN 1
    ELSE
       CASE
           WHEN dbo.GetClientStaffContacts(c.Id, @staffId) is not null THEN 1
           ELSE 0
       END
END = 1

您可以在WHERE条款中加入条件


可能有两种形式的CASE表达式:

  • 简单CASE(当您尝试在关键字CASE之后找到等于提供的值的WHEN子句时) - 这是外部CASE表达式正在执行的操作(尝试查找与{{匹配的WHEN子句) 1}} value)。
  • 搜索CASE(当您尝试根据任意谓词匹配WHEN子句时) - 这是内部CASE表达式正在执行的操作(测试函数的返回值是否为null)

使用Simple CASE表达式无法确定NULL /非NULL状态,因为NULL不等于NULL(也不等于)。