{"id":78926,"date":"2026-11-10T19:40:29","date_gmt":"2026-11-10T19:40:29","guid":{"rendered":"http:\/\/bangla.sitestree.com\/?p=78926"},"modified":"2026-11-10T19:55:15","modified_gmt":"2026-11-10T19:55:15","slug":"rewritten-sql-server-75827","status":"publish","type":"post","link":"http:\/\/bangla.sitestree.com\/?p=78926","title":{"rendered":"Learning T-SQL: Dynamic Queries, Stored Procedures, and Cursors"},"content":{"rendered":"<p>T-SQL extends SQL with variables, conditional logic, procedures, and server-side control flow. This hands-on tutorial connects these features to a simple product database.<\/p>\n<h2>Use a sample schema<\/h2>\n<p>Suppose dbo.Products has ProductID, ProductName, and UnitPrice. Begin with a static SELECT; it is simpler to read, tune, and secure than a dynamically assembled string.<\/p>\n<h2>Create a procedure<\/h2>\n<pre><code>CREATE OR ALTER PROCEDURE dbo.ProductsAbovePrice\n    @MinPrice decimal(12,2)\nAS\nBEGIN\n    SET NOCOUNT ON;\n    SELECT ProductID, ProductName, UnitPrice\n    FROM dbo.Products\n    WHERE UnitPrice &gt;= @MinPrice\n    ORDER BY UnitPrice DESC;\nEND;\nGO\nEXEC dbo.ProductsAbovePrice @MinPrice = 50.00;<\/code><\/pre>\n<h2>Dynamic table choice<\/h2>\n<p>If a lesson requires selecting from different approved tables, look up the requested table name in a controlled allowlist and construct the SQL identifier with QUOTENAME. Pass filter values through sp_executesql parameters.<\/p>\n<h2>When a cursor makes sense<\/h2>\n<p>A cursor iterates over a result set. Its typical stages are DECLARE, OPEN, FETCH, loop, CLOSE, and DEALLOCATE. For mass updates and aggregations, a single set-based statement is usually easier and faster.<\/p>\n<h2>Exercises<\/h2>\n<p>Write a procedure with two filter parameters. Compare a cursor that processes products individually against a set-based query. Inspect estimated and actual execution plans and document the differences.<\/p>\n<hr\/>\n<p><strong>About this article:<\/strong> This AI-generated educational rewrite was prepared from an existing Open Education article, with new explanations and illustrative examples where helpful. <a href=\"https:\/\/bangla.sitestree.com\/?p=75827\">Read the earlier article (post 75827)<\/a>. The new text may be freely copied, edited, and reused with or without attribution to this site. Third-party material, brand names, and linked resources retain their own rights. <strong>AI can make mistakes:<\/strong> verify syntax, version support, security practices, and outcomes against official documentation before use, especially in production.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>T-SQL extends SQL with variables, conditional logic, procedures, and server-side control flow. This hands-on tutorial connects these features to a simple product database.<\/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-78926","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":78975,"url":"http:\/\/bangla.sitestree.com\/?p=78975","url_meta":{"origin":78926,"position":0},"title":"SQL Server Scripts and Data Export: SSMS Tutorial with Verification Steps","author":"Sayed","date":"November 10, 2026","format":false,"excerpt":"SQL Server Management Studio can generate object-definition scripts and help export data. Schema scripting and data movement should be treated as separate operations.","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":78956,"url":"http:\/\/bangla.sitestree.com\/?p=78956","url_meta":{"origin":78926,"position":1},"title":"Generating SQL Server Scripts and Exporting Data: A Practical Workflow","author":"Sayed","date":"November 10, 2026","format":false,"excerpt":"SQL Server Management Studio can script database objects and help move selected data. Object scripting, data export, backup, and full database deployment are related but different operations.","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":5965,"url":"http:\/\/bangla.sitestree.com\/?p=5965","url_meta":{"origin":78926,"position":2},"title":": SQL Views","author":"Author-Check- Article-or-Video","date":"January 30, 2015","format":false,"excerpt":"Title: SQL Views \u0985\u09a8\u09c1\u09ac\u09be\u09a6\u0995: \u09ab\u09df\u09b8\u09be\u09b2 \u09b0\u0995\u09bf (\u098f\u09ae.\u098f) Total words in the article: 478 SQL \u098f view \u09ac\u09b2\u09a4\u09c7 virtual table \u098f\u09b0 view \u0995\u09c7 \u09ac\u09cb\u099d\u09be\u09df, \u0985\u09b0\u09cd\u09a5\u09be\u09ce real table \u0995\u09c7 virtually \u09a6\u09c7\u0996\u09be\u09a8\u09cb \u09b9\u09df\u0964 \u0995\u09bf\u09ad\u09be\u09ac\u09c7 view create, update \u09ac\u09be delete \u0995\u09b0\u09be \u09b9\u09df \u09a4\u09be \u098f\u0987 \u0985\u09a7\u09cd\u09af\u09be\u09df\u09c7 \u0986\u09b2\u09cb\u099a\u09a8\u09be \u0995\u09b0\u09be \u09b9\u09ac\u09c7\u0964 SQL CREATE VIEW Statement SQL statement\u2026","rel":"","context":"In &quot;\u098f\u09b8 \u0995\u09bf\u0989 \u098f\u09b2 - \u09e6\u09e6\u09e7 \u0964 SQL - 001&quot;","block_context":{"text":"\u098f\u09b8 \u0995\u09bf\u0989 \u098f\u09b2 - \u09e6\u09e6\u09e7 \u0964 SQL - 001","link":"http:\/\/bangla.sitestree.com\/?cat=155"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":78931,"url":"http:\/\/bangla.sitestree.com\/?p=78931","url_meta":{"origin":78926,"position":3},"title":"Managing Product Prices and Updates in SQL Server","author":"Sayed","date":"November 10, 2026","format":false,"excerpt":"Price changes require care because incorrect UPDATE statements can modify every row, break reporting, or overwrite valid historical prices. This tutorial outlines a controlled update workflow.","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":78935,"url":"http:\/\/bangla.sitestree.com\/?p=78935","url_meta":{"origin":78926,"position":4},"title":"PHP Functions for SQL Server: Historical MSSQL APIs and Supported Replacements","author":"Sayed","date":"November 10, 2026","format":false,"excerpt":"A historic PHP API family exposed functions beginning with mssql_, including connection management, result fetching, stored-procedure execution, and metadata inspection. That API is no longer suitable for modern PHP applications.","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":78939,"url":"http:\/\/bangla.sitestree.com\/?p=78939","url_meta":{"origin":78926,"position":5},"title":"How to Improve SQL Server Query Response Time: A Measured Workflow","author":"Sayed","date":"November 10, 2026","format":false,"excerpt":"Speeding up a SQL Server query is an evidence-based process. An index hint or server upgrade should not be the first response to an unexplained delay.","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\/78926","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=78926"}],"version-history":[{"count":1,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/78926\/revisions"}],"predecessor-version":[{"id":78988,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/78926\/revisions\/78988"}],"wp:attachment":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=78926"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=78926"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=78926"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}