New to Claude Skills? Learn how to install them →

jeremylongshore on GitHub

Stored Procedure Generator

Free

Easily create and deploy stored procedures for major databases.

Get this skill

Free · Opens the source repo

What Stored Procedure Generator does

The Stored Procedure Generator is a practical tool designed for developers and database administrators who need to create, validate, and deploy stored procedures across PostgreSQL, MySQL, and SQL Server. It streamlines the process of writing database functions, triggers, and procedures while ensuring that they adhere to best practices for error handling and transaction management. This skill is particularly useful for teams that require a consistent approach to database operations, allowing for efficient development and deployment of complex SQL procedures.

Users begin by identifying the target database type and its requirements, followed by generating the necessary stored procedures. The generator provides sample code for each database type, demonstrating how to create functions and procedures with proper syntax. Additionally, it includes robust error handling mechanisms, ensuring that users can manage exceptions gracefully. This is crucial for maintaining data integrity and providing clear feedback during database operations.

The skill also features syntax validation and deployment scripts, which facilitate the testing and execution of stored procedures. Users can validate their SQL syntax before deployment, reducing the likelihood of runtime errors. This makes it an ideal choice for developers working in environments where database reliability is paramount. By automating these processes, the Stored Procedure Generator helps teams save time and minimize errors, allowing them to focus on more critical tasks.

When to use it

Use this skill when you need to generate, validate, or deploy stored procedures for PostgreSQL, MySQL, or SQL Server efficiently.

When not to use it

This skill may not be suitable for very simple database queries or when working with databases not supported by this tool.

What you can build with it

Generate CRUD Procedures

Create a set of CRUD stored procedures for a specified database table, streamlining the process of data management.

Create Audit Triggers

Automatically generate triggers that log changes to specific tables, ensuring data integrity and accountability.

Validate and Deploy Procedures

Use the skill to validate the syntax of your stored procedures before deploying them to your database.

How to install Stored Procedure Generator

View source

1. Install with the skills CLI

npx skills add jeremylongshore/claude-code-plugins-plus-skills/generating-stored-procedures --agent claude-code

2. Or install it manually

Download the skill folder and drop it into ~/.claude/skills/ for all projects, or .claude/skills/ to scope it to one repo. Restart Claude Code so it picks up the new skill.

Anthropic's agentic coding CLI, and the reference implementation of Agent Skills. Drop a skill folder into ~/.claude/skills and Claude Code loads it automatically whenever a task matches the skill's description. Claude Code docs

Inside SKILL.md

Written by jeremylongshore

Stored Procedure Generator

Generate production-ready stored procedures for PostgreSQL, MySQL, and SQL Server with proper error handling, transaction management, and security best practices.

Prerequisites

  • Database connection credentials (host, port, database, user, password)
  • Appropriate permissions: CREATE PROCEDURE, CREATE FUNCTION, EXECUTE
  • Target database type identified (PostgreSQL, MySQL, or SQL Server)

Instructions

Step 1: Identify Database Type and Requirements

Determine the target database and procedure requirements:

-- PostgreSQL: Check version and extensions
SELECT version();
\dx

-- MySQL: Check version and settings
SELECT VERSION();
SHOW VARIABLES LIKE 'sql_mode';

-- SQL Server: Check version and edition
SELECT @@VERSION;

Step 2: Generate Stored Procedure

PostgreSQL Function (PL/pgSQL):

CREATE OR REPLACE FUNCTION get_user_by_id(p_user_id INTEGER)
RETURNS TABLE(id INTEGER, username VARCHAR, email VARCHAR, created_at TIMESTAMP)
LANGUAGE plpgsql
AS $$
BEGIN
    RETURN QUERY
    SELECT u.id, u.username, u.email, u.created_at
    FROM users u
    WHERE u.id = p_user_id;

    IF NOT FOUND THEN
        RAISE EXCEPTION 'User with ID % not found', p_user_id
            USING ERRCODE = 'P0002';
    END IF;
END;
$$;

MySQL Stored Procedure:

DELIMITER //
CREATE PROCEDURE GetUserById(IN p_user_id INT)
BEGIN
    DECLARE user_exists INT DEFAULT 0;

    SELECT COUNT(*) INTO user_exists FROM users WHERE id = p_user_id;

    IF user_exists = 0 THEN
        SIGNAL SQLSTATE '45000'  # 45000 = configured value
            SET MESSAGE_TEXT = 'User not found';
    END IF;

    SELECT id, username, email, created_at
    FROM users
    WHERE id = p_user_id;
END //
DELIMITER ;

SQL Server Stored Procedure (T-SQL):

CREATE PROCEDURE dbo.GetUserById
    @UserId INT
