Search code examples
sqlcursor

cursor with if statement


How can i write cursor when table is not fix. E.g

DECLARE cursor_1 CURSOR
FOR 
 IF BEGIN @i=1 SELECT col1,col2 FROM table_1 END
 ELSE BEGIN SELECT col1,col2 FROM table_2 END

...

This gives syntax error please suggest ?


Solution

  • This actually works, if you write it correctly!

    IF @i=1
     declare cur cursor for
      SELECT col1,col2 FROM table_1
    ELSE
     declare cur cursor for
      SELECT col1,col2 FROM table_2