{"id":100,"date":"2026-03-06T08:21:05","date_gmt":"2026-03-06T08:21:05","guid":{"rendered":"https:\/\/gigz.pk\/sql\/?post_type=lesson&#038;p=100"},"modified":"2026-03-16T18:53:32","modified_gmt":"2026-03-16T18:53:32","slug":"stored-procedures","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/sql\/lesson\/stored-procedures\/","title":{"rendered":"Stored Procedures"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">A <strong>Stored Procedure<\/strong> is a set of SQL statements that is stored in the database and can be executed repeatedly. It helps in improving performance, maintaining consistency, and reducing repetitive coding.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Key Advantages<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Reusability:<\/strong> Write once, use multiple times.<\/li>\n\n\n\n<li><strong>Performance:<\/strong> Executed on the server, reducing network traffic.<\/li>\n\n\n\n<li><strong>Security:<\/strong> Can restrict direct access to underlying tables.<\/li>\n\n\n\n<li><strong>Maintainability:<\/strong> Changes are centralized in one place.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">2. Creating a Stored Procedure<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The basic syntax to create a stored procedure:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">CREATE PROCEDURE procedure_name<br>    @parameter1 datatype,<br>    @parameter2 datatype<br>AS<br>BEGIN<br>    -- SQL statements<br>END;<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Example:<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">CREATE PROCEDURE GetEmployeeByID<br>    @EmployeeID INT<br>AS<br>BEGIN<br>    SELECT * FROM Employees<br>    WHERE EmployeeID = @EmployeeID;<br>END;<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">3. Executing a Stored Procedure<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Stored procedures can be executed using the <code>EXEC<\/code> or <code>EXECUTE<\/code> command.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example:<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">EXEC GetEmployeeByID @EmployeeID = 101;<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">4. Updating a Stored Procedure<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To modify an existing stored procedure, use <code>ALTER PROCEDURE<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example:<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">ALTER PROCEDURE GetEmployeeByID<br>    @EmployeeID INT<br>AS<br>BEGIN<br>    SELECT EmployeeName, Department<br>    FROM Employees<br>    WHERE EmployeeID = @EmployeeID;<br>END;<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">5. Deleting a Stored Procedure<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">If a stored procedure is no longer needed, you can remove it:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">DROP PROCEDURE procedure_name;<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Example:<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">DROP PROCEDURE GetEmployeeByID;<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">6. Parameters in Stored Procedures<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Stored procedures can accept <strong>input parameters<\/strong> to filter data or perform operations dynamically.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Input Parameter:<\/strong> Provides data to the procedure.<\/li>\n\n\n\n<li><strong>Output Parameter:<\/strong> Returns data back to the caller.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Example of Output Parameter:<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">CREATE PROCEDURE GetEmployeeCount<br>    @DeptID INT,<br>    @EmpCount INT OUTPUT<br>AS<br>BEGIN<br>    SELECT @EmpCount = COUNT(*)<br>    FROM Employees<br>    WHERE DepartmentID = @DeptID;<br>END;<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">7. Best Practices<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Always use meaningful procedure names.<\/li>\n\n\n\n<li>Avoid hardcoding values; use parameters.<\/li>\n\n\n\n<li>Keep procedures modular and focused on a single task.<\/li>\n\n\n\n<li>Document procedure logic for future reference.<\/li>\n\n\n\n<li>Test procedures thoroughly before deploying to production.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/sql\/\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">Intermediate SQL for Data Professionals (SQL-201) > Views &#038; Stored Procedures > Updating Views<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1773578712873\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n","protected":false},"menu_order":47,"template":"","class_list":["post-100","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Stored Procedures - SQL Learning Hub<\/title>\n<meta name=\"description\" content=\"&quot;Learn SQL Stored Procedures: create, execute, update, and optimize reusable queries for better performance and maintainability.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/sql\/lesson\/stored-procedures\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Stored Procedures - SQL Learning Hub\" \/>\n<meta property=\"og:description\" content=\"&quot;Learn SQL Stored Procedures: create, execute, update, and optimize reusable queries for better performance and maintainability.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/sql\/lesson\/stored-procedures\/\" \/>\n<meta property=\"og:site_name\" content=\"SQL Learning Hub\" \/>\n<meta property=\"article:modified_time\" content=\"2026-03-16T18:53:32+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/sql\\\/lesson\\\/stored-procedures\\\/\",\"url\":\"https:\\\/\\\/gigz.pk\\\/sql\\\/lesson\\\/stored-procedures\\\/\",\"name\":\"Stored Procedures - SQL Learning Hub\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/sql\\\/#website\"},\"datePublished\":\"2026-03-06T08:21:05+00:00\",\"dateModified\":\"2026-03-16T18:53:32+00:00\",\"description\":\"\\\"Learn SQL Stored Procedures: create, execute, update, and optimize reusable queries for better performance and maintainability.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/sql\\\/lesson\\\/stored-procedures\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/sql\\\/lesson\\\/stored-procedures\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/sql\\\/lesson\\\/stored-procedures\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/sql\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Intermediate SQL for Data Professionals (SQL-201) > Views & Stored Procedures > Updating Views\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/sql\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/sql\\\/\",\"name\":\"SQL Learning Hub\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/sql\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Stored Procedures - SQL Learning Hub","description":"\"Learn SQL Stored Procedures: create, execute, update, and optimize reusable queries for better performance and maintainability.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/gigz.pk\/sql\/lesson\/stored-procedures\/","og_locale":"en_US","og_type":"article","og_title":"Stored Procedures - SQL Learning Hub","og_description":"\"Learn SQL Stored Procedures: create, execute, update, and optimize reusable queries for better performance and maintainability.","og_url":"https:\/\/gigz.pk\/sql\/lesson\/stored-procedures\/","og_site_name":"SQL Learning Hub","article_modified_time":"2026-03-16T18:53:32+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/sql\/lesson\/stored-procedures\/","url":"https:\/\/gigz.pk\/sql\/lesson\/stored-procedures\/","name":"Stored Procedures - SQL Learning Hub","isPartOf":{"@id":"https:\/\/gigz.pk\/sql\/#website"},"datePublished":"2026-03-06T08:21:05+00:00","dateModified":"2026-03-16T18:53:32+00:00","description":"\"Learn SQL Stored Procedures: create, execute, update, and optimize reusable queries for better performance and maintainability.","breadcrumb":{"@id":"https:\/\/gigz.pk\/sql\/lesson\/stored-procedures\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/sql\/lesson\/stored-procedures\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/sql\/lesson\/stored-procedures\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/sql\/"},{"@type":"ListItem","position":2,"name":"Intermediate SQL for Data Professionals (SQL-201) > Views & Stored Procedures > Updating Views"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/sql\/#website","url":"https:\/\/gigz.pk\/sql\/","name":"SQL Learning Hub","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/sql\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/sql\/wp-json\/wp\/v2\/lesson\/100","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/sql\/wp-json\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/sql\/wp-json\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/sql\/wp-json\/wp\/v2\/media?parent=100"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}