AS
BEGIN
    SET NOCOUNT ON;

    IF NOT EXISTS (SELECT 1 FROM dbo.Users WHERE Id = @UserId)
    BEGIN
        RAISERROR('User with ID %d not found', 16, 1, @UserId);
        RETURN;
    END

    SELECT Id, Username, Email, CreatedAt
    FROM dbo.Users
    WHERE Id = @UserId;
END;
GO

Step 3: Add Transaction Management

PostgreSQL with Transaction:

CREATE OR REPLACE FUNCTION transfer_funds(
    p_from_account INTEGER,
    p_to_account INTEGER,
    p_amount NUMERIC(15,2)
)
RETURNS BOOLEAN
LANGUAGE plpgsql
AS $$
BEGIN
    -- Debit source account
    UPDATE accounts SET balance = balance - p_amount
    WHERE id = p_from_account AND balance >= p_amount;

    IF NOT FOUND THEN
        RAISE EXCEPTION 'Insufficient funds or invalid source account';
    END IF;

    -- Credit destination account
    UPDATE accounts SET balance = balance + p_amount
    WHERE id = p_to_account;

    IF NOT FOUND THEN
        RAISE EXCEPTION 'Invalid destination account';
    END IF;

    RETURN TRUE;
EXCEPTION
    WHEN OTHERS THEN
        RAISE;
END;
$$;

MySQL with Transaction:

DELIMITER //
CREATE PROCEDURE TransferFunds(
    IN p_from_account INT,
    IN p_to_account INT,
    IN p_amount DECIMAL(15,2)
)
BEGIN
    DECLARE EXIT HANDLER FOR SQLEXCEPTION
    BEGIN
        ROLLBACK;
        RESIGNAL;
    END;

    START TRANSACTION;

    UPDATE accounts SET balance = balance - p_amount
    WHERE id = p_from_account AND balance >= p_amount;

    IF ROW_COUNT() = 0 THEN
        SIGNAL SQLSTATE '45000'  # 45000 = configured value
            SET MESSAGE_TEXT = 'Insufficient funds';
    END IF;

    UPDATE accounts SET balance = balance + p_amount
    WHERE id = p_to_account;

    COMMIT;
END //
DELIMITER ;

Step 4: Validate Syntax

Use the validation script to check procedure syntax:

# Validate PostgreSQL procedure
python3 ${CLAUDE_SKILL_DIR}/scripts/stored_procedure_syntax_validator.py \
    --db-type postgresql \
    --file procedure.sql

# Validate MySQL procedure
python3 ${CLAUDE_SKILL_DIR}/scripts/stored_procedure_syntax_validator.py \
    --db-type mysql \
    --file procedure.sql

Step 5: Deploy to Database

# Deploy to PostgreSQL
python3 ${CLAUDE_SKILL_DIR}/scripts/stored_procedure_deployer.py \
    --db-type postgresql \
    --host localhost \
    --database mydb \
    --file procedure.sql

# Deploy to MySQL
python3 ${CLAUDE_SKILL_DIR}/scripts/stored_procedure_deployer.py \
    --db-type mysql \
    --host localhost \
    --database mydb \
    --file procedure.sql

Output

  • SQL procedure file with proper syntax for target database
  • Validation report confirming syntax correctness
  • Deployment confirmation with execution results
  • Rollback script for procedure removal

Error Handling

ErrorCauseSolution
permission deniedMissing CREATE PROCEDURE privilegeGRANT CREATE PROCEDURE ON database TO user;
syntax errorInvalid SQL for database typeUse database-specific syntax validator
function already existsProcedure exists without OR REPLACEAdd OR REPLACE or DROP first
undefined columnReferenced column doesn't existVerify table schema before deployment
transaction abortedError during transactionCheck EXCEPTION handler and ROLLBACK logic

Examples

Generate CRUD procedures for a table:

User: Generate CRUD stored procedures for the 'products' table in PostgreSQL

Claude: I'll create four procedures for the products table:
1. create_product - Insert new product
2. get_product - Retrieve by ID
3. update_product - Update existing product
4. delete_product - Soft delete product

Create audit trigger:

User: Create a trigger to log all changes to the orders table

Claude: I'll create an audit trigger that:
1. Creates an orders_audit table if not exists
2. Captures INSERT, UPDATE, DELETE operations
3. Records old/new values, user, and timestamp

Resources

  • ${CLAUDE_SKILL_DIR}/references/postgresql_stored_procedure_best_practices.md
  • ${CLAUDE_SKILL_DIR}/references/mysql_stored_procedure_best_practices.md
  • ${CLAUDE_SKILL_DIR}/references/sqlserver_stored_procedure_best_practices.md
  • ${CLAUDE_SKILL_DIR}/references/database_security_guidelines.md
  • ${CLAUDE_SKILL_DIR}/references/stored_procedure_optimization_techniques.md

Overview

Use when you need to generate, validate, or deploy stored procedures for PostgreSQL, MySQL, or SQL Server.

Frequently asked questions about Stored Procedure Generator

Similar skills