locked
What am I missing here RRS feed

  • Question

  • Hi,
    I am trying to write a simple proc but getting nowhere and I cannot see why
    I am gettint a syntax error near word ELSE but do not understand:

     

    IF EXISTS(SELECT COUNT(IdCount) FROMtblWWWCount
                   WHERE UserName = @user)
     

     

     

    --find out the qty of the current user
    SELECT @cnt = Qty FROM tblwwwCount WHERE Username = +'@user'
    ;
    print@cnt
    ELSE

     

    --enter new information
    INSERT INTO tblWWWCount (Qty,UserName)
    VALUES(1,@user);
    END

    I know my brain is jello right now but this is simple right

    Thanks

    Thursday, July 7, 2011 7:45 PM

Answers

  • Change this to:

    IF EXISTS(SELECT COUNT(IdCount) FROMtblWWWCount
        WHERE UserName = @user) 
     
    
     
    BEGIN
    --find out the qty of the current user
      SELECT @cnt = Qty FROM tblwwwCount WHERE Username = +'@user';
     print@cnt
    END
    ELSE
     
    
    --enter new information
    INSERT INTO tblWWWCount (Qty,UserName)
    VALUES(1,@user);
    
    

    Same of course goes for ELSE part also. In fact, it may be a good habit to always use BEGIN END keywords for any constructs. I remember when I just started with T-SQL it was hard to remember/understand.


    In other words, if you need to use more than one statement for IF part, you must put them in BEGIN/END block to be able to use ELSE

     

    IF Condition
    
         BEGIN
    
            Some statements
    
        END
    
    ELSE
    

     


    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog

    • Marked as answer by Alabil Thursday, July 7, 2011 7:52 PM
    • Edited by Naomi N Thursday, July 7, 2011 7:53 PM
    Thursday, July 7, 2011 7:49 PM

All replies

  • Change this to:

    IF EXISTS(SELECT COUNT(IdCount) FROMtblWWWCount
        WHERE UserName = @user) 
     
    
     
    BEGIN
    --find out the qty of the current user
      SELECT @cnt = Qty FROM tblwwwCount WHERE Username = +'@user';
     print@cnt
    END
    ELSE
     
    
    --enter new information
    INSERT INTO tblWWWCount (Qty,UserName)
    VALUES(1,@user);
    
    

    Same of course goes for ELSE part also. In fact, it may be a good habit to always use BEGIN END keywords for any constructs. I remember when I just started with T-SQL it was hard to remember/understand.


    In other words, if you need to use more than one statement for IF part, you must put them in BEGIN/END block to be able to use ELSE

     

    IF Condition
    
         BEGIN
    
            Some statements
    
        END
    
    ELSE
    

     


    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog

    • Marked as answer by Alabil Thursday, July 7, 2011 7:52 PM
    • Edited by Naomi N Thursday, July 7, 2011 7:53 PM
    Thursday, July 7, 2011 7:49 PM
  • IF EXISTS(SELECT COUNT(IdCount) FROMtblWWWCount
            WHERE UserName = @user) 
    Begin 
        --find out the qty of the current user
        SELECT @cnt = Qty FROM tblwwwCount WHERE Username = +'@user';
        print@cnt
    End
    ELSE 
    Begin 
        --enter new information
        INSERT INTO tblWWWCount (Qty,UserName)
        VALUES(1,@user);
    END 
    


    Best Wishes, Arbi --- MCC 2011; Please vote if you find this posting was helpful or Mark it as answered.
    Thursday, July 7, 2011 7:50 PM
  • if more than one statement in the if or else block need begin and end. also count of 0 is still true shoud be if (select count(*)...) > 0 

    if  EXISTS(SELECT COUNT(IdCount) FROMtblWWWCount

                   WHERE UserName = @user) 

    begin

    --find out the qty of the current user

    SELECT @cnt = Qty FROM tblwwwCount WHERE Username = +'@user';

    print@cnt

    end

    ELSE

     

     

    --enter new information

    INSERT INTO tblWWWCount (Qty,UserName)

    VALUES(1,@user);

    Thursday, July 7, 2011 7:52 PM
  • I really need some vacation :))))

    Thanks for the answer

    Thursday, July 7, 2011 7:53 PM