<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Lucas Alves</title>
    <description>The latest articles on Forem by Lucas Alves (@lulucasalves).</description>
    <link>https://forem.com/lulucasalves</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3500234%2F69182d36-c93b-416a-87bf-c8d6f600d7c0.jpg</url>
      <title>Forem: Lucas Alves</title>
      <link>https://forem.com/lulucasalves</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/lulucasalves"/>
    <language>en</language>
    <item>
      <title>Stored Procedures: Organization and Code Quality in SQL</title>
      <dc:creator>Lucas Alves</dc:creator>
      <pubDate>Sat, 13 Sep 2025 20:18:14 +0000</pubDate>
      <link>https://forem.com/lulucasalves/stored-procedures-organization-and-code-quality-in-sql-39e3</link>
      <guid>https://forem.com/lulucasalves/stored-procedures-organization-and-code-quality-in-sql-39e3</guid>
      <description>&lt;p&gt;Among the advanced features of SQL, stored procedures are one of my favorite tools when it comes to keeping systems clean, organized, and efficient in relational databases.&lt;/p&gt;

&lt;p&gt;The examples in this article will all be in MySQL, but keep in mind that stored procedures are also supported in &lt;strong&gt;MariaDB&lt;/strong&gt;, &lt;strong&gt;PostgreSQL&lt;/strong&gt;, &lt;strong&gt;SQL Server&lt;/strong&gt;, and &lt;strong&gt;Oracle&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Advantages
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Security
&lt;/h3&gt;

&lt;p&gt;You can grant &lt;strong&gt;execution-only permissions&lt;/strong&gt; on a procedure to certain users, without giving them direct access to insert or modify data in the tables. On top of that, procedures enforce &lt;strong&gt;fixed rules&lt;/strong&gt;, minimizing the risk of human error.&lt;/p&gt;

&lt;h3&gt;
  
  
  Performance
&lt;/h3&gt;

&lt;p&gt;Using stored procedures improves performance since, instead of executing multiple SQL statements from application code (opening and closing connections repeatedly), you can &lt;strong&gt;store the logic directly in the database&lt;/strong&gt;. This makes the system faster because all operations run in sequence inside SQL itself.&lt;/p&gt;

&lt;h3&gt;
  
  
  Encapsulation
&lt;/h3&gt;

&lt;p&gt;Stored procedures allow you to &lt;strong&gt;encapsulate business rules&lt;/strong&gt; and hide implementation details. This makes your system cleaner and easier for developers to read and maintain.&lt;/p&gt;

&lt;h3&gt;
  
  
  Reusability
&lt;/h3&gt;

&lt;p&gt;In a &lt;strong&gt;microservices architecture&lt;/strong&gt;, one common challenge is duplicating the same business logic across multiple services. Whenever that logic changes, you have to refactor it everywhere. With stored procedures, you just update the SQL once and call it from all services, simplifying maintenance.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to create a procedure
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DELIMITER $$

CREATE PROCEDURE verify_avaiable_suppliers(
    IN p_avaiable_budget DECIMAL(10, 2),
    IN p_product_id INT
)
BEGIN
    IF NOT EXISTS (SELECT 1 FROM Products WHERE id = p_product_id) THEN
        SIGNAL SQLSTATE '45000' 
            SET MESSAGE_TEXT = 'Product does not exist';
    ELSE
        SELECT *
        FROM Suppliers s
        WHERE s.product_id = p_product_id
          AND s.price &amp;lt;= p_avaiable_budget
        ORDER BY s.price;
    END IF;
END $$

DELIMITER ;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Inside a procedure, you can declare variables and build more advanced logic with WHILE loops, IF conditions, and variable declarations, enabling you to implement sophisticated workflows directly in SQL.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to use it
&lt;/h2&gt;

&lt;p&gt;To execute a procedure, just call it as if it were a regular function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CALL verify_avaiable_suppliers();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How to modify a procedure
&lt;/h2&gt;

&lt;p&gt;Unfortunately, MySQL does not allow direct edits to stored procedures. You need to drop and recreate them.&lt;br&gt;
The execution time is usually short, but it can cause temporary instability in the database.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DROP PROCEDURE IF EXISTS verify_avaiable_suppliers;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How to debug a procedure
&lt;/h2&gt;

&lt;p&gt;Personally, I debug complex procedures by adding &lt;strong&gt;SELECT statements&lt;/strong&gt; as if they were “breakpoints.” This helps identify exactly where the logic isn’t working as expected.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DELIMITER $$

CREATE PROCEDURE verify_avaiable_suppliers(
    IN p_avaiable_budget DECIMAL(10, 2),
    IN p_product_id INT
)
BEGIN
    SELECT p_avaiable_budget;  -- Debug breakpoint
END $$

DELIMITER ;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Things to watch out for
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Complexity
&lt;/h3&gt;

&lt;p&gt;Very large procedures (800+ lines) can be confusing and difficult for new team members to maintain.&lt;/p&gt;

&lt;h3&gt;
  
  
  Permissions
&lt;/h3&gt;

&lt;p&gt;Must be carefully managed, especially for critical procedures that should only be executed by authorized roles.&lt;/p&gt;

&lt;h3&gt;
  
  
  Team knowledge
&lt;/h3&gt;

&lt;p&gt;If most developers on your team have only basic SQL knowledge, heavy reliance on procedures can create risks.&lt;/p&gt;

&lt;h3&gt;
  
  
  System flexibility
&lt;/h3&gt;

&lt;p&gt;Some business rules might become “locked” inside a procedure, making it harder to test or adapt.&lt;/p&gt;

&lt;h3&gt;
  
  
  Procedures calling other procedures
&lt;/h3&gt;

&lt;p&gt;This can overcomplicate logic and make the code harder to follow and maintain.&lt;/p&gt;

</description>
      <category>sql</category>
      <category>database</category>
      <category>mysql</category>
      <category>postgres</category>
    </item>
  </channel>
</rss>
