Showing posts with label Stored Procedures. Show all posts
Showing posts with label Stored Procedures. Show all posts

Stored Procedures and User-Defined Functions in SQL Server

Microsoft SQL Server Tutorial Series: Beginner to Expert

Part 11: Stored Procedures and User-Defined Functions in SQL Server

Welcome to Part 11 of our SQL Server series! In this post, you’ll learn how to structure your database logic with Stored Procedures and User-Defined Functions (UDFs).


๐Ÿง  What is a Stored Procedure?

A Stored Procedure is a set of SQL statements that can be saved and reused. It allows you to encapsulate logic, improve performance, and enforce security.

✅ Benefits:

  • Reusability and modularity
  • Improved performance due to pre-compilation
  • Security via controlled execution

๐Ÿ“˜ Basic Syntax Example:

CREATE PROCEDURE GetAllStudents
AS
BEGIN
    SELECT * FROM Students;
END;
GO

๐Ÿ› ️ Execute the Procedure:

EXEC GetAllStudents;

๐Ÿงฉ With Parameters:

CREATE PROCEDURE GetStudentByID
    @StudentID INT
AS
BEGIN
    SELECT * FROM Students WHERE StudentID = @StudentID;
END;
GO

-- Execute it
EXEC GetStudentByID @StudentID = 1;

๐Ÿงฎ What is a User-Defined Function (UDF)?

A User-Defined Function allows you to create reusable SQL logic that returns a single value or a table.

๐Ÿ”น Scalar Function Example:

CREATE FUNCTION GetStudentAge (@BirthDate DATE)
RETURNS INT
AS
BEGIN
    RETURN DATEDIFF(YEAR, @BirthDate, GETDATE());
END;
GO

-- Use the function
SELECT dbo.GetStudentAge('2005-03-15') AS Age;

๐Ÿ”น Table-Valued Function Example:

CREATE FUNCTION GetActiveStudents()
RETURNS TABLE
AS
RETURN (
    SELECT * FROM Students WHERE IsActive = 1
);
GO

-- Use the function
SELECT * FROM dbo.GetActiveStudents();

๐Ÿ“Š Comparison Table: Stored Procedure vs Function

Feature Stored Procedure User-Defined Function
Return Type 0 or more result sets Single value or a table
Parameters Input and Output Input only
Transaction Control Yes No
Use in SELECT No Yes
Typical Use Business logic & data changes Reusable calculations, filters

๐Ÿซ Real-World Example: School System

Use procedures to insert students and functions to calculate GPA or get full names.

CREATE PROCEDURE AddStudent
    @StudentID INT,
    @FirstName VARCHAR(50),
    @LastName VARCHAR(50),
    @BirthDate DATE
AS
BEGIN
    INSERT INTO Students (StudentID, FirstName, LastName, BirthDate, IsActive)
    VALUES (@StudentID, @FirstName, @LastName, @BirthDate, 1);
END;
GO

CREATE FUNCTION FullName(@FirstName VARCHAR(50), @LastName VARCHAR(50))
RETURNS VARCHAR(101)
AS
BEGIN
    RETURN @FirstName + ' ' + @LastName;
END;
GO

๐Ÿงพ SQL Server Cheat Sheet

-- Stored Procedure
CREATE PROCEDURE [Name] AS BEGIN ... END;

-- Scalar Function
CREATE FUNCTION [Name]() RETURNS DataType AS BEGIN RETURN ... END;

-- Table-Valued Function
CREATE FUNCTION [Name]() RETURNS TABLE AS RETURN (SELECT ...);

๐Ÿ’ก Best Practices

Tip Recommendation
Naming Conventions Use usp_ prefix for procedures and fn_ for functions
Security Grant EXECUTE rights selectively
Performance Avoid functions in WHERE clause if possible
Readability Use comments and consistent formatting

✅ Summary

  • Stored Procedures = reusable blocks of logic
  • Functions = return data (value or table) for use in queries
  • Both help you write cleaner, more efficient SQL

๐Ÿ”— What's Next?

Next up in Part 12, we’ll cover: Views and Triggers in SQL Server.

