简体   繁体   中英

Convert C# if Statement Into SQL Server if Statement

I am trying to convert an C# if statement into a SQL Server if statement so I can use it within a sql server statement to filter record based on this condition.

public bool checkSourceSystem(bool A, bool B, bool C, string SendToA, string SendToB, string SendToC)
    {
        int count = 0;

        if (A == true || B == true || C == true)
        {
            if(A == true && SendToA == "Success")
            {
                count++;
            }
            else
            {
                return false;
            }

            if(B == true && SendToB== "Success")
            {
                count++;
            }
            else
            {
                return false;
            }

            if (C == true && SendToC == "Success")
            {
                count++;
            }
            else
            {
                return false;
            }
        }
        else
        {
            return false;
        }

        if (count > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}

}

What I did as per below. But I dont think this one correct.

SELECT CASE

WHEN A = '1' AND SendToA = '1' THEN 'true' 

WHEN B = '1' AND SendToB = '1' THEN 'true' 

WHEN C = '1' AND SendToC = '1' THEN 'true'

ELSE 'false'

END 

FROM Table1

A,B,C,SendToA,SendToB,SendToC are fields in a table and the values for true and Success = 1 in sqlserver. Can anyone help me please?

Looking at your C# method, the return value is false when:

  1. A , B , C are all false
  2. A is false or A is true , but SendToA is not Success
  3. B is false or B is true , but SendToB is not Success
  4. C is false or C is true , but SendToC is not Success

This means that true is only ever returned when:

  1. A == B == C == true and
  2. SendToA == SendToB == SendToC == Success

This can then be shortened to:

public bool checkSourceSystem(bool A, bool B, bool C, string SendToA, string SendToB, string SendToC)
{
    return (A && B && C) && SendToA == "Success" && SendToB == "Success" && SendToC == "Success";
}

This corresponds to the following CASE statement:

select *,
    (
        CASE WHEN A=1
            AND B=1
            AND C=1
            AND SendToA = 1
            AND SendToB = 1
            AND SendToC = 1
        THEN 'true'
        ELSE 'false'
        END 
    ) as TrueFalse
from Table1

Create Function in SQL with below statement which will return you RESULT (True/False).

DECLARE @A BIT=0
DECLARE @B BIT=0
DECLARE @C BIT=0
DECLARE @SendToA VARCHAR(50)=''
DECLARE @SendToB VARCHAR(50)=''
DECLARE @SendToC VARCHAR(50)=''
DECLARE @Count INT=0

DECLARE @Result VARCHAR(50)=''

IF (@A=1 OR @B=1 OR @C=1)
BEGIN
    IF (@A=1 AND @SendToA='Success')
    BEGIN
        SET @Count=@Count+1
    END
    ELSE
    BEGIN
        SET @Result='False'
    END

    IF (@B=1 AND @SendToB='Success')
    BEGIN
        SET @Count=@Count+1
    END
    ELSE
    BEGIN
        SET @Result='False'
    END

    IF (@C=1 AND @SendToC='Success')
    BEGIN
        SET @Count=@Count+1
    END
    ELSE
    BEGIN
        SET @Result='False'
    END
END
ELSE
BEGIN
    SET @Result='False'
END

IF @Count>0
BEGIN
    SET @Result='True'
END

SELECT @Result AS Result

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM