﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2005 / SQL Server 2005 General Discussion  / Better logic than cursor / 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>Thu, 20 Jun 2013 03:16:58 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Better logic than cursor</title><link>http://www.sqlservercentral.com/Forums/Topic1425834-149-1.aspx</link><description>[quote][b]Compassionate (3/2/2013)[/b][hr]Hi All,I have two tables Tab1 and Tab2.For each row in Tab1, the table Tab2 contains multiple rows.Important columns in both tables are C0,C1 and C2. So i have to update the C1 and C2 columns in Tab1 with count(C1) and max(C2) for each C0.assume C0="Temp" and Tab2 has 10 rows for Temp.So Tab1 should be updated with C1 as 10 and C2 asmax(C2) in Tab2.so i wrote a cursor as below.It takes 1 min for two rows in Tab1.Any better solution ?declare T1_cursor CURSOR for 	select distinct C0 from Tab1			OPEN T1_cursor		FETCH NEXT FROM T1_cursor 	INTO @C0				WHILE @@FETCH_STATUS = 0	BEGIN		select @recordCount=count(*)+1,@maxTime=max(maxtime) from Tab2		where C0=@C0					update T1 set RecordCount=@recordCount , Maxtime=@maxTime where C0=@C0			FETCH NEXT FROM T1_cursor 	INTO @C0		   	END  -- End of cursor		CLOSE T1_cursor	DEALLOCATE T1_cursor[/quote]I'm really concerned for your system because even a cursor isn't normally as slow as that.  There's something really funky going on if it's only doing 2 rows a minute.  Perhaps it's just some really big tables that have no indexes.  If that's true, even the following set based code may be slower than desired.And, no... haven't tested it because you didn't post any readily consumable data to test with.  Follow the link Lynn pointed to.  You'll get much quicker responses and even tested coded answers if you follow the suggestions at that article.[code="sql"] UPDATE t1    SET RecordCount = preagg.RecordCount,        MaxTime     = preagg.MaxTime   FROM dbo.Tab1 t1   JOIN (         SELECT CO, RecordCount = COUNT(*)+1, MaxTime = MAX(MaxTime)           FROM dbo.Tab2          GROUP BY CO        ) preagg     ON preagg.CO = t1.CO;[/code]</description><pubDate>Sat, 02 Mar 2013 13:43:20 GMT</pubDate><dc:creator>Jeff Moden</dc:creator></item><item><title>RE: Better logic than cursor</title><link>http://www.sqlservercentral.com/Forums/Topic1425834-149-1.aspx</link><description>Actually, yes.  Don't use a cursor.  Oh, wait, you would like some actual code here wouldn't you?Okay, we can that once you post the DDL (CREATE TABLE statements) for the tables, some sample data (as INSERT INTO statements) for each of the tables, the expected results based on the sample data provided (usually best provided as another table and series of insert statements to make testing/comparisons easier).Need help with this, read the first article I have referenced below in my signature block.  It will help you with everything you need to do to get the best possible answers quickly.</description><pubDate>Sat, 02 Mar 2013 11:59:15 GMT</pubDate><dc:creator>Lynn Pettis</dc:creator></item><item><title>Better logic than cursor</title><link>http://www.sqlservercentral.com/Forums/Topic1425834-149-1.aspx</link><description>Hi All,I have two tables Tab1 and Tab2.For each row in Tab1, the table Tab2 contains multiple rows.Important columns in both tables are C0,C1 and C2. So i have to update the C1 and C2 columns in Tab1 with count(C1) and max(C2) for each C0.assume C0="Temp" and Tab2 has 10 rows for Temp.So Tab1 should be updated with C1 as 10 and C2 asmax(C2) in Tab2.so i wrote a cursor as below.It takes 1 min for two rows in Tab1.Any better solution ?declare T1_cursor CURSOR for 	select distinct C0 from Tab1			OPEN T1_cursor		FETCH NEXT FROM T1_cursor 	INTO @C0				WHILE @@FETCH_STATUS = 0	BEGIN		select @recordCount=count(*)+1,@maxTime=max(maxtime) from Tab2		where C0=@C0					update T1 set RecordCount=@recordCount , Maxtime=@maxTime where C0=@C0			FETCH NEXT FROM T1_cursor 	INTO @C0		   	END  -- End of cursor		CLOSE T1_cursor	DEALLOCATE T1_cursor</description><pubDate>Sat, 02 Mar 2013 02:06:06 GMT</pubDate><dc:creator>Compassionate</dc:creator></item></channel></rss>