<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: SQL SERVER &#8211; Simple Example of WHILE Loop with BREAK and CONTINUE</title>
	<atom:link href="http://blog.sqlauthority.com/2008/02/04/sql-server-simple-example-of-while-loop-with-break-and-continue/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.sqlauthority.com/2008/02/04/sql-server-simple-example-of-while-loop-with-break-and-continue/</link>
	<description>Personal Notes of Pinal Dave</description>
	<lastBuildDate>Fri, 17 May 2013 15:26:57 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
	<item>
		<title>By: SQL SERVER &#8211; Weekly Series &#8211; Memory Lane &#8211; #015 &#171; SQL Server Journey with SQL Authority</title>
		<link>http://blog.sqlauthority.com/2008/02/04/sql-server-simple-example-of-while-loop-with-break-and-continue/#comment-419677</link>
		<dc:creator><![CDATA[SQL SERVER &#8211; Weekly Series &#8211; Memory Lane &#8211; #015 &#171; SQL Server Journey with SQL Authority]]></dc:creator>
		<pubDate>Sat, 09 Feb 2013 01:30:24 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=495#comment-419677</guid>
		<description><![CDATA[[...] Simple Example of WHILE Loop with BREAK and CONTINUE One of the most popular blog posts of my blog has been from this week. Lots of people do not know how to run the WHILE loop in SQL Server and this blog post precisely describes the same. [...]]]></description>
		<content:encoded><![CDATA[<p>[...] Simple Example of WHILE Loop with BREAK and CONTINUE One of the most popular blog posts of my blog has been from this week. Lots of people do not know how to run the WHILE loop in SQL Server and this blog post precisely describes the same. [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: sql tutorial</title>
		<link>http://blog.sqlauthority.com/2008/02/04/sql-server-simple-example-of-while-loop-with-break-and-continue/#comment-341370</link>
		<dc:creator><![CDATA[sql tutorial]]></dc:creator>
		<pubDate>Thu, 06 Sep 2012 06:39:57 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=495#comment-341370</guid>
		<description><![CDATA[forgive me,  I forgot to say 
thanks a lot  to Mr SqlAuthority]]></description>
		<content:encoded><![CDATA[<p>forgive me,  I forgot to say<br />
thanks a lot  to Mr SqlAuthority</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: sql tutorial</title>
		<link>http://blog.sqlauthority.com/2008/02/04/sql-server-simple-example-of-while-loop-with-break-and-continue/#comment-341369</link>
		<dc:creator><![CDATA[sql tutorial]]></dc:creator>
		<pubDate>Thu, 06 Sep 2012 06:37:26 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=495#comment-341369</guid>
		<description><![CDATA[I try your explanation, and query running well, see detail &lt;a href=&#039;http://sqltosql.blogspot.com/search/label/sql%20while&#039; rel=&quot;nofollow&quot;&gt;http://sqltosql.blogspot.com/search/label/sql%20while&lt;/a&gt;]]></description>
		<content:encoded><![CDATA[<p>I try your explanation, and query running well, see detail <a href='http://sqltosql.blogspot.com/search/label/sql%20while' rel="nofollow">http://sqltosql.blogspot.com/search/label/sql%20while</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: krishnapriya</title>
		<link>http://blog.sqlauthority.com/2008/02/04/sql-server-simple-example-of-while-loop-with-break-and-continue/#comment-291405</link>
		<dc:creator><![CDATA[krishnapriya]]></dc:creator>
		<pubDate>Thu, 31 May 2012 05:42:53 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=495#comment-291405</guid>
		<description><![CDATA[sir,   i want to create the result as follows..
the id and date is here in tablw.i want to collect the sum of datediff in as order as employee id...in table,,date in punch in and punch out...iwant to diff alternate  table...then their sum ...which is considered as slno.. ie,
slno 1
employeeId 1
time  6(in hr)
can u plz help me sir,,]]></description>
		<content:encoded><![CDATA[<p>sir,   i want to create the result as follows..<br />
the id and date is here in tablw.i want to collect the sum of datediff in as order as employee id&#8230;in table,,date in punch in and punch out&#8230;iwant to diff alternate  table&#8230;then their sum &#8230;which is considered as slno.. ie,<br />
slno 1<br />
employeeId 1<br />
time  6(in hr)<br />
can u plz help me sir,,</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rajiv CS</title>
		<link>http://blog.sqlauthority.com/2008/02/04/sql-server-simple-example-of-while-loop-with-break-and-continue/#comment-261027</link>
		<dc:creator><![CDATA[Rajiv CS]]></dc:creator>
		<pubDate>Fri, 09 Mar 2012 08:23:07 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=495#comment-261027</guid>
		<description><![CDATA[I have problem in deleting records from the table.
Below is the requirement.

I have table UserLogins(UserId,Logindate)
Data Like
UserId Logindate
111       01/12/2012
111       02/12/2012
111       03/12/2012
111       04/12/2012
222       01/12/2012
222       02/12/2012
222       03/12/2012
222       04/12/2012
333       01/12/2012
333       02/12/2012
333       03/12/2012
333       04/12/2012

I want to delete the records from this table and I want retain only last 3 recent records for each userid.

Any help on this Appreciated.]]></description>
		<content:encoded><![CDATA[<p>I have problem in deleting records from the table.<br />
Below is the requirement.</p>
<p>I have table UserLogins(UserId,Logindate)<br />
Data Like<br />
UserId Logindate<br />
111       01/12/2012<br />
111       02/12/2012<br />
111       03/12/2012<br />
111       04/12/2012<br />
222       01/12/2012<br />
222       02/12/2012<br />
222       03/12/2012<br />
222       04/12/2012<br />
333       01/12/2012<br />
333       02/12/2012<br />
333       03/12/2012<br />
333       04/12/2012</p>
<p>I want to delete the records from this table and I want retain only last 3 recent records for each userid.</p>
<p>Any help on this Appreciated.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: eb</title>
		<link>http://blog.sqlauthority.com/2008/02/04/sql-server-simple-example-of-while-loop-with-break-and-continue/#comment-176116</link>
		<dc:creator><![CDATA[eb]]></dc:creator>
		<pubDate>Fri, 07 Oct 2011 14:14:30 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=495#comment-176116</guid>
		<description><![CDATA[DRY does not need a while loop --- it&#039;s SQL.  try:

 SELECT @SQL = @SQL +[Text] 
    FROM @tblSQL 
 WHERE id &lt;= @intCount
 ORDER BY id]]></description>
		<content:encoded><![CDATA[<p>DRY does not need a while loop &#8212; it&#8217;s SQL.  try:</p>
<p> SELECT @SQL = @SQL +[Text]<br />
    FROM @tblSQL<br />
 WHERE id &lt;= @intCount<br />
 ORDER BY id</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brijesh</title>
		<link>http://blog.sqlauthority.com/2008/02/04/sql-server-simple-example-of-while-loop-with-break-and-continue/#comment-151023</link>
		<dc:creator><![CDATA[Brijesh]]></dc:creator>
		<pubDate>Wed, 27 Jul 2011 10:14:14 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=495#comment-151023</guid>
		<description><![CDATA[Hi Dave,
My query is , im having table sale

Sale{Cleant_name,Product}
eg:
Client_name &#124;  Product
a &#124; 123
a &#124; 321
b &#124; 123
output must be in this pattern:
Client_name &#124; product1 &#124; product2
a &#124; 123 &#124; 321
b &#124; 123

Thanks in advance 
waiting for reply]]></description>
		<content:encoded><![CDATA[<p>Hi Dave,<br />
My query is , im having table sale</p>
<p>Sale{Cleant_name,Product}<br />
eg:<br />
Client_name |  Product<br />
a | 123<br />
a | 321<br />
b | 123<br />
output must be in this pattern:<br />
Client_name | product1 | product2<br />
a | 123 | 321<br />
b | 123</p>
<p>Thanks in advance<br />
waiting for reply</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: V.KIRUPANIDHI</title>
		<link>http://blog.sqlauthority.com/2008/02/04/sql-server-simple-example-of-while-loop-with-break-and-continue/#comment-141555</link>
		<dc:creator><![CDATA[V.KIRUPANIDHI]]></dc:creator>
		<pubDate>Fri, 17 Jun 2011 04:39:15 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=495#comment-141555</guid>
		<description><![CDATA[Yes. U can insert a new column using an insert script itself...]]></description>
		<content:encoded><![CDATA[<p>Yes. U can insert a new column using an insert script itself&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Imran Mohammed</title>
		<link>http://blog.sqlauthority.com/2008/02/04/sql-server-simple-example-of-while-loop-with-break-and-continue/#comment-135084</link>
		<dc:creator><![CDATA[Imran Mohammed]]></dc:creator>
		<pubDate>Wed, 18 May 2011 21:49:48 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=495#comment-135084</guid>
		<description><![CDATA[@Punia,

See, I knew this would kill performance. Use this for now. In couple of days I will update another version which I can assure you will be much faster than this.

But, I wonder, 14,000 records execution time is 4 minutes... That is worst than I expected.

What is your Server Specs ?

~ IM.]]></description>
		<content:encoded><![CDATA[<p>@Punia,</p>
<p>See, I knew this would kill performance. Use this for now. In couple of days I will update another version which I can assure you will be much faster than this.</p>
<p>But, I wonder, 14,000 records execution time is 4 minutes&#8230; That is worst than I expected.</p>
<p>What is your Server Specs ?</p>
<p>~ IM.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Punia</title>
		<link>http://blog.sqlauthority.com/2008/02/04/sql-server-simple-example-of-while-loop-with-break-and-continue/#comment-135008</link>
		<dc:creator><![CDATA[Punia]]></dc:creator>
		<pubDate>Wed, 18 May 2011 15:13:49 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=495#comment-135008</guid>
		<description><![CDATA[Thanks Imran,
This solves my problem. 
I have one more question. I have about 14,000 rows. So when i use your logic, it gives the correct result, but performance degrades (takes about 4 minutes to execute). Is there is any solution to decrease the execution time.

Thanks again for your help.
Punia]]></description>
		<content:encoded><![CDATA[<p>Thanks Imran,<br />
This solves my problem.<br />
I have one more question. I have about 14,000 rows. So when i use your logic, it gives the correct result, but performance degrades (takes about 4 minutes to execute). Is there is any solution to decrease the execution time.</p>
<p>Thanks again for your help.<br />
Punia</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Imran Mohammed</title>
		<link>http://blog.sqlauthority.com/2008/02/04/sql-server-simple-example-of-while-loop-with-break-and-continue/#comment-134945</link>
		<dc:creator><![CDATA[Imran Mohammed]]></dc:creator>
		<pubDate>Wed, 18 May 2011 05:12:30 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=495#comment-134945</guid>
		<description><![CDATA[@Punia,

One Important assumption I made here was, First C_Rank Value for any C_ID would be &#039;A&#039;. If not, you have to modify script accordingly. 

~IM.]]></description>
		<content:encoded><![CDATA[<p>@Punia,</p>
<p>One Important assumption I made here was, First C_Rank Value for any C_ID would be &#8216;A&#8217;. If not, you have to modify script accordingly. </p>
<p>~IM.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Imran Mohammed</title>
		<link>http://blog.sqlauthority.com/2008/02/04/sql-server-simple-example-of-while-loop-with-break-and-continue/#comment-134943</link>
		<dc:creator><![CDATA[Imran Mohammed]]></dc:creator>
		<pubDate>Wed, 18 May 2011 05:02:24 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=495#comment-134943</guid>
		<description><![CDATA[@Punia.

I am sure there is a better way of doing this, but below script is the quiest way....


/* 
THIS IS SETUP TABLE, WITH SOME SAMPLE DATA
*/
if exists (select 1 from tempdb.sys.objects where name like &#039;#Temp_Table%&#039;)
drop table #Temp_Table

create table #Temp_Table	
	(
		C_ID int
		, C_Rank char(1)
		, Date datetime
		, NewColumn int
	 )

insert into #Temp_Table  
	(
		C_ID
		, C_Rank 
		, Date
	)
	select 42, &#039;A&#039;, &#039;10/14/2010&#039;
	union all
	select 42, &#039;B&#039;, &#039;10/26/2010&#039;
	union all
	select 42, &#039;B&#039;, &#039;10/14/2010&#039;
	union all
	select 42, &#039;C&#039;, &#039;10/26/2010&#039;
	union all
	select 42, &#039;A&#039;,&#039;02/16/2011&#039;
	union all 
	select 43, &#039;A&#039;, &#039;12/17/2010&#039;
	union all 
	select 44, &#039;A&#039;,  &#039;07/28/2010&#039;
	union all 
	select 44, &#039;B&#039;,  &#039;08/10/2010&#039;
	union all 
	select 44, &#039;A&#039;,  &#039;01/11/2011&#039;
	union all 
	select 44, &#039;B&#039;,  &#039;01/28/2011&#039;
	union all
	select 44, &#039;C&#039;, &#039;10/14/2010&#039;
	union all
	select 44, &#039;D&#039;, &#039;10/26/2010&#039;

Select	&#039;Original Data&#039; Comment
		,* 
from	#Temp_Table

/*
 This would be Actual Script to get the New ID based on information you provided
*/		
Declare @Count int 
		,@C_ID int
		,@C_Rank char(1)
		,@total_Count int 
		,@Count_Partition int 
		,@Previous_ID int 
		
Declare @Table Table (ID  int IDENTITY(1,1), C_ID int, C_Rank char(1), Date datetime, NewColumn int )

Set @Count  = 1
Set @Count_Partition  = 0

insert	into @Table 
Select	*	
from	#Temp_Table		

Select	@total_Count = ISNULL(MAX(ID),0)
from	@Table


While @Count &lt; = @total_Count
Begin
		Select	@C_ID = C_ID
				,@C_Rank = C_Rank
		From	@Table
		Where	ID = @Count
		
		If @Count = 1 
			Set @Previous_ID = @C_ID
		
		If @Previous_ID != @C_ID
			Set @Count_Partition = 1
			
		Else If @C_Rank = &#039;A&#039;
			Set @Count_Partition = @Count_Partition + 1

		update 	@Table
				Set	NewColumn = @Count_Partition
		Where	ID = @Count
				
		Set @Previous_ID = @C_ID
		Set @Count = @Count + 1 
End 

Select	C_ID 
		, C_Rank 
		, [Date]
		, NewColumn 
from	@Table

--Drop table #Temp_Table 



~ IM.]]></description>
		<content:encoded><![CDATA[<p>@Punia.</p>
<p>I am sure there is a better way of doing this, but below script is the quiest way&#8230;.</p>
<p>/*<br />
THIS IS SETUP TABLE, WITH SOME SAMPLE DATA<br />
*/<br />
if exists (select 1 from tempdb.sys.objects where name like &#8216;#Temp_Table%&#8217;)<br />
drop table #Temp_Table</p>
<p>create table #Temp_Table<br />
	(<br />
		C_ID int<br />
		, C_Rank char(1)<br />
		, Date datetime<br />
		, NewColumn int<br />
	 )</p>
<p>insert into #Temp_Table<br />
	(<br />
		C_ID<br />
		, C_Rank<br />
		, Date<br />
	)<br />
	select 42, &#8216;A&#8217;, &#8217;10/14/2010&#8242;<br />
	union all<br />
	select 42, &#8216;B&#8217;, &#8217;10/26/2010&#8242;<br />
	union all<br />
	select 42, &#8216;B&#8217;, &#8217;10/14/2010&#8242;<br />
	union all<br />
	select 42, &#8216;C&#8217;, &#8217;10/26/2010&#8242;<br />
	union all<br />
	select 42, &#8216;A&#8217;,&#8217;02/16/2011&#8242;<br />
	union all<br />
	select 43, &#8216;A&#8217;, &#8217;12/17/2010&#8242;<br />
	union all<br />
	select 44, &#8216;A&#8217;,  &#8217;07/28/2010&#8242;<br />
	union all<br />
	select 44, &#8216;B&#8217;,  &#8217;08/10/2010&#8242;<br />
	union all<br />
	select 44, &#8216;A&#8217;,  &#8217;01/11/2011&#8242;<br />
	union all<br />
	select 44, &#8216;B&#8217;,  &#8217;01/28/2011&#8242;<br />
	union all<br />
	select 44, &#8216;C&#8217;, &#8217;10/14/2010&#8242;<br />
	union all<br />
	select 44, &#8216;D&#8217;, &#8217;10/26/2010&#8242;</p>
<p>Select	&#8216;Original Data&#8217; Comment<br />
		,*<br />
from	#Temp_Table</p>
<p>/*<br />
 This would be Actual Script to get the New ID based on information you provided<br />
*/<br />
Declare @Count int<br />
		,@C_ID int<br />
		,@C_Rank char(1)<br />
		,@total_Count int<br />
		,@Count_Partition int<br />
		,@Previous_ID int </p>
<p>Declare @Table Table (ID  int IDENTITY(1,1), C_ID int, C_Rank char(1), Date datetime, NewColumn int )</p>
<p>Set @Count  = 1<br />
Set @Count_Partition  = 0</p>
<p>insert	into @Table<br />
Select	*<br />
from	#Temp_Table		</p>
<p>Select	@total_Count = ISNULL(MAX(ID),0)<br />
from	@Table</p>
<p>While @Count &lt; = @total_Count<br />
Begin<br />
		Select	@C_ID = C_ID<br />
				,@C_Rank = C_Rank<br />
		From	@Table<br />
		Where	ID = @Count</p>
<p>		If @Count = 1<br />
			Set @Previous_ID = @C_ID</p>
<p>		If @Previous_ID != @C_ID<br />
			Set @Count_Partition = 1</p>
<p>		Else If @C_Rank = &#039;A&#039;<br />
			Set @Count_Partition = @Count_Partition + 1</p>
<p>		update 	@Table<br />
				Set	NewColumn = @Count_Partition<br />
		Where	ID = @Count</p>
<p>		Set @Previous_ID = @C_ID<br />
		Set @Count = @Count + 1<br />
End </p>
<p>Select	C_ID<br />
		, C_Rank<br />
		, [Date]<br />
		, NewColumn<br />
from	@Table</p>
<p>&#8211;Drop table #Temp_Table </p>
<p>~ IM.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Punia</title>
		<link>http://blog.sqlauthority.com/2008/02/04/sql-server-simple-example-of-while-loop-with-break-and-continue/#comment-134886</link>
		<dc:creator><![CDATA[Punia]]></dc:creator>
		<pubDate>Tue, 17 May 2011 19:58:51 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=495#comment-134886</guid>
		<description><![CDATA[Hi Pinal and Forum,

Your blog is very helpful. But i am stuck with something different. I have table having following columns.

C_ID     C_ Rank    NewColumn(Cycle)     Date
42	A	1	October 14, 2010
42	B	1	October 26, 2010
42	A	2	February 16, 2011
			
43	A	1	December 17, 2010
			
44	A	1	July 28, 2010
44	B	1	August 10, 2010
44	A	2	January 11, 2011
44	B	2	January 28, 2011
			
45	A	1	July 30, 2010
45	B	1	August 9, 2010
45	B	1	September 24, 2010
45	A	2	April 5, 2011
45	B	2	April 26, 2011

Actually table has three columns(C_ID, C_rank, Date). I want to generate one more column called Cycle in such a way that for each C_ID, it should generate the number start from one and increment the number from next C_rank &#039;A&#039; (a shown above).
I tried using row_number, but no luck.
May be some loop option till next C_Rank &#039;A&#039; works.
This is urgent. 
Any help is appreciated.

Thanks.]]></description>
		<content:encoded><![CDATA[<p>Hi Pinal and Forum,</p>
<p>Your blog is very helpful. But i am stuck with something different. I have table having following columns.</p>
<p>C_ID     C_ Rank    NewColumn(Cycle)     Date<br />
42	A	1	October 14, 2010<br />
42	B	1	October 26, 2010<br />
42	A	2	February 16, 2011</p>
<p>43	A	1	December 17, 2010</p>
<p>44	A	1	July 28, 2010<br />
44	B	1	August 10, 2010<br />
44	A	2	January 11, 2011<br />
44	B	2	January 28, 2011</p>
<p>45	A	1	July 30, 2010<br />
45	B	1	August 9, 2010<br />
45	B	1	September 24, 2010<br />
45	A	2	April 5, 2011<br />
45	B	2	April 26, 2011</p>
<p>Actually table has three columns(C_ID, C_rank, Date). I want to generate one more column called Cycle in such a way that for each C_ID, it should generate the number start from one and increment the number from next C_rank &#8216;A&#8217; (a shown above).<br />
I tried using row_number, but no luck.<br />
May be some loop option till next C_Rank &#8216;A&#8217; works.<br />
This is urgent.<br />
Any help is appreciated.</p>
<p>Thanks.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: rafael greca</title>
		<link>http://blog.sqlauthority.com/2008/02/04/sql-server-simple-example-of-while-loop-with-break-and-continue/#comment-121560</link>
		<dc:creator><![CDATA[rafael greca]]></dc:creator>
		<pubDate>Wed, 02 Mar 2011 18:37:49 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=495#comment-121560</guid>
		<description><![CDATA[Boa tarde, 

como faz qual formulas horario para turno? exemplo

hora --- turno
6:15 --    1
9:00 --    1
15:50 --  2
23:05 --  3]]></description>
		<content:encoded><![CDATA[<p>Boa tarde, </p>
<p>como faz qual formulas horario para turno? exemplo</p>
<p>hora &#8212; turno<br />
6:15 &#8212;    1<br />
9:00 &#8212;    1<br />
15:50 &#8212;  2<br />
23:05 &#8212;  3</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: enricoinoz</title>
		<link>http://blog.sqlauthority.com/2008/02/04/sql-server-simple-example-of-while-loop-with-break-and-continue/#comment-111551</link>
		<dc:creator><![CDATA[enricoinoz]]></dc:creator>
		<pubDate>Tue, 18 Jan 2011 05:01:08 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=495#comment-111551</guid>
		<description><![CDATA[Hi Pinal Dave and Forum,

I was hoping if you guys can point me to the right direction.

I do have the following table scenario


Columns: Year, Period, MonthToDate, YearToDate

Row1: 2010, 0, 100, 100
Row2: 2010, 3, 200, 300
Row3: 2010, 9, 300, 600

I was hoping to create a While script that will insert the missing periods (up to period 12), and then place 0 for the MonthToDate, while also making sure the YearToDate values are correct.

As such, I was hoping to get the following result set:

Columns: Year, Period, MonthToDate, YearToDate

Row1: 2010, 0, 100, 100
Row2: 2010, 1, 0, 100
Row3: 2010, 2, 0, 100
Row4: 2010, 3, 200, 300
Row5: 2010, 4, 0, 300
Row6: 2010, 5, 0, 300
Row7: 2010, 6, 0, 300
Row8: 2010, 7, 0, 300
Row9: 2010, 8, 0, 300
Row10: 2010, 9, 300, 600
Row11: 2010, 10, 0, 600
Row12: 2010, 11, 0, 600
Row13: 2010, 12, 0, 600

It looks like I would also need some grouping on the script.

Any ideas you have would be greatly appreciated.

Thank you for your time.

Regards,

Eric]]></description>
		<content:encoded><![CDATA[<p>Hi Pinal Dave and Forum,</p>
<p>I was hoping if you guys can point me to the right direction.</p>
<p>I do have the following table scenario</p>
<p>Columns: Year, Period, MonthToDate, YearToDate</p>
<p>Row1: 2010, 0, 100, 100<br />
Row2: 2010, 3, 200, 300<br />
Row3: 2010, 9, 300, 600</p>
<p>I was hoping to create a While script that will insert the missing periods (up to period 12), and then place 0 for the MonthToDate, while also making sure the YearToDate values are correct.</p>
<p>As such, I was hoping to get the following result set:</p>
<p>Columns: Year, Period, MonthToDate, YearToDate</p>
<p>Row1: 2010, 0, 100, 100<br />
Row2: 2010, 1, 0, 100<br />
Row3: 2010, 2, 0, 100<br />
Row4: 2010, 3, 200, 300<br />
Row5: 2010, 4, 0, 300<br />
Row6: 2010, 5, 0, 300<br />
Row7: 2010, 6, 0, 300<br />
Row8: 2010, 7, 0, 300<br />
Row9: 2010, 8, 0, 300<br />
Row10: 2010, 9, 300, 600<br />
Row11: 2010, 10, 0, 600<br />
Row12: 2010, 11, 0, 600<br />
Row13: 2010, 12, 0, 600</p>
<p>It looks like I would also need some grouping on the script.</p>
<p>Any ideas you have would be greatly appreciated.</p>
<p>Thank you for your time.</p>
<p>Regards,</p>
<p>Eric</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: madhivanan</title>
		<link>http://blog.sqlauthority.com/2008/02/04/sql-server-simple-example-of-while-loop-with-break-and-continue/#comment-95858</link>
		<dc:creator><![CDATA[madhivanan]]></dc:creator>
		<pubDate>Tue, 26 Oct 2010 08:06:46 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=495#comment-95858</guid>
		<description><![CDATA[Note that this assignment syntax will work from version 2008 onwards]]></description>
		<content:encoded><![CDATA[<p>Note that this assignment syntax will work from version 2008 onwards</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: kumar</title>
		<link>http://blog.sqlauthority.com/2008/02/04/sql-server-simple-example-of-while-loop-with-break-and-continue/#comment-95645</link>
		<dc:creator><![CDATA[kumar]]></dc:creator>
		<pubDate>Mon, 25 Oct 2010 04:47:13 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=495#comment-95645</guid>
		<description><![CDATA[Declare @Var1 int
Declare @Var2 int
Set @Var1 = 1

While @Var1 &lt; = 10
Begin
Print &#039;This is outer loop Value is :&#039;+Convert (varchar, @Var1)
Set @Var2 = 1
While @var2 &lt;= 10
Begin
Print &#039; This is Inner Loop Value is:&#039;+Convert (varchar, @Var2)
Set @Var2 = @var2+1
End
Set @Var1 = @var1+1
End]]></description>
		<content:encoded><![CDATA[<p>Declare @Var1 int<br />
Declare @Var2 int<br />
Set @Var1 = 1</p>
<p>While @Var1 &lt; = 10<br />
Begin<br />
Print &#039;This is outer loop Value is :&#039;+Convert (varchar, @Var1)<br />
Set @Var2 = 1<br />
While @var2 &lt;= 10<br />
Begin<br />
Print &#039; This is Inner Loop Value is:&#039;+Convert (varchar, @Var2)<br />
Set @Var2 = @var2+1<br />
End<br />
Set @Var1 = @var1+1<br />
End</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: kumar</title>
		<link>http://blog.sqlauthority.com/2008/02/04/sql-server-simple-example-of-while-loop-with-break-and-continue/#comment-95644</link>
		<dc:creator><![CDATA[kumar]]></dc:creator>
		<pubDate>Mon, 25 Oct 2010 04:45:00 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=495#comment-95644</guid>
		<description><![CDATA[DECLARE @Digit TABLE (digit int NOT NULL);

SET NOCOUNT ON;
DECLARE @i int = @Start;
BEGIN TRAN
WHILE @i &lt;= @End
BEGIN
INSERT Number (Num) SELECT @I
SET @i += 1;
END
COMMIT]]></description>
		<content:encoded><![CDATA[<p>DECLARE @Digit TABLE (digit int NOT NULL);</p>
<p>SET NOCOUNT ON;<br />
DECLARE @i int = @Start;<br />
BEGIN TRAN<br />
WHILE @i &lt;= @End<br />
BEGIN<br />
INSERT Number (Num) SELECT @I<br />
SET @i += 1;<br />
END<br />
COMMIT</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Madhivanan</title>
		<link>http://blog.sqlauthority.com/2008/02/04/sql-server-simple-example-of-while-loop-with-break-and-continue/#comment-89730</link>
		<dc:creator><![CDATA[Madhivanan]]></dc:creator>
		<pubDate>Mon, 27 Sep 2010 11:17:55 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=495#comment-89730</guid>
		<description><![CDATA[So you want to get 5 duplicate entries?]]></description>
		<content:encoded><![CDATA[<p>So you want to get 5 duplicate entries?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Harish</title>
		<link>http://blog.sqlauthority.com/2008/02/04/sql-server-simple-example-of-while-loop-with-break-and-continue/#comment-89700</link>
		<dc:creator><![CDATA[Harish]]></dc:creator>
		<pubDate>Mon, 27 Sep 2010 08:04:13 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=495#comment-89700</guid>
		<description><![CDATA[My table (Bill) is as following 

Doc_no    Date                 ItemName        Qty
A1001      27/09/10             Pen                 5

Select * From Bill where Doc_No = &#039;A1001&#039;

I want to repeat above  line number of Qty Times (5 Times) with qty 1.

Plz help, thnx in advance.]]></description>
		<content:encoded><![CDATA[<p>My table (Bill) is as following </p>
<p>Doc_no    Date                 ItemName        Qty<br />
A1001      27/09/10             Pen                 5</p>
<p>Select * From Bill where Doc_No = &#8216;A1001&#8242;</p>
<p>I want to repeat above  line number of Qty Times (5 Times) with qty 1.</p>
<p>Plz help, thnx in advance.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: John Doe</title>
		<link>http://blog.sqlauthority.com/2008/02/04/sql-server-simple-example-of-while-loop-with-break-and-continue/#comment-86381</link>
		<dc:creator><![CDATA[John Doe]]></dc:creator>
		<pubDate>Fri, 03 Sep 2010 01:25:44 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=495#comment-86381</guid>
		<description><![CDATA[Isn&#039;t this exact example provided on the Microsoft reference website? :-)

http://msdn.microsoft.com/en-us/library/ms178642.aspx]]></description>
		<content:encoded><![CDATA[<p>Isn&#8217;t this exact example provided on the Microsoft reference website? :-)</p>
<p><a href="http://msdn.microsoft.com/en-us/library/ms178642.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/ms178642.aspx</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Madhivanan</title>
		<link>http://blog.sqlauthority.com/2008/02/04/sql-server-simple-example-of-while-loop-with-break-and-continue/#comment-85747</link>
		<dc:creator><![CDATA[Madhivanan]]></dc:creator>
		<pubDate>Tue, 31 Aug 2010 07:44:24 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=495#comment-85747</guid>
		<description><![CDATA[NSERT Number (Num) 
select @start+number from master..spt_values
where type=&#039;p&#039; and number between 0 and @end]]></description>
		<content:encoded><![CDATA[<p>NSERT Number (Num)<br />
select @start+number from master..spt_values<br />
where type=&#8217;p&#8217; and number between 0 and @end</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: shrikant</title>
		<link>http://blog.sqlauthority.com/2008/02/04/sql-server-simple-example-of-while-loop-with-break-and-continue/#comment-85631</link>
		<dc:creator><![CDATA[shrikant]]></dc:creator>
		<pubDate>Mon, 30 Aug 2010 09:31:19 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=495#comment-85631</guid>
		<description><![CDATA[Hi Sir,

How to get fast execution of this below code

ALTER PROC [dbo].[usp_PopulateNumber4] @Start int = NULL, @End int = NULL AS
 
    DECLARE @Digit TABLE (digit int NOT NULL);    
 
    SET NOCOUNT ON;
    DECLARE @i int = @Start;
    BEGIN TRAN
    WHILE @i &lt;= @End
    BEGIN
        INSERT Number (Num) SELECT @I
        SET @i += 1;
    END
	COMMIT]]></description>
		<content:encoded><![CDATA[<p>Hi Sir,</p>
<p>How to get fast execution of this below code</p>
<p>ALTER PROC [dbo].[usp_PopulateNumber4] @Start int = NULL, @End int = NULL AS</p>
<p>    DECLARE @Digit TABLE (digit int NOT NULL);    </p>
<p>    SET NOCOUNT ON;<br />
    DECLARE @i int = @Start;<br />
    BEGIN TRAN<br />
    WHILE @i &lt;= @End<br />
    BEGIN<br />
        INSERT Number (Num) SELECT @I<br />
        SET @i += 1;<br />
    END<br />
	COMMIT</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Phyo</title>
		<link>http://blog.sqlauthority.com/2008/02/04/sql-server-simple-example-of-while-loop-with-break-and-continue/#comment-80492</link>
		<dc:creator><![CDATA[Phyo]]></dc:creator>
		<pubDate>Fri, 16 Jul 2010 09:21:18 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=495#comment-80492</guid>
		<description><![CDATA[Thanks so much Imran. It works like a charm!! So, GROUP BY is the function I was looking for. I was modifying an existing query file and when I first tried, MSSQL gave me an error saying that I need to put all the data fields in the select statement in GROUP BY clause. Now, I tried like you said and it works wonderfully. Thanks once again. Have a nice day.]]></description>
		<content:encoded><![CDATA[<p>Thanks so much Imran. It works like a charm!! So, GROUP BY is the function I was looking for. I was modifying an existing query file and when I first tried, MSSQL gave me an error saying that I need to put all the data fields in the select statement in GROUP BY clause. Now, I tried like you said and it works wonderfully. Thanks once again. Have a nice day.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Imran Mohammed</title>
		<link>http://blog.sqlauthority.com/2008/02/04/sql-server-simple-example-of-while-loop-with-break-and-continue/#comment-80442</link>
		<dc:creator><![CDATA[Imran Mohammed]]></dc:creator>
		<pubDate>Fri, 16 Jul 2010 00:45:21 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=495#comment-80442</guid>
		<description><![CDATA[@Phyo

Small Correction in SQL Statement, it should be Plus but not a comma, 

Corrected SQL :
-------------------------------------

Select AccountID
,SUM(ISNULL(InBytes,0)+ ISNULL(OutBytes,0) TotalUsage
From MyTable
Where Day Between @StartDate AND @EndDate
Group BY AccountID

~Peace.]]></description>
		<content:encoded><![CDATA[<p>@Phyo</p>
<p>Small Correction in SQL Statement, it should be Plus but not a comma, </p>
<p>Corrected SQL :<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-</p>
<p>Select AccountID<br />
,SUM(ISNULL(InBytes,0)+ ISNULL(OutBytes,0) TotalUsage<br />
From MyTable<br />
Where Day Between @StartDate AND @EndDate<br />
Group BY AccountID</p>
<p>~Peace.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
