﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / Discuss Content Posted by Clinton Herring / Article Discussions / Article Discussions by Author  / Replacing Cursors and While Loops / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Wed, 22 May 2013 19:17:43 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Replacing Cursors and While Loops</title><link>http://www.sqlservercentral.com/Forums/Topic195535-238-1.aspx</link><description>&lt;P&gt;Perhaps this has already been asked (or scoffed at for being 'undocumented') but have you considered a function using COALESCE to return your pivoted series?&lt;/P&gt;&lt;P&gt;CREATE TABLE Test (id INT,val VARCHAR(10))&lt;/P&gt;&lt;P&gt;INSERT Test (id, val) VALUES(56, 'run')INSERT Test (id, val) VALUES(56, 'jump')INSERT Test (id, val) VALUES(56, 'scale')INSERT Test (id, val) VALUES(57, 'fly')INSERT Test (id, val) VALUES(57, 'swim')INSERT Test (id, val) VALUES(57, 'crawl')&lt;/P&gt;&lt;P&gt;GO&lt;/P&gt;&lt;P&gt;CREATE FUNCTION uf_pivot_series (@id INT)RETURNS VARCHAR(8000)BEGINDECLARE @return VARCHAR(8000)DECLARE @delimiter CHAR(2)SET @delimiter = ', 'SELECT @return = COALESCE(@return + @delimiter, '') + val FROM TestWHERE id = @id&lt;/P&gt;&lt;P&gt;RETURN @returnENDGOSELECT id,dbo.uf_pivot_series(id) as valFROM Test&lt;/P&gt;</description><pubDate>Mon, 09 Apr 2007 14:00:00 GMT</pubDate><dc:creator>Doug Hiwiller</dc:creator></item><item><title>RE: Replacing Cursors and While Loops</title><link>http://www.sqlservercentral.com/Forums/Topic195535-238-1.aspx</link><description>Hi Herring,Below is my example to illustrate my issue.From your article, I know how to use function toupdate using select statement.But I can't think on how to insert new valuesusing select statement. Please illustrate withexample to guide me. How to convert my exampleinto using select statement inserting and updating..Thank you.declare cur_insert CURSOR FORselect ..... FROM &lt;some table&gt;where xxxxWhile(xxx)begin        if(xxx)          Insert into &lt;some table&gt; values(xxxx)        else          update &lt;some table&gt;end</description><pubDate>Sun, 07 May 2006 10:48:00 GMT</pubDate><dc:creator>Vladimir Sim</dc:creator></item><item><title>RE: Replacing Cursors and While Loops</title><link>http://www.sqlservercentral.com/Forums/Topic195535-238-1.aspx</link><description>You can of course use the Values clause but if you want to call functions to generate some new data then use the select statement...insert into &gt;table&lt; (&gt;columnlist&lt;)select value1, value2, &gt;function&lt;, &gt;etc. until column list is matched&lt;</description><pubDate>Sun, 07 May 2006 00:44:00 GMT</pubDate><dc:creator>Clinton Herring</dc:creator></item><item><title>RE: Replacing Cursors and While Loops</title><link>http://www.sqlservercentral.com/Forums/Topic195535-238-1.aspx</link><description>Hi Herring,I read with interest on this topic of the 'replacing cursor and while loop'.I faced the same problem on having lot of cursor which I replace with loop, but didn't solve my execution time.Your article really help me to look at different way to replace cursor with select statment. I know how to useselect for updating data using select method from yourarticle.But I cannot think of any good way to insert new datausing select statement. Do you have any good way toinsert new data using the select statement which I replacefrom cursor.Please advise. thank you</description><pubDate>Sun, 07 May 2006 00:08:00 GMT</pubDate><dc:creator>Vladimir Sim</dc:creator></item><item><title>RE: Replacing Cursors and While Loops</title><link>http://www.sqlservercentral.com/Forums/Topic195535-238-1.aspx</link><description>Good thoughts Mark. Thanks for your input.</description><pubDate>Tue, 11 Apr 2006 06:58:00 GMT</pubDate><dc:creator>Clinton Herring</dc:creator></item><item><title>RE: Replacing Cursors and While Loops</title><link>http://www.sqlservercentral.com/Forums/Topic195535-238-1.aspx</link><description>&lt;P&gt;It's just worth pointing out that by default how a cursor and while loop work are different from a transaction view point. A while will execute as a complete transaction - which might bring about its own problems. ( this assumes no explicit transactions defined )&lt;/P&gt;&lt;P&gt;Just an observation - I like replacing cursors with while loops but you do have to be careful some times.&lt;/P&gt;</description><pubDate>Mon, 10 Apr 2006 06:43:00 GMT</pubDate><dc:creator>colin.Leversuch-Roberts</dc:creator></item><item><title>RE: Replacing Cursors and While Loops</title><link>http://www.sqlservercentral.com/Forums/Topic195535-238-1.aspx</link><description>&lt;P&gt;Here is how a highly available database would look.  I work on databases with hundreds of thousands of users on databases unconceivable large to most.  At my job location, we don't even have a nightly processing time (global app).  &lt;/P&gt;&lt;P&gt;Whenever I read posts on this and most other sites, I have to keep in mind that they have a low concurrency of users.  As a user base grows, most will have a an increasing number of problems they can't identify over time.&lt;/P&gt;&lt;P&gt;The following code is focused on removing cursors, actually a lot of code should be added for error checking, but for simplicity and keeping to the topic it is left out.  While loops in our environment are a necessary evil to keep the number of rows being updated down to a small number so they can complete and release locks within 4 seconds (our apps rule; should actually be lower; perhaps 3).  Think availablity first, then speed.  At my work location, we even have variations of this that loop through rows in blocks of 5, 10, or whatever higher number still allows us to complete in less than 4 seconds.  As you can see, it is a completely different mind set.&lt;/P&gt;&lt;P&gt;--------------------&lt;/P&gt;&lt;P&gt;Declare @tmp_id intDeclare @prev_id intDeclare @tmp_values varchar(20)&lt;/P&gt;&lt;P&gt;Set LOCK_TIMEOUT = 12000  -- assuming all code in app is tested to have                                        -- no update take longer than 4 seconds                                         Set @prev_id = -1  -- assumes all ID's are positive in nature&lt;/P&gt;&lt;P&gt;While Exists (                     SELECT TOP 1 @tmp_id = id from DB.dbo.OutPut_tbl WITH(NOLOCK)                    Where id &amp;gt; @prev_id     -- retrieve next row &amp;gt; than the previous                    order by id                   -- key data ; preferable clustered ;                                                       -- order by id to maintain                                                        -- a reference point                )BEGIN      --Direct column pivot into a variable      -- I don't know enough about the column_out field; but lets hope we never       -- increase in size greater than 20; declarations and table field would       -- need to be changed if that       -- was possible.      SELECT @tmp_values = @tmp_values + convert(varchar(20), column_data) + ','      FROM DB.dbo.many_tbl WITH(NOLOCK)       WHERE id = @tmp_id        UPDATE DB.dbo.OutPut_tbl WITH(ROWLOCK) -- assuming that this will take                                                                    -- less than 4 seconds                                                                   -- otherwise create another                                                                    -- while exists loop      SET column_out = Left(@tmp_values,20)      WHERE id = @tmp_id&lt;/P&gt;&lt;P&gt;      SET @prev_id = @tmp_id  -- move our pointer to the next row&lt;/P&gt;&lt;P&gt;END -- While Exists&lt;/P&gt;</description><pubDate>Fri, 07 Apr 2006 11:01:00 GMT</pubDate><dc:creator>Mark Bolen</dc:creator></item><item><title>RE: Replacing Cursors and While Loops</title><link>http://www.sqlservercentral.com/Forums/Topic195535-238-1.aspx</link><description>&lt;P&gt;I hope there are not many people hitting this table. This may be OK as long as it is a nightly run that you know you are the only code running against it.  &lt;/P&gt;&lt;P&gt;However long it runs, it will be locked during this process.  Although fast, for databases with a lot of people expecting to update it, it would be unacceptable unless you can get it to run in less than 4 seconds (you last said 26 seconds I believe) and lock timeout's are set well above that.  Speed is not always the only consideration unless you are running exclusively!  In which case I would put a table lock hint on it to speed it up even more.  I would recommend taking a little longer time to make sure you don't have a table lock for a half minute.  &lt;/P&gt;&lt;P&gt;Mark &lt;/P&gt;</description><pubDate>Fri, 07 Apr 2006 08:21:00 GMT</pubDate><dc:creator>Mark Bolen</dc:creator></item><item><title>RE: Replacing Cursors and While Loops</title><link>http://www.sqlservercentral.com/Forums/Topic195535-238-1.aspx</link><description>Just a couple of things I've found useful. If you have to loop generally it's quicker to use a while loop selecting each record individually rather than a cursor. Also if you find you have very large SQL statement it often runs much quicker broken up into smaller ones. If each calculation can be broken up into individual set based queries I'm sure you'll improve the performance.</description><pubDate>Fri, 07 Apr 2006 05:32:00 GMT</pubDate><dc:creator>Phil.Nicholas</dc:creator></item><item><title>RE: Replacing Cursors and While Loops</title><link>http://www.sqlservercentral.com/Forums/Topic195535-238-1.aspx</link><description>&lt;P&gt;Happend to see this while browsing . thought it might be relevant here. have a look&lt;/P&gt;&lt;P&gt;&lt;A href="http://www.sqlteam.com/item.asp?ItemID=2368"&gt;http://www.sqlteam.com/item.asp?ItemID=2368&lt;/A&gt;&lt;/P&gt;&lt;P&gt;Jeswanth&lt;/P&gt;</description><pubDate>Mon, 25 Jul 2005 00:14:00 GMT</pubDate><dc:creator>Jeswanth Jaishanker</dc:creator></item><item><title>RE: Replacing Cursors and While Loops</title><link>http://www.sqlservercentral.com/Forums/Topic195535-238-1.aspx</link><description>All,I just yesterday used the techique at a contract client with similar results although not quite as dramatic as the one I wrote about. To be fair though, there is a limit to how well this may work. After I wrote the article I came across another update that was being handled with a cursor. Within the cursor 18 columns had to be updated while pivoting data. I found I only needed 7 functions to perform the 18 updates but by the time all thoses calls were made for 2.2 milllion records, the new process clock out at the same speed as the old one. I tested individual calls and they were very short but that many had an accumulative affect. The good side though is that the code is so much simpler and easier to read that I plan to replace the old code anyway. Have fun building your functions.</description><pubDate>Tue, 12 Jul 2005 07:15:00 GMT</pubDate><dc:creator>Clinton Herring</dc:creator></item><item><title>RE: Replacing Cursors and While Loops</title><link>http://www.sqlservercentral.com/Forums/Topic195535-238-1.aspx</link><description>You are correct for the code I listed in the article. I did change it in the final results here at work to remove the last comma.</description><pubDate>Tue, 12 Jul 2005 07:09:00 GMT</pubDate><dc:creator>Clinton Herring</dc:creator></item><item><title>RE: Replacing Cursors and While Loops</title><link>http://www.sqlservercentral.com/Forums/Topic195535-238-1.aspx</link><description>&lt;P&gt;I've had similar experiences with improving cursors.  I had to look at someone's report because they weren't getting good data from it anymore.  I ran the existing proc on my machine just to see what it would produce.  5 minutes later I hadn't gotten a result, and I had started to read what they were doing and understood why they weren't getting fast results, but still didn't understand what they were trying to do.  10 minutes after that I understood what they were trying to do, their proc is still running.  I open up a new window and start getting answers piecemeal.&lt;/P&gt;&lt;P&gt;If I execute this select I'll get all the users they are interested in.  Yep.  If I join this information I'll find out the offices they are involved in.  Yep.  Hmmm, how am I going to include all the related offices.  Oh, this should do it, but this could loop indefinitely, before I start this, put a different stop in the loop.  (Came in handy when I blew the data comparison later and caused the infinite loop.)&lt;/P&gt;&lt;P&gt;Anyway, by the end of the day, I had reproduced the report information (including the information the original proc was dropping.) that they wanted.  I left my workstation up overnight because I still hadn't gotten an answer from my machine in 6 hours of running.&lt;/P&gt;&lt;P&gt;Next morning I killed the 20 hour process, wrote up a new proc and got a report in 30 seconds.  I heard the old processes ran in 4 hours on the faster production machine for 1 DB and 2 hours for another DB.  (Custom processes for each DB.)  My proc gets reports from both DBs in 28 seconds.&lt;/P&gt;</description><pubDate>Mon, 11 Jul 2005 17:07:00 GMT</pubDate><dc:creator>Kenneth Lee</dc:creator></item><item><title>RE: Replacing Cursors and While Loops</title><link>http://www.sqlservercentral.com/Forums/Topic195535-238-1.aspx</link><description>&lt;P&gt;After the pivot the results data would look like this...ID Result-- ----------------56 run, jump, scale&lt;/P&gt;&lt;P&gt;No, not really, it should look like:&lt;/P&gt;&lt;P&gt;ID Result-- ----------------56 run,jump,scale,&lt;/P&gt;&lt;P&gt;That's 15 characters long, and if the return statement was:&lt;/P&gt;&lt;P&gt;return substring(@value,1,LEN(@value)-1)&lt;/P&gt;&lt;P&gt;you could drop the comma at the end.  That's assuming your string including the last ',' character was 20 or under characters long.&lt;/P&gt;</description><pubDate>Mon, 11 Jul 2005 16:42:00 GMT</pubDate><dc:creator>Kenneth Lee</dc:creator></item><item><title>RE: Replacing Cursors and While Loops</title><link>http://www.sqlservercentral.com/Forums/Topic195535-238-1.aspx</link><description>This doesn't pivot the results. I wanted the the data from several matching rows in the Many table to come back as a single value of data separated by commas in the results table.Suppose I had the following Many table...ID  DATA--  ------56  run56  jump56  scaleAfter the pivot the results data would look like this...ID  Result--  ----------------56  run, jump, scale</description><pubDate>Mon, 11 Jul 2005 10:53:00 GMT</pubDate><dc:creator>Clinton Herring</dc:creator></item><item><title>RE: Replacing Cursors and While Loops</title><link>http://www.sqlservercentral.com/Forums/Topic195535-238-1.aspx</link><description>&lt;P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;&lt;SPAN lang=EN-CA style="FONT-SIZE: 10pt; FONT-FAMILY: Verdana; mso-ansi-language: EN-CA"&gt;The problem is not really in the cursor itself.  It is in the fact that operations are not set-oriented.  Doing 1 million inserts is not the same at all as doing a single insert based on a select that returns 1 million rows and insert them in a single operation.&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;&lt;SPAN lang=EN-CA style="FONT-SIZE: 10pt; FONT-FAMILY: Verdana; mso-ansi-language: EN-CA"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;&lt;SPAN lang=EN-CA style="FONT-SIZE: 10pt; FONT-FAMILY: Verdana; mso-ansi-language: EN-CA"&gt;I converted recently a transact-sql batch that was doing about 8 million inserts. The execution time was about 8 hours. By making a big query using derived tables and case statement to apply some conversion/extraction logic in the query, and union all to bring together rows obtains under disjoin logic context, this time reduced from 8 hours to 30 minutes (a 16 to 1 improvement). By using such query it was also possible to reduce logging by doing a select INTO.&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;&lt;SPAN lang=EN-CA style="FONT-SIZE: 10pt; FONT-FAMILY: Verdana; mso-ansi-language: EN-CA"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;&lt;SPAN lang=EN-CA style="FONT-SIZE: 10pt; FONT-FAMILY: Verdana; mso-ansi-language: EN-CA"&gt;Even without doing a SELECT INTO, SQL Server optimize log writes operations.&lt;SPAN style="mso-spacerun: yes"&gt;  &lt;/SPAN&gt;It seems that there is a lot less logging required when doing such massive single insert than when doing it row by row. &lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;&lt;SPAN lang=EN-CA style="FONT-SIZE: 10pt; FONT-FAMILY: Verdana; mso-ansi-language: EN-CA"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;&lt;SPAN lang=EN-CA style="FONT-SIZE: 10pt; FONT-FAMILY: Verdana; mso-ansi-language: EN-CA"&gt;Cursors are an easy way to describe a job, but the net result is that it ends in row by row reads &amp;amp; writes. For example a single row read use almost all the time suitable indexes, it they exists, which may seems good. But it is very not good if you finally read all the rows from the table this way. When you match a lot of rows using a join SQL Server may evaluate that using an index is not good, and the match is going to be done with a better suited joining algorithm for this type of job. There is another optimization gain possible there.&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;&lt;SPAN lang=EN-CA style="FONT-SIZE: 10pt; FONT-FAMILY: Verdana; mso-ansi-language: EN-CA"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"&gt;&lt;SPAN lang=EN-CA style="FONT-SIZE: 10pt; FONT-FAMILY: Verdana; mso-ansi-language: EN-CA"&gt;The only problem with this approach, is that you have to work harder to define the big “do it all” query.&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;</description><pubDate>Mon, 11 Jul 2005 10:35:00 GMT</pubDate><dc:creator>Maurice Pelchat</dc:creator></item><item><title>RE: Replacing Cursors and While Loops</title><link>http://www.sqlservercentral.com/Forums/Topic195535-238-1.aspx</link><description>Stephen,In my actual code the data needed to be order in the pivoted result set. If one doesn't care about the order then your correct, the order by is not needed.Clint</description><pubDate>Mon, 11 Jul 2005 10:18:00 GMT</pubDate><dc:creator>Clinton Herring</dc:creator></item><item><title>RE: Replacing Cursors and While Loops</title><link>http://www.sqlservercentral.com/Forums/Topic195535-238-1.aspx</link><description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;Good Article!&lt;/P&gt;&lt;P&gt;In my opinion cursor usage should depend on "How Often". Cursors provide an easy programming solution when the extensive row processing should be done. If something should be done only a couple of times like in one time data migration tasks, I ususally opt for cursors. If something has to run every day and takes a lot of time than the set approach should be researched and maybe some database architecture changes including de-normalizing the database should be done. &lt;/P&gt;&lt;P&gt;Yelena&lt;/P&gt;</description><pubDate>Mon, 11 Jul 2005 08:49:00 GMT</pubDate><dc:creator>Yelena Varshal</dc:creator></item><item><title>RE: Replacing Cursors and While Loops</title><link>http://www.sqlservercentral.com/Forums/Topic195535-238-1.aspx</link><description>&lt;P&gt;In addition to what was already stated about creating a temp table to hold intermediate results, if your calculations are conditional using IF..ELSE constructs, you might look at converting them to CASE expressions and/or modifying your WHERE clauses.&lt;/P&gt;&lt;P&gt;Without seeing your DDL it's impossible to get much more detailed than that.&lt;/P&gt;&lt;P&gt; &lt;/P&gt;</description><pubDate>Mon, 11 Jul 2005 08:23:00 GMT</pubDate><dc:creator>Mike C</dc:creator></item><item><title>RE: Replacing Cursors and While Loops</title><link>http://www.sqlservercentral.com/Forums/Topic195535-238-1.aspx</link><description>&lt;P&gt;Great article.  A lot of people make the mistake of trying to use SQL as a procedural language instead of a declarative language.  It's hard for some to think in terms of SQL set-based statements as opposed to iterative looping and cursors.&lt;/P&gt;&lt;P&gt;I'd estimate that cursors could be eliminated and replaced with set-based SQL statements 99.9999% of the time that they are used.&lt;/P&gt;</description><pubDate>Mon, 11 Jul 2005 08:12:00 GMT</pubDate><dc:creator>Mike C</dc:creator></item><item><title>RE: Replacing Cursors and While Loops</title><link>http://www.sqlservercentral.com/Forums/Topic195535-238-1.aspx</link><description>It would be best if you started a new thread for this question.  You will get more attention to it and you'll be able to provide more details.Could you provide the table definition (even if you change the names). Some sample data and the expected outputs.  I'm thinking at the moment that you could do a first select statement that would do the 100 calculations and insert that data in a temp table. Then you could do your updates/inserts using that temp table.  It should run 10/50 times faster that way.</description><pubDate>Mon, 11 Jul 2005 07:59:00 GMT</pubDate><dc:creator>Ninja's_RGR'us</dc:creator></item><item><title>RE: Replacing Cursors and While Loops</title><link>http://www.sqlservercentral.com/Forums/Topic195535-238-1.aspx</link><description>&lt;P&gt;Recently I have had to create an admin task that needs to be run on our 24gb database each day.  Unfortunately due to our company security I cannot show the DDL but will do my best to explain...&lt;/P&gt;&lt;P&gt;In this task (50000 lines of TSQL) I have had to use several cursors to complete the task.  This task is broken down into several smaller or sub tasks to perform the entire operation.&lt;/P&gt;&lt;P&gt;Though I admit I have not had the chance to fully analyze the task as yet to identify scans and the like (which I will get to shortly) however I wish to try and rid myself of the cursors first.&lt;/P&gt;&lt;P&gt;In one 'sub' task I managed to do this by converting a cursor into a simple insert statement:&lt;/P&gt;&lt;P&gt;Insert Into dbo.tblSelect col1, col2, col3From dbo.workingtbl&lt;/P&gt;&lt;P&gt;This managed to cut time down from 11 minutes to 3.  Working with approx 500,000 rows in the sql above.&lt;/P&gt;&lt;P&gt; &lt;/P&gt;&lt;P&gt;The remaining cursors are becoming a little hard to work our.  In the cursor I open approximately 20 variables which are used throughout the loop..&lt;/P&gt;&lt;P&gt;Declare #x cursor forselect col1, col2... col20from dbo.tableorder by col1fetch next from #x into @var1, @var2... @var20while.....  (you all know the rest)In the while loop above I perform close to 100 calculations, 5 to 10 updates and inserts etc.... &lt;/P&gt;&lt;P&gt;With this many calculations and updates/inserts could anyone suggest a better method of this loop without using a cursor?  Or perhaps this is one of those isolated cases where a cursor is best?&lt;/P&gt;&lt;P&gt;Either way, any suggestions would be great!&lt;/P&gt;</description><pubDate>Mon, 11 Jul 2005 07:51:00 GMT</pubDate><dc:creator>Anubis-237029</dc:creator></item><item><title>RE: Replacing Cursors and While Loops</title><link>http://www.sqlservercentral.com/Forums/Topic195535-238-1.aspx</link><description>&lt;P&gt;If you don't believe this example take a look at the example I posted which Noeld and Remi helped me convert:&lt;/P&gt;&lt;P&gt;&lt;A href="http://www.sqlservercentral.com/forums/shwmessage.aspx?forumid=8&amp;amp;messageid=197948"&gt;http://www.sqlservercentral.com/forums/shwmessage.aspx?forumid=8&amp;amp;messageid=197948&lt;/A&gt;&lt;/P&gt;&lt;P&gt;This went from 2 hours to 6:46 to 0:26 in the final iteration!!&lt;/P&gt;</description><pubDate>Mon, 11 Jul 2005 05:53:00 GMT</pubDate><dc:creator>rschaeferhig</dc:creator></item><item><title>RE: Replacing Cursors and While Loops</title><link>http://www.sqlservercentral.com/Forums/Topic195535-238-1.aspx</link><description>&lt;P&gt;I can't see the purpose of the ORDER BY clause.  If you were to remove this from your select statement, you may find that you improve the performance.  It may also be the case that the outer loop has an ORDER BY clause that can also be removed.&lt;/P&gt;&lt;P&gt;Profile the procedure to determine what indexes are being used on the tables.  Ensure that the indexes in use are the most efficient. You may be able to improve the performance yet again by creating covering indexes.&lt;/P&gt;</description><pubDate>Mon, 11 Jul 2005 03:16:00 GMT</pubDate><dc:creator>Stephen Bailey</dc:creator></item><item><title>RE: Replacing Cursors and While Loops</title><link>http://www.sqlservercentral.com/Forums/Topic195535-238-1.aspx</link><description>&lt;P&gt;Try this code. you might save remaining 12 Minutes also.. &lt;img src='images/emotions/smile.gif' height='20' width='20' border='0' title='Smile' align='absmiddle'&gt;&lt;/P&gt;&lt;P&gt;UPDATE BSET b.id = A.idFROM dbo.many_tbl A inner join dbo.OutpUt_tbl B ON A.id = B.ID&lt;/P&gt;&lt;P&gt; &lt;/P&gt;&lt;P&gt;Jeswanth&lt;/P&gt;</description><pubDate>Mon, 11 Jul 2005 01:09:00 GMT</pubDate><dc:creator>Jeswanth Jaishanker</dc:creator></item><item><title>Replacing Cursors and While Loops</title><link>http://www.sqlservercentral.com/Forums/Topic195535-238-1.aspx</link><description>Comments posted to this topic are about the content posted at &lt;A HREF="http://www.sqlservercentral.com/columnists/cherring/replacingcursorsandwhileloops.asp"&gt;http://www.sqlservercentral.com/columnists/cherring/replacingcursorsandwhileloops.asp&lt;/A&gt;</description><pubDate>Wed, 29 Jun 2005 16:16:00 GMT</pubDate><dc:creator>Clinton Herring</dc:creator></item></channel></rss>