MSSQL WHERE 動態條件

WHERE 動態條件

使用邏輯達到動態條件效果
  AND ( @type = 0 OR id <= 5000 )
    如果 type = 0 相當於忽略後面條件
    如果 type = 1 後面條件才有效

--動態條件
DECLARE
  @type int
SELECT
  @type = 0

SELECT
  id
  ,account
FROM [dbo].[userAccount] AS ua WITH (NOLOCK)
WHERE 1 = 1
  AND ( @type = 0 OR id <= 5000 )
ORDER BY id ASC

我只是一棵樹