IF ELSE Statement in SQL

后端 未结 7 513
名媛妹妹
名媛妹妹 2021-01-22 12:10
SELECT S.Id, S.Name, S.Version, S.SoftNo
FROM SOFTWARE S WITH(NOLOCK)
WHERE (IF S.Version = 0 THEN S.Version > 0 ELSE S.Version = @Version)
AND (IF S.SoftNo = 0 THEN          


        
7条回答
  •  面向向阳花
    2021-01-22 12:54

    Why not do

    SELECT S.Id, S.Name, S.Version, S.SoftNo
    FROM SOFTWARE S WITH(NOLOCK)
    WHERE 
    (
        (@Version = 0 OR (@Version <> 0 AND S.Version = @Version))
        AND
        (@SoftNo = 0 OR (@SoftNo <> 0 AND S.SoftNo = @SoftNo))
    )
    

    (Do you really need the NOLOCK?)

提交回复
热议问题