3

Every time I see SQL scripts with cursors...

C'mon man. Set based operations are way more efficient.

Comments
  • 0
    You can't always rewrite cursors with set based operations. With that said, you can do it most of the time.
  • 0
    @Bikonja Pretty much always can in SQL server. I can't think of an instance where you can't.

    Even if I grab a list of tables and need to do something to all tables I just grab a list of the tables and fields then generate all my SQL statements into one variable with the for xml path('') functionality it has. Then one exec on that variable and it's done but all the ops are set based. I'm sure there's an instance that requires it but I haven't really found one yet where I've had to. Table valued functions often eliminate things that would have to get a cursor too.
Add Comment