简体   繁体   English

将C#if语句转换为SQL Server if语句

[英]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. 我正在尝试将C#if语句转换为SQL Server if语句,以便我可以在sql server语句中使用它来根据此条件过滤记录。

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. A,B,C,SendToA,SendToB,SendToC是表中的字段,并且在sqlserver中为trueSuccess = 1的值。 Can anyone help me please? 谁能帮我吗?

Looking at your C# method, the return value is false when: 查看您的C#方法,在以下情况下返回值为false

  1. A , B , C are all false ABC都是false
  2. A is false or A is true , but SendToA is not Success A是假的或Atrue ,但SendToA并不Success
  3. B is false or B is true , but SendToB is not Success B是假的或Btrue的,但SendToB并不Success
  4. C is false or C is true , but SendToC is not Success C是假的或Ctrue的,但SendToC并不Success

This means that true is only ever returned when: 这意味着只有在以下true下才返回true

  1. A == B == C == true and A == B == C == true
  2. SendToA == SendToB == SendToC == Success 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: 这对应于以下CASE语句:

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). 使用下面的语句在SQL中创建函数,函数将返回RESULT(正确/错误)。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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