๐Ÿ” Previous Parts:

Share your own stored procedures or UDF tips in the comments below!


Stored Procedures & Triggers in SQLite with Python – Fun Beginner Guide

Stored Procedures and Triggers with SQL: A Simple and Fun Tutorial for Everyone

Welcome back to our exciting SQL adventure! In our previous tutorials, we learned how to manage data with commands like SELECT, JOIN, and transactions in our toy store database. Now, let’s dive into something super cool: stored procedures and triggers. These are like magic spells that make your database smarter by automating tasks and responding to changes. This tutorial is designed to be easy to understand, useful for beginners and experienced users, and covers key aspects of stored procedures and triggers. We’ll use SQLite and Python with our toystore.db database, keeping it fun and simple like casting spells in a magical toy shop!


What are Stored Procedures and Triggers?

Imagine you run a toy store and have a favorite recipe for organizing toys—like always checking stock and updating prices in one go. Instead of writing the same instructions every time, you could save that recipe as a magic spell to use whenever you want. That’s what a stored procedure does—it’s a saved set of SQL commands you can run with one call.

Now, imagine a magic alarm that automatically updates your inventory list whenever a toy is sold. That’s a trigger—it automatically runs specific commands when something happens in your database, like adding or deleting data.

In our toy store, we’ll:

  • Create a stored procedure to process a toy sale.
  • Create a trigger to automatically log sales in a history table.
  • Use SQLite and Python to make it all happen.

Why Learn Stored Procedures and Triggers?

Stored procedures and triggers are awesome because:

  • They’re Time-Savers: They let you reuse and automate tasks, like magic shortcuts.
  • They’re Simple: Once you set them up, they’re easy to use.
  • They’re Powerful: They make your database smarter and more organized.
  • They’re Useful: They’re used in apps, websites, and games to handle repetitive tasks.
  • They’re Fun: It’s like programming your database to do tricks for you!

Let’s explore these magical tools in our toy store!


Getting Started

We’ll use Python with SQLite to run our SQL commands, just like in our previous tutorials. Make sure you have Python installed (download it from python.org if needed). SQLite comes with Python, so no extra setup is required. You can also use DB Browser for SQLite to see your data visually, but we’ll focus on Python code for clarity.

Important Note: SQLite has limited support for stored procedures compared to databases like MySQL or PostgreSQL. It doesn’t natively support stored procedures, but we can simulate them using Python functions that run SQL commands. Triggers, however, are fully supported in SQLite. We’ll show both concepts clearly, using SQLite for triggers and Python to mimic stored procedures.

We’ll work with our toystore.db database, using the Toys table, a Sales table, and a new SaleHistory table to log sales automatically. Here’s the setup code:

import sqlite3

# Connect to the database
conn = sqlite3.connect('toystore.db')
cursor = conn.cursor()

# Create Toys table
cursor.execute('''
    CREATE TABLE IF NOT EXISTS Toys (
        ToyID INTEGER PRIMARY KEY,
        Name TEXT,
        Type TEXT,
        Price REAL,
        Stock INTEGER
    )
''')

# Create Sales table
cursor.execute('''
    CREATE TABLE IF NOT EXISTS Sales (
        SaleID INTEGER PRIMARY KEY,
        ToyID INTEGER,
        Quantity INTEGER,
        TotalPrice REAL,
        FOREIGN KEY (ToyID) REFERENCES Toys(ToyID)
    )
''')

# Create SaleHistory table for triggers
cursor.execute('''
    CREATE TABLE IF NOT EXISTS SaleHistory (
        HistoryID INTEGER PRIMARY KEY,
        SaleID INTEGER,
        ToyName TEXT,
        SaleTime TEXT
    )
''')

# Clear existing data to avoid duplicates
cursor.execute("DELETE FROM Toys")
cursor.execute("DELETE FROM Sales")
cursor.execute("DELETE FROM SaleHistory")

