mardi 30 novembre 2021

IF-Else condition with SQL statement

declare @RevisionId int =3
declare @ObjectId  int =2

if @RevisionId in(2,3) 
Begin 
    If @ObjectId like '%1%' OR @ObjectId like '%2%' and @RevisionId =2
    Begin 
        If @ObjectId = '1'
        Begin       
            print 'ObjectId1 rev2'
        End    
        else If @ObjectId = '2'
        Begin 
            print 'ObjectId2 rev 2'
        End 
    End 
    else If @ObjectId like '%1%' OR @ObjectId like '%2%' and @RevisionId =3
    Begin
        If @ObjectId = '1'
        Begin   
            print 'ObjectId1 rev3'
        End  
    End 
    else If @ObjectId = '2'
    Begin
        print 'ObjectId2 rev3'
    End   
End 

Expected result:

  • For RevisionId 2 and ObjectId 1 -> ObjectId1 rev2 -- as expected from above solution

  • For RevisionId 2 and ObjectId 2 -> ObjectId2 rev2 -- as expected from above solution

  • For RevisionId 3 and ObjectId 1 -> ObjectId1 rev3 -- result failed and showing command successfully

  • For RevisionId 3 and ObjectId 2 -> ObjectId2 rev3 -- result failed and showing command successfully

There might be issue with the solution - how can I find the solution?

Aucun commentaire:

Enregistrer un commentaire