<?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>Using Cursor in SQL Server - MSSQL Query</title>
	<atom:link href="https://mssqlquery.com/tag/using-cursor-in-sql-server/feed" rel="self" type="application/rss+xml" />
	<link>https://mssqlquery.com</link>
	<description>MSSQL and TSQL Programming and TSQL Examples</description>
	<lastBuildDate>Fri, 10 Mar 2023 11:45:57 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.3.1</generator>

<image>
	<url>https://mssqlquery.com/wp-content/uploads/2023/06/cropped-mssql-query-icon-32x32.png</url>
	<title>Using Cursor in SQL Server - MSSQL Query</title>
	<link>https://mssqlquery.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Navigating Table Without Using Cursor in SQL Server</title>
		<link>https://mssqlquery.com/navigating-table-without-using-cursor-in-sql-server</link>
		
		<dc:creator><![CDATA[Yavuz Selim Kart]]></dc:creator>
		<pubDate>Sat, 13 Nov 2021 14:45:18 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Cursor and Table]]></category>
		<category><![CDATA[Using Cursor in SQL Server]]></category>
		<category><![CDATA[Without Using Cursor in SQL Server]]></category>
		<guid isPermaLink="false">https://mssqlquery.com/?p=343</guid>

					<description><![CDATA[<p>Hello to everyone, In this article, I will give information about navigating the table without using Cursor in SQL Server. In most cases, most people prefer to use Cursor to navigate the table in SQL Server, but it is a very costly operation. Of course, Cursor is used where Cursor&#46;&#46;&#46;</p>
<p>The post <a href="https://mssqlquery.com/navigating-table-without-using-cursor-in-sql-server">Navigating Table Without Using Cursor in SQL Server</a> first appeared on <a href="https://mssqlquery.com">MSSQL Query</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>Hello to everyone,</p>
<p>In this article, I will give information about navigating the table without using Cursor in SQL Server.</p>
<p>In most cases, most people prefer to use Cursor to navigate the table in SQL Server, but it is a very costly operation.</p>
<p>Of course, Cursor is used where Cursor should be used. Using Cursor in every operation puts extra load on SQL Server.</p>
<p>In the following example, you will learn how to navigate the table with and without Cursor.</p>
<p>I made the examples using the Northwind database.</p>
<p>Our first example is about Cursor.</p>
<pre class="line-numbers"><code class="language-sql">DECLARE @ProductID INT;
DECLARE @ProductName NVARCHAR(50);

DECLARE Cur_Example CURSOR FAST_FORWARD FOR
SELECT ProductID
FROM Products;

OPEN Cur_Example;
FETCH NEXT FROM Cur_Example
INTO @ProductID;

WHILE @@FETCH_STATUS = 0
BEGIN
    SELECT @ProductName = ProductName
    FROM Products
    WHERE ProductID = @ProductID;

    PRINT 'Product Name : ' + @ProductName;

    FETCH NEXT FROM Cur_Example
    INTO @ProductID;
END;

CLOSE Cur_Example;
DEALLOCATE Cur_Example;
GO</code></pre>
<p>When you run the above query, you will see the following result.</p>
<p><img decoding="async" fetchpriority="high" class="alignnone wp-image-345 size-full" src="https://mssqlquery.com/wp-content/uploads/2021/11/navigating-table-without-using-cursor-in-sql-server-1.jpg" alt="Navigating Table Without Using Cursor in SQL Server" width="700" height="509" srcset="https://mssqlquery.com/wp-content/uploads/2021/11/navigating-table-without-using-cursor-in-sql-server-1.jpg 700w, https://mssqlquery.com/wp-content/uploads/2021/11/navigating-table-without-using-cursor-in-sql-server-1-300x218.jpg 300w" sizes="(max-width: 700px) 100vw, 700px" /></p>
<p>Our second example is about the While loop.</p>
<pre class="line-numbers"><code class="language-sql">DECLARE @RecordCount INT;
DECLARE @ProductID INT = 1;
DECLARE @ProductName NVARCHAR(50);

SELECT @RecordCount = COUNT(*)
FROM dbo.Products;

WHILE @ProductID &lt;= @RecordCount
BEGIN
    SELECT @ProductName = ProductName
    FROM dbo.Products
    WHERE ProductID = @ProductID;
    PRINT 'Product Name : ' + @ProductName;
    SET @ProductID += 1;
END;
GO</code></pre>
<p>When you run the above query, you will see the following result.</p>
<p><img decoding="async" class="alignnone wp-image-346 size-full" src="https://mssqlquery.com/wp-content/uploads/2021/11/navigating-table-without-using-cursor-in-sql-server-2.jpg" alt="Navigating Table Without Using Cursor in SQL Server" width="700" height="527" srcset="https://mssqlquery.com/wp-content/uploads/2021/11/navigating-table-without-using-cursor-in-sql-server-2.jpg 700w, https://mssqlquery.com/wp-content/uploads/2021/11/navigating-table-without-using-cursor-in-sql-server-2-300x226.jpg 300w" sizes="(max-width: 700px) 100vw, 700px" /></p>
<p>As you can see, you can move around the table without using the Cursor. Its query is shorter than Cursor. Also, while loop is faster to complete the operation than Cursor.</p>
<p>Good luck to everyone in business and life.</p>
<div class='epvc-post-count'><span class='epvc-eye'></span>  <span class="epvc-count"> 694</span><span class='epvc-label'> Views</span></div><p>The post <a href="https://mssqlquery.com/navigating-table-without-using-cursor-in-sql-server">Navigating Table Without Using Cursor in SQL Server</a> first appeared on <a href="https://mssqlquery.com">MSSQL Query</a>.</p>]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