# Add toys
cursor.execute("INSERT INTO Toys (Name, Type, Price, Stock) VALUES ('Robot', 'Action Figure', 30.00, 10)")
cursor.execute("INSERT INTO Toys (Name, Type, Price, Stock) VALUES ('Jigsaw', 'Puzzle', 10.00, 15)")
cursor.execute("INSERT INTO Toys (Name, Type, Price, Stock) VALUES ('Teddy', 'Stuffed Animal', 15.00, 8)")

conn.commit()
conn.close()
print("Toy store database ready for stored procedures and triggers!")

What’s Happening?

  • Toys table: Stores toy details with Stock to track inventory.
  • Sales table: Records sales with ToyID, Quantity, and TotalPrice.
  • SaleHistory table: Logs sales with a timestamp for tracking (used by triggers).
  • We added three toys to start.

Our Toys table looks like this:

ToyIDNameTypePriceStock
1RobotAction Figure30.0010
2JigsawPuzzle10.0015
3TeddyStuffed Animal15.008

What is a Stored Procedure?

A stored procedure is a saved set of SQL commands you can run with one call, like a recipe you store for later. In SQLite, we can’t create stored procedures directly, but we can mimic them with Python functions that run SQL commands.

For example, let’s create a “stored procedure” (Python function) to process a toy sale, which:

  1. Checks if there’s enough stock.
  2. Updates the stock in the Toys table.
  3. Adds a sale record to the Sales table.

Example: Simulating a Stored Procedure

import sqlite3

def process_sale(toy_id, quantity):
    conn = sqlite3.connect('toystore.db')
    cursor = conn.cursor()
    
    try:
        # Start transaction
        cursor.execute("SELECT Stock, Price, Name FROM Toys WHERE ToyID = ?", (toy_id,))
        result = cursor.fetchone()
        if not result:
            raise Exception("Toy not found!")
        stock, price, toy_name = result
        
        if stock < quantity:
            raise Exception(f"Not enough {toy_name} in stock!")
        
        # Update stock
        cursor.execute("UPDATE Toys SET Stock = Stock - ? WHERE ToyID = ?", (quantity, toy_id))
        
        # Add sale
        total_price = quantity * price
        cursor.execute("INSERT INTO Sales (ToyID, Quantity, TotalPrice) VALUES (?, ?, ?)",
                      (toy_id, quantity, total_price))
        
        conn.commit()
        print(f"Sale of {quantity} {toy_name}(s) completed!")
        
        # Show results
        cursor.execute("SELECT * FROM Toys WHERE ToyID = ?", (toy_id,))
        print("Updated Toy:", cursor.fetchone())
        cursor.execute("SELECT * FROM Sales WHERE ToyID = ?", (toy_id,))
        print("Sales:", cursor.fetchall())
    
    except Exception as e:
        conn.rollback()
        print(f"Error: {e}. Sale cancelled!")
    
    conn.close()

# Test the procedure
process_sale(1, 2)  # Sell 2 Robots

Output:

Sale of 2 Robot(s) completed!
Updated Toy: (1, 'Robot', 'Action Figure', 30.0, 8)
Sales: [(1, 1, 2, 60.0)]

What is a Trigger?

A trigger is an automatic action that runs when something happens in a table, like adding, updating, or deleting data. For example, we can create a trigger to log every sale in the SaleHistory table with a timestamp.

Example: Creating a Trigger

import sqlite3
from datetime import datetime

conn = sqlite3.connect('toystore.db')
cursor = conn.cursor()

# Create trigger
cursor.execute('''
    CREATE TRIGGER IF NOT EXISTS log_sale
    AFTER INSERT ON Sales
    FOR EACH ROW
    BEGIN
        INSERT INTO SaleHistory (SaleID, ToyName, SaleTime)
        SELECT NEW.SaleID, Toys.Name, DATETIME('now')
        FROM Toys 
        WHERE Toys.ToyID = NEW.ToyID;
    END;
''')

conn.commit()

# Test the trigger by adding a sale
try:
    cursor.execute("UPDATE Toys SET Stock = Stock - 3 WHERE ToyID = 3")
    cursor.execute("INSERT INTO Sales (ToyID, Quantity, TotalPrice) VALUES (3, 3, 45.00)")
    conn.commit()
    print("Sale of 3 Teddies added!")
    
    # Check SaleHistory
    cursor.execute("SELECT * FROM SaleHistory")
    print("Sale History:", cursor.fetchall())

