Cute Bear
Cute Bear

Reputation: 3293

IF ELSE Statement in SQL

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)

If Version is equal to zero, I want to list everything version number is greater then 0 if it is not equal to 0, then Version should be whatever it is value.

This is same for SoftNo.

How can I fix my SQL query. It just doesn't work.

More details:

This is what I want to achieve:

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.

Upvotes: 2

Views: 30568

Answers (7)

abhimanyuaryan
abhimanyuaryan

Reputation: 4014

Have you thought of using SQL cases?

SELECT col1, col2,
    CASE
        WHEN expression THEN return 
        WHEN expression THEN return 
        ELSE return 
    END AS NameOfNewColWithReturnValues
FROM Col_FROM_WHICH_TABLE 

Upvotes: 1

Daniel Powell
Daniel Powell

Reputation: 43

Don't use concatenated SQL, it is a poor habit that increases the probability of SQL injection vulnerabilities. Your SQL code is now the exact same as the following (safer) code:

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

Upvotes: 0

Ian Yates
Ian Yates

Reputation: 1354

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?)

Upvotes: 0

Cute Bear
Cute Bear

Reputation: 3293

Instead, I came up with this workaround,

sSQL = @"SELECT S.Id, S.Name, S.Version, S.SoftNo
         FROM SOFTWARE S WITH(NOLOCK)
         WHERE 1 = 1";
// 1=1 is used just to list everything.

if(pVersion != 0)
{
     sSQL += " AND S.Version = @Version";
}
if(pSoftNo != 0)
{
     sSQL += " AND S.SoftNo = @SoftNo";
}

Conclusion, if else part is moved to code side.

Upvotes: 0

Paul Grimshaw
Paul Grimshaw

Reputation: 21034

Don't quite understand, but if you mean the input variables equal zero, you could do something like:

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

Upvotes: 0

tobias86
tobias86

Reputation: 5029

Try something like this:

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))

Upvotes: 1

Habib
Habib

Reputation: 223257

You have to use CASE WHEN statement instead of if

Upvotes: 0

Related Questions