{"id":78947,"date":"2026-11-10T19:45:17","date_gmt":"2026-11-10T19:45:17","guid":{"rendered":"http:\/\/bangla.sitestree.com\/?p=78947"},"modified":"2026-11-10T19:56:06","modified_gmt":"2026-11-10T19:56:06","slug":"rewritten-sql-server-69249","status":"publish","type":"post","link":"http:\/\/bangla.sitestree.com\/?p=78947","title":{"rendered":"SQL Server Cursors: How They Work and a Complete T-SQL Example"},"content":{"rendered":"<p>A cursor provides row-by-row navigation through a query result. It is helpful when a task genuinely needs sequential procedural work, but should not replace set-based SQL without a reason.<\/p>\n<h2>Cursor lifecycle<\/h2>\n<p>A typical T-SQL cursor is declared, opened, fetched, used in a loop, closed, and deallocated. Options such as LOCAL, FAST_FORWARD, and READ_ONLY affect cursor scope and behaviour.<\/p>\n<h2>Working example<\/h2>\n<pre><code>DECLARE @CustomerID int;\nDECLARE customer_cursor CURSOR LOCAL FAST_FORWARD FOR\n  SELECT CustomerID FROM dbo.Customers WHERE IsActive = 1;\nOPEN customer_cursor;\nFETCH NEXT FROM customer_cursor INTO @CustomerID;\nWHILE @@FETCH_STATUS = 0\nBEGIN\n    PRINT CONCAT('Customer: ', @CustomerID);\n    FETCH NEXT FROM customer_cursor INTO @CustomerID;\nEND;\nCLOSE customer_cursor;\nDEALLOCATE customer_cursor;<\/code><\/pre>\n<h2>Compare a set-based approach<\/h2>\n<p>If the objective is to update every active customer&#8217;s status, a single UPDATE with a WHERE clause normally expresses the intention more simply and can avoid cursor overhead.<\/p>\n<h2>Error handling<\/h2>\n<p>For production procedures, ensure cleanup also occurs when work fails. Limit long transactions and test behaviour with large result sets.<\/p>\n<hr\/>\n<p><strong>AI disclosure and reuse:<\/strong> This AI-generated article is rewritten from <a href=\"https:\/\/bangla.sitestree.com\/?p=69249\">an existing Open Education post (ID 69249)<\/a>. Additional explanations, code examples, and version notes may have been added. You may reuse the newly generated article text with or without attribution. Third-party trademarks and linked works retain their own rights. <strong>AI can make mistakes.<\/strong> Check examples, permissions, supported SQL Server versions, and documentation before use in any live system.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A cursor provides row-by-row navigation through a query result. It is helpful when a task genuinely needs sequential procedural work, but should not replace set-based SQL without a reason.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_hide_featured_image":false,"_jetpack_featured_image_duplicate_dismissed":0,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[20,182],"tags":[466],"class_list":["post-78947","post","type-post","status-publish","format-standard","hentry","category-information-technology","category---blog","tag-sql-server","item-wrap"],"jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":69249,"url":"http:\/\/bangla.sitestree.com\/?p=69249","url_meta":{"origin":78947,"position":0},"title":"What are cursors? #38","author":"Author-Check- Article-or-Video","date":"August 15, 2021","format":false,"excerpt":"Cursors are server side database objects that are used by applications to apply operations on the database table data on a row-by-row basis. The operations may vary from one row to another row dynamically based on the requirements (business logic) and also multiple operations can be performed on the same\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":78933,"url":"http:\/\/bangla.sitestree.com\/?p=78933","url_meta":{"origin":78947,"position":1},"title":"Using C# with SQL Server: Relationships, Procedures, and Safe Parameters","author":"Sayed","date":"November 10, 2026","format":false,"excerpt":"C# applications typically access SQL Server through ADO.NET. A reliable design starts with proper relational constraints, calls stored procedures using typed parameters, and manages connections predictably.","rel":"","context":"In &quot;Information Technology&quot;","block_context":{"text":"Information Technology","link":"http:\/\/bangla.sitestree.com\/?cat=20"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":78948,"url":"http:\/\/bangla.sitestree.com\/?p=78948","url_meta":{"origin":78947,"position":2},"title":"Improving SQL Server Cursor Performance: Practical Options and Set-Based Alternatives","author":"Sayed","date":"November 10, 2026","format":false,"excerpt":"Cursors can become costly when they fetch many records and run additional queries inside a loop. Optimization therefore begins by asking whether row-by-row work is necessary at all.","rel":"","context":"In &quot;Information Technology&quot;","block_context":{"text":"Information Technology","link":"http:\/\/bangla.sitestree.com\/?cat=20"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":78941,"url":"http:\/\/bangla.sitestree.com\/?p=78941","url_meta":{"origin":78947,"position":3},"title":"SQL Server Indexes Explained: Clustered, Nonclustered, and Composite Keys","author":"Sayed","date":"November 10, 2026","format":false,"excerpt":"An index helps the SQL Server query optimizer find rows efficiently, but every index carries maintenance costs. Good index design begins with real workloads rather than indexing every column.","rel":"","context":"In &quot;Information Technology&quot;","block_context":{"text":"Information Technology","link":"http:\/\/bangla.sitestree.com\/?cat=20"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":78924,"url":"http:\/\/bangla.sitestree.com\/?p=78924","url_meta":{"origin":78947,"position":4},"title":"SQL Server Dynamic SQL, Procedures, Cursors, and Injection: Concepts and Examples","author":"Sayed","date":"November 10, 2026","format":false,"excerpt":"Stored procedures, dynamic SQL, and cursors solve different problems. Good T-SQL design starts with ordinary set-based queries and introduces these features only when they are warranted.","rel":"","context":"In &quot;Information Technology&quot;","block_context":{"text":"Information Technology","link":"http:\/\/bangla.sitestree.com\/?cat=20"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":78936,"url":"http:\/\/bangla.sitestree.com\/?p=78936","url_meta":{"origin":78947,"position":5},"title":"C# and SQL Server: From Foreign Keys to Reusable Database Commands","author":"Sayed","date":"November 10, 2026","format":false,"excerpt":"Database-backed .NET applications depend on both a correct schema and correctly parameterized application queries. This tutorial links foreign keys, stored procedures, and prepared SQL commands.","rel":"","context":"In &quot;Information Technology&quot;","block_context":{"text":"Information Technology","link":"http:\/\/bangla.sitestree.com\/?cat=20"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_featured_media_url":"","_links":{"self":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/78947","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=78947"}],"version-history":[{"count":1,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/78947\/revisions"}],"predecessor-version":[{"id":79009,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/78947\/revisions\/79009"}],"wp:attachment":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=78947"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=78947"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=78947"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}