简体   繁体   English

在SQL过程中的case语句中写if条件

[英]to write if condition in when statement of case in sql procedure

I have following data in sql 我在SQL中有以下数据

数据表

I want to write query in such way that, I have data of John Smith with middle initial M, and when license-sector field value selected is not equal to non-frontline then it should show john smith M with license sector value non-frontline, door, security, private investigation. 我想以这样的方式编写查询:我有约翰·史密斯的数据,中间有首字母M,并且当选择的许可证扇区字段值不等于非前线时,它应该显示约翰·史密斯M的许可证扇区值是非前线,门,安全,私人调查。 It is only when Role atrribute is null, of role contains Non front line or front it would show data which have role as per selected value. 仅当“角色属性”为null,角色包含“非第一行”或“非第一行”时,它才会显示按所选值具有角色的数据。

As I am passing value to licence sector from front end using drop down and my drop down does not contains non-front line. 当我使用下拉菜单从前端向许可证部门传递价值时,我的下拉菜单不包含非第一行。 It is just in data base and role is another dropdown which has non-frontline and frontline value. 它只是在数据库中,角色是另一个具有非前线和前线值的下拉菜单。

So I want out put in following way 所以我想以下面的方式

If licence sector value selected from drop down is door security and role value is null then out put for John smith M 如果从下拉列表中选择的许可证部门值为门安全并且角色值为空,则将约翰·史密斯M放出

1102551085816250    Smith       John    M   12/15/1987  Non-Frontline   Non-Frontline                   
1102941363382950    Smith       John    M   12/15/1987  Door Security   Frontline                   

If licence sector value selected from drop down is door security and role value selected from dropdown is front line: 如果从下拉列表中选择的许可证扇区值是门禁安全性,而从下拉列表中选择的角色值是前线:

 1102941363382950   Smith   John    M   12/15/1987  Door Security   Frontline

if licence sector value selected from drop down is door security and role value selected from dropdown is Non-frontline then it should not show any data as there is no such combination 如果从下拉列表中选择的许可证扇区值是门禁安全性,并且从下拉列表中选择的角色值是非前线,则它不应显示任何数据,因为没有这样的组合

I wrote procedure in following way 我以以下方式编写程序

SELECT Licence_Number,
       Family_Name,
       First_Name,
       Middle_Initial,
       Date_of_birth,
       Licence_sector,
       [Role],
       [Expiry_date],
       [Status],
       Status_as_of_date,
       Status_explanation,
       Additional_condition
FROM   Public_Register
WHERE  Family_Name = @Familyname
       AND [Expiry_date] > Getdate()
       AND ( CASE
               WHEN @Forname IS NULL THEN 'A'
               ELSE First_Name
             END LIKE Isnull('%' + @Forname + '%', 'A')
             AND CASE
                   WHEN @MiddleInit IS NULL THEN 'A'
                   ELSE Middle_Initial
                 END = Isnull(@MiddleInit, 'A')
             AND CASE
                   WHEN @DOB IS NULL THEN ''
                   ELSE Date_of_birth
                 END = Isnull(@DOB, '')
             AND ( CASE
                     WHEN @Role IS NULL THEN ''
                     ELSE [Role]
                   END = Isnull(@Role, '') )
              OR [Role] = 'Non-Frontline' )
       AND CASE
             WHEN @Sector IS NULL THEN ''
             ELSE Licence_sector
           END = Isnull(@Sector, '') 

Please help me how can i modify procedure to obtain desired output. 请帮我如何修改程序以获得所需的输出。

尝试修改此行:

    AND (CASE WHEN @Role IS NULL THEN TRUE ELSE ( [Role] = @Role ) END) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM