sql server - T-SQL cursor loop syntax -


just simple style question i've been curious about.

the canonical forward cursor loop goes this.

declare table_data forward_only cursor select c1, c2 t1;  declare @c1 int; declare @c2 int;  open table_data; fetch table_data @c1, @c2; while @@fetch_status = 0 begin     -- @c1, @c2     fetch table_data @c1, @c2; end;  close table_data; deallocate table_data; 

there's minor code style issue in fetch statement repeated verbatim. there reason "standard" loop when written this?

open table_data; while 1 = 1     fetch table_data @c1, @c2;     if @@fetch_status != 0 break;     -- @c1, @c2 end; 

i'm not looking opinions on way better. i'm curious if there's reason first way seems overwhelmingly standard can argued have drawback. there problem infinite loop version i'm not seeing?

i'm guessing reason break recent t-sql addition, i'm having trouble finding changelog of t-sql language confirm hypothesis.

the common way see first way. sean mentioned, 1=1 can accidentally end in endless loop it's lesser choice. either way, knows why people either? same people ones decided use cursor when didn't need to. if need loop-like solution, use recursive cte never use cursors plus syntax obnoxious. if choose between cursor , while loop(like choosing poison drink), i'd choose while loop because syntax simpler.


Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

order - Notification for user in user account opencart -