SQL Stored Procedures: From Hard‑Coded Logic to Reusable SQL
Source: Dev.to
What Finally Clicked: Stored Procedures Are Just Reusable Logic
The mistake I made early on was treating stored procedures as something exotic.
They’re not.
A stored procedure is just a named SQL logic that lives in the database and can accept inputs. That’s it.
If you already understand:
- functions
- parameters
- if / else logic
then stored procedures are not a new concept, just a new name in a new environment.
Why Plain SQL Started Feeling Limited
Imagine this very normal requirement:
“Give me all orders above a certain amount.”
In plain SQL, I might write:
select *
from orders
where total_amount > 1000;
That works… but it’s hard‑coded.
If the rule changes to 1500, I have to edit the query.
If another report needs 500, I copy the query.
That repetition is where stored procedures start to make sense.
My First Useful Stored Procedure
create or replace procedure high_value_orders
as
begin
select *
from orders
where total_amount > 1000;
end;
Now I can just run:
exec high_value_orders;
The logic works, but the rule is still hard‑coded.
The Real Upgrade: Parameters
Instead of fixing the value inside the procedure, I pass it in:
create or replace procedure high_value_orders (
p_min_amount number
)
as
begin
select *
from orders
where total_amount > p_min_amount;
end;
Now I can reuse the same logic with different thresholds:
exec high_value_orders(500);
exec high_value_orders(1500);
This turns the procedure into configurable behaviour.
Where CASE Entered the Picture
CASE confused me at first because I kept seeing it explained abstractly.
Using it to encode business rules helped.
Example requirement: “Classify customers based on how much they’ve spent.”
select
customer_id,
total_spent,
case
when total_spent >= 5000 then 'VIP'
when total_spent >= 2000 then 'Regular'
else 'Occasional'
end as customer_type
from customers;
CASE is SQL’s version of if / else—conditions evaluated top to bottom.
Combining CASE With Stored Procedures
Let’s say the business wants:
“Show me customers, but label them differently depending on the threshold I care about.”
create or replace procedure customer_segments (
p_vip_threshold number
)
as
begin
select
customer_id,
total_spent,
case
when total_spent >= p_vip_threshold then 'VIP'
else 'Non-VIP'
end as segment
from customers;
end;
Now the logic adapts:
exec customer_segments(5000);
exec customer_segments(3000);
Same procedure, different behaviour.
What I Was Doing Wrong Before
- Treated stored procedures like advanced SQL syntax
- Avoided parameters and hard‑coded everything
- Saw CASE as a trick, not as logic
Reframing them as:
- reusable logic
- configurable inputs
- conditional rules
made everything click.
Why This Matters Beyond Syntax
SQL is not just querying data; it also:
- enforces rules
- centralizes logic
- reduces duplication
- makes behaviour explicit
Stored procedures aren’t always the answer, but understanding them changes how you think about databases.
Closing
If stored procedures or CASE ever felt heavy or unnecessary, I get it.
They only make sense once you stop thinking:
“How do I write this query?”
and start thinking:
“How do I make this logic reusable?”
That shift took me a while, but once it happened, SQL stopped feeling shallow.