简体   繁体   中英

to write if condition in when statement of case in sql procedure

I have following data in 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. 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.

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

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) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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