title: stored procedure with if else sql server 2008
body: <p>I have 3 tables, I have to check if grandparent table id has records in grandchildren table. If yes, return yes, else return no. Here is my stored procedure. I got an incorrect syntax error. I am new to stored procedure. Please help me.</p>
  
  <pre><code>CREATE PROCEDURE P_Check
     @PKG_ID INT,
     @S_ID INT,
     @FLAG VCHAR(10) OUT
  
     DECLARE IDS CURSOR LOCAL FOR SELECT S_ID FROM T1 WHERE P_ID = @PKG_ID
     OPEN IDS
     FETCH NEXT FROM IDS into @S_ID
  
     WHILE @@FETCH_STATUS = 0
     BEGIN
        SELECT * FROM T2 WHERE S_ID = @S_ID
  
        IF @@ROWCOUNT&lt;&gt;0
           @FLAG = 'YES'
           RETURN 
        ELSE
           @FLAG = 'NO'
  
        FETCH NEXT FROM IDS into @S_ID
     END
  
     CLOSE IDS
     DEALLOCATE IDS
  </code></pre>