Don’t use Cursors in SQL Server

Using cursors in TSQL is like using a paddle to row your car.  It will get you there, but every other way to get there is better.

I was a programmer for a decade before I became a DBA.  I understand the desire to make Turing machines out of everything you can get your hands or keyboard attached to.

Techies who do not understand SQL tend to try to turn it into a procedural programming language.  The desire to create familiar programming structures can be overwhelming, but you must resist it when it comes to cursors and TSQL.

Next time you think you need to make a cursor to plod through a set of data please spend a few moment making a temp table and putting your data there for sorting and adjusting.  Then update the existing data from your temp table.

Database engines are built to manipulate data in relational databases.  Let them do what they do using TSQL.  Put away the paddle and try using the steering wheel instead.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s