liveBook · Manning

Stored procedure

As explained in 100 SQL Server Mistakes and How to Avoid Them

A stored procedure is a named, executable routine stored in a database and capable of accepting parameters and performing database operations.

A stored procedure is a named database routine that accepts parameters and performs database work. Treat it as an operational boundary: a failure inside a multi-step write can leave an open transaction and confuse both callers and support staff. section 7.1

Avoid the “it failed, so it must have rolled back” mistake:

  • Check @@TRANCOUNT after a failed execution; an unhandled error can leave the count at 1, requiring ROLLBACK. section 7.1
  • Handle expected failures with TRY..CATCH, and make duplicate-order failures identifiable with a custom error message. section 7.1
  • Log useful error details so application support can investigate production failures. section 7.1

A sales-order failure

dbo.InsertSalesOrder builds a customer prefix and inserts sales-order header and detail records inside a transaction. Renaming SalesOrderDetails makes the insert fail; without error handling, the transaction remains open rather than rolling back automatically. section 7.1

Listing 7.1 Creating the dbo.InsertSalesOrder stored procedure
CREATE PROCEDURE dbo.InsertSalesOrder
   @SalesOrderNumber NVARCHAR(12),
   @SalesOrderDate DATE,
   @SalesPersonID INT,
   @SalesAreaID INT,
   @CustomerID INT,
   @SalesOrderDeliveryDueDate DATE,
   @SalesOrderDeliveryActualDate DATE,
   @CurrierUsedForDelivery NVARCHAR(32),
   @ProductID INT,
   @Quantity INT
AS
BEGIN
   BEGIN TRANSACTION
 
       DECLARE @CustomerPrefix NVARCHAR(12) ;
       SET @CustomerPrefix = (
           SELECT SUBSTRING(CustomerCompanyName,1,3)
           FROM dbo.Customers
           WHERE CustomerID = @CustomerID
       ) ;
       
       INSERT INTO dbo.SalesOrderHeaders
       VALUES (
             @CustomerPrefix + @SalesOrderNumber
           , @SalesOrderDate
           , @SalesPersonID
           , @SalesAreaID
           , @CustomerID
           , @SalesOrderDeliveryDueDate
           , @SalesOrderDeliveryActualDate
           , @CurrierUsedForDelivery
       ) ;
 
       INSERT INTO dbo.SalesOrderDetails (
             ProductID
           , Quantity
           , SalesOrderNumber
       )
       VALUES (
             @ProductID
           , @Quantity
           , @CustomerPrefix + @SalesOrderNumber
       ) ;
 
   COMMIT
END

Inspect the parameterized InsertSalesOrder routine and its transaction-wrapped header and detail inserts. section 7.1

How 100 SQL Server Mistakes and How to Avoid Them covers it

How deep it goes
Works through a parameterized routine, transaction failures, TRY..CATCH, custom errors, and logging rather than procedure design broadly.
Examples
Uses dbo.InsertSalesOrder, a renamed SalesOrderDetails table, invalid product IDs, duplicate orders, and an ErrorLog table.
What you can do after
Diagnose/debug failed procedures by checking transaction counts, interpreting constraint errors, and adding handling or logging.
How it connects
Connects stored procedures to transactions, TRY..CATCH, foreign keys, primary keys, RAISERROR, and error logging.

Also explained in

Compare how 4 books explain stored procedure →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of 100 SQL Server Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.