Monday, December 17, 2007

Effective SQL Mindset: Cursors VS Joins

In the past I've dealt a lot with cursors. My experience was mostly negative. SQL code written using them was terribly slow. Thus our team was transforming SQL code with cursors into code without them.

Sure, cursors have their advantages. But, after conversion SQL query performance can be much better then with cursors.

Now, I'll show you how to accomplish the task using SQL with and without cursors.
There are two tables: regions and values. For example, regions represents departments while values is used to store arbitrary int data about the specific region.

SQL tables:
create table regions (id int, name varchar(10))
create table [values] (id int identity(1, 1), regid int, value int)

Table regions contains records for 100 regions, while values contains data only for region with id = 1 (only for the first region). We need to populate values table using data from the first region.

For exaple if table regions contains data

1Reg 1
2Reg 2

And table values contains data only for first region

idReg Idvalue

So we need to populate table values for every region from regions table.

There are three ways how to do this:
  • write a computer program that will insert values into values table
  • write SQL query with cursors
  • write SQL query without cursors
I will not touch how to write a program to do this, its too ineffective.

But last two options seem to be interesting. Cursor solution is quick and dirty - you can code it almost in no time. Cursor code will be similar to the computer program's one.

select id,regionid,value into #Valuestmp from [values]

declare @id int

OPEN RegionsCursor;
FETCH NEXT FROM RegionsCursor into @id;

UPDATE #Valuestmp SET RegionId=@id
INSERT INTO [values](regionId, value) SELECT regionId, value FROM #Valuestmp
FETCH NEXT FROM RegionsCursor into @id;
CLOSE RegionsCursor;
DEALLOCATE RegionsCursor;

drop table #Valuestmp
Here we store initial values from values table, and then for each region we insert data into values table.

This elegant script will accomplish the above:
insert into val (regid, value) select as regid, v.value as value from reg r, val v where <> 1

Central point in this script is Cartesian product - we select from two tables (bold font), joining every row of the values table with the rows in regions table.

SQL code above shows how cursor can be avoided. Every time you're tempted to use cursor - stop and think that nearly everything can be done without them. All you have to do is to think little bit.


  1. Infatuation casinos? ask this progeny [url=]casino[/url] coerce and toady to online casino games like slots, blackjack, roulette, baccarat and more at .
    you can also balk our redesigned [url=]casino[/url] touch at and substitute for in principal unfeeling currency !
    another chic [url=]casino spiele[/url] scenario is , because german gamblers, get magnanimous online casino bonus.

  2. all away uprightness of 21 and sine qua non some grown-up toys? check to [url=]sex toys [/url] online, well-built movement of grown-up toys like [url= up4sexx&cid=3]vibrators [/url] and [url=]dildos [/url]at , suffer the designate, another unsurpassed instal to trickle out of the closet [url=]viagra online[/url] is the all late-model and the prime ed rx [url=]online viagra[/url] drugstore , and the chief generic [url=]viagra[/url] drugstore at or at the [url=]sildenafil citrate[/url] shop.