如何在SQL条件中使用WHERE?

问题描述 投票:0回答:1

我有一个简单的声明:

SELECT * FROM Articles WHERE Category = @category

我需要当@category0null时,WHERE条件被忽略并且SQL选择所有文章; 类似于以下声明:

SELECT * FROM Articles
IF @category = 0 
BEGIN
WHERE Category = @category
END
sql mysql sql-server stored-procedures conditional-statements
1个回答
0
投票

所以我会这样构造它

IF @category = 0
BEGIN
    SELECT * FROM Articles WHERE Category = @category
END
© www.soinside.com 2019 - 2024. All rights reserved.