| name | sql-sp-analysis |
| description | Analyzes SQL Server stored procedures to extract business logic, map data flows, and document dependencies. Use when reverse-engineering stored procedure business rules, analyzing SP call chains, or documenting database-layer business logic. |
Purpose
Provide specialized knowledge for analyzing SQL Server stored procedures and database objects to extract business logic and support migration decisions.
Stored Procedure Classification
Categorize each SP by its primary function:
| Category | Indicators | Migration Strategy |
|---|
| CRUD | Simple INSERT/UPDATE/DELETE/SELECT | Replace with EF Core |
| Business Logic | CASE, IF/ELSE, calculations, multi-step | Extract to application layer |
| Reporting | Complex JOINs, aggregations, CTEs | Keep as SP or move to views |
| ETL/Batch | Cursors, bulk operations, temp tables | Evaluate for Azure Data Factory |
| Security | Permission checks, row-level security | Move to application middleware |
| Orchestration | Calls multiple other SPs | Extract to application service layer |
Analysis Procedure
1. Inventory All Database Objects
SELECT name, create_date, modify_date, LEN(OBJECT_DEFINITION(object_id)) as char_count
FROM sys.procedures ORDER BY name;
SELECT name, type_desc FROM sys.objects WHERE type IN ('FN','IF','TF') ORDER BY name;
SELECT name FROM sys.views ORDER BY name;
SELECT name, parent_id, OBJECT_NAME(parent_id) as table_name FROM sys.triggers;
2. Map SP Dependencies
SELECT DISTINCT referenced_entity_name, referenced_minor_name
FROM sys.dm_sql_referenced_entities('dbo.sp_YourProc', 'OBJECT');
SELECT DISTINCT referencing_entity_name
FROM sys.dm_sql_referencing_entities('dbo.sp_YourProc', 'OBJECT');
3. Identify SP Call Chains
Look for patterns like:
EXEC dbo.sp_SubProcedure @param1, @param2
EXECUTE @result = dbo.sp_AnotherProc
4. Business Logic Extraction Patterns
Decision Logic:
IF @Status = 'Active' AND @Balance > 0
SET @Eligible = 1
Calculations:
SET @Premium = @BaseRate * @AgeFactor * (1 + @RiskAdjustment)
Validation Rules:
IF @DateOfBirth > GETDATE()
RAISERROR('Date of birth cannot be in the future', 16, 1)
State Transitions:
UPDATE Claims SET Status = 'Approved'
WHERE ClaimId = @ClaimId AND Status = 'Pending'
Depth Configuration
Top-Level Analysis
- Document SP name, parameters, and brief purpose
- Note if it calls other SPs (mark for deep analysis if needed)
- Extract obvious business rules from comments and naming
Deep Analysis
- Trace complete SP call chains
- Follow dynamic SQL execution (
EXEC(@sql), sp_executesql)
- Analyze cursor logic and loop patterns
- Map temp table usage and data flows
- Document transaction boundaries (
BEGIN TRAN / COMMIT / ROLLBACK)
- Identify error handling patterns (
TRY/CATCH, @@ERROR)
Migration Strategy Recommendations
Move to EF Core (Recommended for CRUD)
- Simple SELECT/INSERT/UPDATE/DELETE operations
- SPs that map cleanly to entity operations
- SPs with no complex business logic
Extract to Application Layer (Recommended for Business Logic)
- SPs with complex IF/ELSE/CASE logic
- SPs that implement business rules or calculations
- SPs that enforce workflow/state transitions
- SPs that perform validation
Keep as Stored Procedures (Recommended for Performance-Critical)
- SPs with complex reporting queries
- SPs that process large datasets
- SPs that use advanced SQL features (window functions, recursive CTEs)
- SPs where the performance cost of moving logic to the app layer is unacceptable
Output Format
For each SP, document:
- Name and purpose (inferred from code and naming)
- Parameters (name, type, direction, business meaning)
- Business rules (extracted from logic)
- Tables touched (read/write with operation type)
- Dependencies (other SPs, functions, views called)
- Complexity (Simple/Medium/Complex)
- Migration recommendation (EF Core / App Layer / Keep)
- Confidence (High/Medium/Low)