SQL Server – Case When Not Returning More Than One Value

sql server

I try this, but the SwitchCase can not return more than one value.

WHERE CP.NumIntAut IN (
    CASE WHEN @NumIntAut IS NOT NULL
        THEN (@NumIntAut)
        ELSE (SELECT NumIntAut FROM @TAB_NumIntAut) END)

How can I do?

Best Answer

I think you are looking to do something like this

WHERE CP.NumIntAut IN (
                       SELECT  CASE WHEN @NumIntAut IS NOT NULL
                                    THEN @NumIntAut
                                    ELSE NumIntAut END
                                    FROM @TAB_NumIntAut )

Assuming that I understand your question correctly. But it is worth noting that it will return the value @NumIntAut for every record in the @TAB_NumIntAut table if it not NULL. Which may or may not be what you are looking for.