| Home | Free Articles for Your Site | Submit an Article | Advertise | Link to Us | Search | Contact Us |
This site is an archive of old articles

    SEARCH ARTICLES
    Custom Search


vertical line

Article Surfing Archive



Converting Cursors to a Set Based Solution - Part I - Articles Surfing

The biggest hurdle a developer has to overcome when moving from traditional 3rd generation languages to T_SQL is moving the mindset from CURSOR based solutions to SET based solutions.

Don't get me wrong, I believe CURSOR based solutions have their place, and when working with small datasets you won't notice much of a difference in speed. It is only when working with the lart datasets (100,000+ rows) you will notice a dramatic improvement in speed.

I believe there is nothing like a few good examples so I will endeavour to provide multiple examples of the next few posts.

Simple Example 1:

We have two tables one contains a subset of the other; we need to move all the data from the main table to the subset table.

Customer--------IDNameStreetAddressSuburbCityZIPSexMartialStatus
MailingList-----------NameStreetSuburbCityZIP

The CURSOR based solution to do this is:

DECLARE @Name varchar(50), @Street varchar(50)DECLARE @Suburb varchar(50), @City varchar(50)DECLARE @ZIP varchar(50)DECLARE CustomerCursor CURSOR FOR  SELECT Name, StreetAddress, Suburb, City, ZIP    FROM Customer        OPEN CustomerCursor        FETCH NEXT FROM CustomerCursor INTO @Name, @Street, @Suburb, @City, @ZIPWHILE @@FETCH_STATUS = 0BEGIN  INSERT INTO MailingList    (Name, Street, Suburb, City, ZIP)  VALUES    (@Name, @Street, @Suburb, @City, @ZIP)  FETCH NEXT FROM CustomerCursor INTO @Name, @Street, @Suburb, @City, @ZIPENDCLOSE CustomerCursorDEALLOCATE CustomerCursor

The SET based solution to do this is:

INSERT INTO MailingList  SELECT Name, StreetAddress, Suburb, City, ZIP    FROM Customer

As you can probably see from the above example the simple SET based solution is much more efficient and easier to read. The SET based solution also has one other advantage, if the field sizes are to change then you don't need to worry about the code.

Submitted by:

Nicholas Edwards

Nicholas Edwards is co-owner of http://www.ersysgroup.com, a web site about our experiences with starting an online empire. Nicholas has also dedicated himself to sharing technical know-how.



        RELATED SITES






https://articlesurfing.org/computers_and_internet/converting_cursors_to_a_set_based_solution_part_i.html

Copyright © 1995 - Photius Coutsoukis (All Rights Reserved).










ARTICLE CATEGORIES

Aging
Arts and Crafts
Auto and Trucks
Automotive
Business
Business and Finance
Cancer Survival
Career
Classifieds
Computers and Internet
Computers and Technology
Cooking
Culture
Education
Education #2
Entertainment
Etiquette
Family
Finances
Food and Drink
Food and Drink B
Gadgets and Gizmos
Gardening
Health
Hobbies
Home Improvement
Home Management
Humor
Internet
Jobs
Kids and Teens
Learning Languages
Leadership
Legal
Legal B
Marketing
Marketing B
Medical Business
Medicines and Remedies
Music and Movies
Online Business
Opinions
Parenting
Parenting B
Pets
Pets and Animals
Poetry
Politics
Politics and Government
Real Estate
Recreation
Recreation and Sports
Science
Self Help
Self Improvement
Short Stories
Site Promotion
Society
Sports
Travel and Leisure
Travel Part B
Web Development
Wellness, Fitness and Diet
World Affairs
Writing
Writing B