Geekpedia Tutorials Home

Building a C# Chat Client and Server

Building a C# Chat Client and ServerA step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.

in C# Programming Tutorials

Getting Hard Drive Information

Getting Hard Drive InformationA C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.

in C# Programming Tutorials

UPS Shipping Calculator

UPS Shipping CalculatorThis tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.

in PHP Programming Tutorials

Create Your Own Rich Text Editor

Create Your Own Rich Text EditorCreating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.

in JavaScript Programming Tutorials
Search
Tutorials
Programming Tutorials
IT Jobs
From CareerBuilder

How to Convert Cursor into While Loop In SqlServer

This article is about how we can change the cursor in the while loop in SQL Server. It's very useful in improving the performance of stored procedures.

On Sunday, June 27th 2010 at 10:36 PM
By Kuldip Bhatt (View Profile)
-----   (Rated 0 with 0 votes)
Contextual Ads
More SQL Resources
Advertisement

I personally face this issues in many Store Procedures i.e. Cursor taking too much time to execute. I am sure many of you will be having similar problem.

I have worked on that issue and found following solution. It may not be perfect for cursor optimization but it does work in my case.

– Original Cursor Code —
Declare @fk_MedicationOrderId as Bigint
   Declare @AdminTime as Bigint
declare CUR_MEDICATIONORDERID cursor FOR
   select
   fk_MedicationOrderId
   from
   tbl_medicationorderDetail_trn
   where
   Actiondate > Getdate()
OPEN CUR_MEDICATIONORDERID
FETCH NEXT FROM CUR_MEDICATIONORDERID
   INTO @fk_MedicationOrderId
WHILE @@FETCH_STATUS =0
   BEGIN
Select @AdminTime=AdminTime from tbl_medicationorderDetail_trn
   where fk_medicationorderid = @fk_MedicationOrderId
Update tbl_TempMedicationOrderDetail_trn
   set ActionTime = @AdminTime
   set @AdminTime = ”
FETCH NEXT FROM CUR_MEDICATIONORDERID
   INTO @fk_MedicationOrderId
END
CLOSE CUR_MEDICATIONORDERID
   DEALLOCATE CUR_MEDICATIONORDERID
——————-
– Modified Cursor Code (faster) –
Declare @fk_MedicationOrderId as Bigint
   Declare @AdminTime as Bigint
Declare Table for the fields you need in the cursor.
declare @IDList table (ID Bigint)
Insert into @IDList
   select
   fk_MedicationOrderId
   from
   tbl_medicationorderDetail_trn
   where
   Actiondate > Getdate()
while (select count(ID) from @IDList) > 0
   begin
   select top 1 @fk_MedicationOrderId = ID from @IDList
Select @AdminTime=AdminTime from tbl_medicationorderDetail_trn
   where fk_medicationorderid = @fk_MedicationOrderId
Update tbl_TempMedicationOrderDetail_trn
   set ActionTime = @AdminTime
   set @AdminTime = ”
delete from @IDList where ID = @fk_MedicationOrderId
end
——————-

What I have done here is to convert “cursor” into “while loop”. It is just to get your data in any temporary table or table variable after that just delete the rows from that at end of loop.

It helps in improving performance, I have personally tried it in many stored procedures.

Let me know if you find any other useful way of improving performance.

Digg Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this tutorial
Comment Current Comments
by zzx on Wednesday, July 14th 2010 at 04:14 AM

delete from @IDList where ID = @fk_MedicationOrderId
http://www.gucciusaoutlet.net/index.php?main_page=index

by uggs on sale on Monday, July 19th 2010 at 04:28 AM

I am agree with <a href=" http://www.uggssalecheap.com" > uggs for cheap</a> idear,

Welcome to our website <a href="http://www.uggssalecheap.com" >uggs sale online</a>;

<a href="http://www.uggssalecheap.com" >uggs on sale</a>;

by uggs on sale on Monday, July 19th 2010 at 04:30 AM

I am agree with <a href=" http://www.uggssalecheap.com" > uggs for cheap</a> idear,

Welcome to our website <a href="http://www.uggssalecheap.com" >uggs sale online</a>;

<a href="http://www.uggssalecheap.com" >uggs on sale</a>;

by swiss replica watch on Tuesday, August 17th 2010 at 05:02 AM


I am a short girl, and I am afraid of wearing replica day date watches seeing as long seeing as the summer comes. I am not confident at all because of my short legs. Later I find that actually short girls could also wear swiss replica watches at any time seeing as long seeing as they know the skills to complement their clothes, what’s more, they could also appear taller by proper matching ways.
<a href="http://www.watcheslux.com/tissot-watches.html">Tissot replica watches</a>


Comment Comment on this tutorial
Name: Email:
Message:
Comment Related Tutorials
There are no related tutorials.

Comment Related Source Code
There is no related source code.

Jobs SQL Job Search
My skills include:
Enter a City:

Select a State:


Advanced Search >>
Sponsors
Discover Geekpedia

Other Resources