0

I have parameter on sql , and I got an issue when it comes to this situation

declare @MonRemark varchar(10) = 's'
declare @ConfBy varchar(10)= 's'
declare @confstatus varchar(10)= 's'
declare @StatGD varchar(10)= ''
declare @StatGC varchar(50)= ''
declare @STATGTWO varchar(50)= ''



IF @MonRemark <> '' and  (@ConfBy = '' OR @confstatus = '' and (@StatGD = '' or @StatGC = '' or @STATGTWO = ''))   BEGIN
    print 'a'
END
ELSE IF  @ConfBy  <> '' and (@MonRemark = '' OR @confstatus = '' AND (@StatGD = '' or @StatGC = '' or @STATGTWO = '')) BEGIN
    print 'b'
END
ELSE IF @confstatus  <> '' and @MonRemark = '' AND @ConfBy = '' AND (@StatGD = '' or @StatGC = '' or @STATGTWO = '') BEGIN
    print 'c'
END
ELSE IF   (@StatGD <> '' or @StatGC <> '' or @STATGTWO <> '')  and @confstatus  = '' AND @MonRemark = '' AND @ConfBy = '' BEGIN
    print 'd'
END
ELSE BEGIN
    print 'e'
END

if I filled parameter like what I write Above, I wish to get A , but it's always print E, is there any way to get A?

2
  • 1
    you will never get A if u fill the ConfBy and the Confstatus with 's'. you will need to change the constant value or the logic rule to get what u want. Commented Feb 23, 2016 at 6:32
  • 1
    It's like this if you want result A, all the condition must TRUE but in there the results are like IF TRUE and (FALSE and (TRUE)) BEGIN PRINT 'a'. Your condition return FALSE because @ConfBy and @confstatus are not NULL. Both @ConfBy and @confstatus have values S Commented Feb 23, 2016 at 6:55

1 Answer 1

2

Change your condition. I think @Cyan and @JTR has provided enough explanation on why you get the value 'e'

Try like this, I have modified your condition to get the required results.

DECLARE @MonRemark VARCHAR(10) = 's'
DECLARE @ConfBy VARCHAR(10) = 's'
DECLARE @confstatus VARCHAR(10) = 's'
DECLARE @StatGD VARCHAR(10) = ''
DECLARE @StatGC VARCHAR(50) = ''
DECLARE @STATGTWO VARCHAR(50) = ''

IF @MonRemark <> ''
    AND (
        (
            @ConfBy = ''
            OR @confstatus = ''
            )
        OR (
            @StatGD = ''
            OR @StatGC = ''
            OR @STATGTWO = ''
            )
        )
BEGIN
    PRINT 'a'
END
ELSE IF @ConfBy <> ''
    AND (
        @MonRemark = ''
        OR @confstatus = ''
        AND (
            @StatGD = ''
            OR @StatGC = ''
            OR @STATGTWO = ''
            )
        )
BEGIN
    PRINT 'b'
END
ELSE IF @confstatus <> ''
    AND @MonRemark = ''
    AND @ConfBy = ''
    AND (
        @StatGD = ''
        OR @StatGC = ''
        OR @STATGTWO = ''
        )
BEGIN
    PRINT 'c'
END
ELSE IF (
        @StatGD <> ''
        OR @StatGC <> ''
        OR @STATGTWO <> ''
        )
    AND @confstatus = ''
    AND @MonRemark = ''
    AND @ConfBy = ''
BEGIN
    PRINT 'd'
END
ELSE
BEGIN
    PRINT 'e'
END
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.