SQL中的IF ELSE语句
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 S.SoftNo > 0 ELSE S.SoftNo = @SoftNo)
如果版本等于零,我想列出版本号大于0的所有版本号(如果它不等于0),那么Version应该是任何值。
这与SoftNo相同。
我怎样才能修复我的SQL查询。 它只是不起作用。
更多细节:
这是我想要实现的:
if(Version == 0)
{
display every single rows if their version number is greater then 0;
}
else
{
Lets assume that Version is equal to 5. Then just display the rows if their Version number is equal to 5;
} // This is like a C# code of what I am trying to do in my sql query.
您必须使用CASE WHEN语句而不是if
尝试这样的事情:
SELECT S.Id, S.Name, S.Version, S.SoftNo
FROM SOFTWARE S WITH(NOLOCK)
WHERE ((@Version = 0 AND S.Version > 0) OR
(@Version <> 0 AND S.Version = @Version)) AND
((@SoftNo = 0 AND S.SoftNo > 0) OR
(@SoftNo <> 0 AND S.SoftNo = @SoftNo))
你有没有想过使用SQL案例?
SELECT col1, col2,
CASE
WHEN expression THEN return
WHEN expression THEN return
ELSE return
END AS NameOfNewColWithReturnValues
FROM Col_FROM_WHICH_TABLE
链接地址: http://www.djcxy.com/p/94357.html
下一篇: IF, ELIF, ELSE in T