<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Control of Flow &#8211; SQLpowered.com</title>
	<atom:link href="https://sqlpowered.com/command-category/control-of-flow/feed/" rel="self" type="application/rss+xml" />
	<link>https://sqlpowered.com</link>
	<description>SQL Server + BI</description>
	<lastBuildDate>Wed, 23 Jul 2025 07:49:05 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://sqlpowered.com/wp-content/uploads/2020/07/FavIcon-e1594067873682-99x100.png</url>
	<title>Control of Flow &#8211; SQLpowered.com</title>
	<link>https://sqlpowered.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>WHILE</title>
		<link>https://sqlpowered.com/t-sql-commands/while/</link>
		
		<dc:creator><![CDATA[Jan Dvořák]]></dc:creator>
		<pubDate>Sat, 15 Jun 2024 08:11:23 +0000</pubDate>
				<category><![CDATA[T-SQLpowered]]></category>
		<guid isPermaLink="false">https://sqlpowered.com/?post_type=t-sql-commands&#038;p=5699</guid>

					<description><![CDATA[Iterate with MIN/MAX Id. Doesn&#8217;t perform well for broken number sequences. DECLARE @Current_Id INT DECLARE @Max_Id INT DECLARE @Ids TABLE ( [Id] INT) INSERT INTO @Ids ([Id]) SELECT [Id] FROM (VALUES (1),(2),(3)) [v] ([Id]) SELECT @Current_Id = MIN([Id]) FROM @Ids SELECT @Max_Id = MAX([Id]) FROM @Ids WHILE @Current_Id &#60;= @Max_Id...]]></description>
										<content:encoded><![CDATA[<p>Iterate with MIN/MAX Id. Doesn&#8217;t perform well for broken number sequences.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="sql">DECLARE @Current_Id INT
DECLARE @Max_Id INT
DECLARE @Ids TABLE ( [Id] INT)

INSERT INTO @Ids ([Id])
	SELECT [Id]
	FROM (VALUES (1),(2),(3)) [v] ([Id])

SELECT @Current_Id = MIN([Id]) FROM @Ids
SELECT @Max_Id = MAX([Id]) FROM @Ids

WHILE @Current_Id &lt;= @Max_Id
BEGIN

    SELECT 
		@Current_Id = [Id]
    FROM @Ids
    WHERE [Id] = @Current_Id

    PRINT @Current_Id

    SET @Current_Id = @Current_Id + 1

END
GO</pre>
<p>Iterate as long as there is a row to be processed. May have performance impact for large set of ids.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="sql">DECLARE @Id INT
DECLARE @Ids TABLE ( [Id] INT)

INSERT INTO @Ids ([Id])
	SELECT 
        [Id]
	FROM (VALUES (1),(2),(3)) [v] ([Id])

WHILE EXISTS (SELECT * FROM @Ids)
BEGIN
    
    SELECT TOP(1)
		@Id = [Id]
    FROM @Ids
    ORDER BY Id

    RAISERROR('%i', 10, 1, @Id) WITH NOWAIT

    DELETE FROM @Ids WHERE Id = @Id

END
GO</pre>
<p>&nbsp;</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