except:
    conn.rollback()
    print("Sale failed!")

conn.close()

Output (timestamp will vary):

Sale of 3 Teddies added!
Sale History: [(1, 3, 'Teddy', '2025-09-03 00:15:23')]

Combining Stored Procedures and Triggers

process_sale(2, 2)  # Sell 2 Jigsaws

# Check SaleHistory
conn = sqlite3.connect('toystore.db')
cursor = conn.cursor()
cursor.execute("SELECT * FROM SaleHistory")
print("Sale History:", cursor.fetchall())
conn.close()

Output:

Sale of 2 Jigsaw(s) completed!
Updated Toy: (2, 'Jigsaw', 'Puzzle', 10.0, 13)
Sales: [(2, 2, 2, 20.0)]
Sale History: [(1, 3, 'Teddy', '2025-09-03 00:15:23'), (2, 2, 'Jigsaw', '2025-09-03 00:15:25')]

Tips for Success

  1. Start Simple: Try one trigger or procedure at a time.
  2. Test Triggers: Insert data to see if your trigger works.
  3. Use Transactions: Combine with transactions for safety.
  4. Check SQLite Limits: SQLite doesn’t support stored procedures natively, so use Python functions.
  5. Practice: Try triggers for logging updates or procedures for complex tasks like discounts.

Recap: Why Learn Stored Procedures and Triggers?

These tools are like magic for your database:

  • They Save Time: Reuse code and automate tasks.
  • They’re Safe: Triggers ensure actions happen consistently.
  • They’re Useful: Used in apps, stores, and games.
  • They’re Fun: It’s like programming your database to be smart!

Common Questions

1. Does SQLite support stored procedures?

Not natively, but Python functions can mimic them.

2. Do triggers work in other databases?

Yes, MySQL, PostgreSQL, and others support triggers and stored procedures with similar syntax.

3. Can triggers cause errors?

Yes, if not written carefully, so test them with small data first.

4. Can I have multiple triggers?

Yes, but each must have a unique name.


๐Ÿงฉ Challenge for Readers

Ready to test your SQL wizardry? Try this challenge:

๐ŸŽฏ Challenge:
Create your own trigger that updates a LowStockAlerts table whenever a toy's stock drops below 5.

Steps:

  1. Create a new table called LowStockAlerts with columns for ToyID, Name, and AlertTime.
  2. Write a trigger that runs AFTER an update to the Toys table’s Stock column.
  3. If Stock < 5, insert a new row into LowStockAlerts with the toy name and the current time.

๐Ÿ’ก Bonus: Combine this with your existing Python “stored procedure” so stock updates from sales automatically trigger the alert!


Wrapping Up

Stored procedures and triggers are like magic spells that make your database smarter and safer. In this tutorial, we used a Python function to simulate a stored procedure for toy sales and created a trigger to log sales automatically in our toystore.db database. Whether you’re a 6th grader or a pro coder, these tools are fun and powerful for automating database tasks.

Try creating your own database for a game or library and experiment with triggers or procedures. Use DB Browser for SQLite to see your data or keep coding in Python. With stored procedures and triggers, you’re now a database wizard, ready to automate your data like a superhero!

Happy SQL adventures, and keep casting those database spells!


๐Ÿš€ What’s Next?

Loved this tutorial? Want to keep leveling up your SQL and Python skills?

  • ๐Ÿ”„ Try building a small inventory or game database from scratch using what you’ve learned.
  • ๐Ÿ’ฌ Share your version of the “Low Stock Alert” challenge in the comments below!
  • ๐Ÿ“ง Subscribe or follow for more fun, beginner-friendly tutorials using real code and creative examples.
  • ๐Ÿง™‍♂️ Keep casting those SQL spells and automating your data like a true database wizard!


Featured Post

Stored Procedures and User-Defined Functions in SQL Server

Microsoft SQL Server Tutorial Series: Beginner to Expert Part 11: Stored Procedures and User-Defined Functions in SQL Server Welcome to ...

Popular Posts