SQL Server Stored Procedures Reference
This section provides comprehensive reference information for Transact-SQL stored procedures in SQL Server. Stored procedures are a set of Transact-SQL statements grouped together to perform a specific task.
Overview of Stored Procedures
Stored procedures offer several benefits including improved performance, modularity, and enhanced security. They can accept input parameters, return output parameters, and return result sets.
Key concepts include:
- Execution Plan Reuse: Stored procedures are compiled and execution plans are cached, leading to faster execution for subsequent calls.
- Reduced Network Traffic: Instead of sending multiple Transact-SQL statements over the network, only the procedure name and parameters are sent.
- Security: Permissions can be granted on stored procedures, allowing users to perform specific actions without needing direct access to underlying tables.
- Modularity and Reusability: Procedures can be called from other procedures, applications, or scripts, promoting code reuse.
Creating and Managing Stored Procedures
CREATE PROCEDURE
Use the CREATE PROCEDURE
statement to create a new stored procedure.
ALTER PROCEDURE
Use the ALTER PROCEDURE
statement to modify an existing stored procedure.
DROP PROCEDURE
Use the DROP PROCEDURE
statement to remove a stored procedure.
Executing Stored Procedures
Use the EXECUTE
or EXEC
command to run a stored procedure.
System Stored Procedures
SQL Server provides a rich set of system stored procedures (often prefixed with sp_
) for managing and administering SQL Server. These procedures are typically stored in the master
database.
Common System Stored Procedures
sp_help
: Provides information about database objects.sp_configure
: Allows you to change server configuration options.sp_monitor
: Reports on SQL Server usage statistics.sp_addrole
: Creates a new database role.
For a complete list and detailed descriptions, refer to the System Stored Procedures documentation.
Stored Procedure Parameters
Stored procedures can accept parameters to customize their behavior. Parameters can be input, output, or both.
Parameter Modes
- Input Parameters: Values passed into the procedure.
- Output Parameters: Values returned from the procedure.
- Input/Output Parameters: Values passed in and potentially modified and returned.
Parameter Syntax
Parameters are declared with a name (starting with @
) and a data type.
Parameter Name | Data Type | Mode | Description |
---|---|---|---|
@CustomerID |
INT |
Input | The ID of the customer to retrieve orders for. |
@OrderCount |
INT |
Output | The total number of orders found for the customer. |
Error Handling in Stored Procedures
Effective error handling is crucial for robust stored procedures. Use TRY...CATCH
blocks for structured error management.
TRY...CATCH
Blocks
Code that might raise an error is placed within the TRY
block. If an error occurs, control is transferred to the CATCH
block.
@@ERROR
and RAISERROR
While TRY...CATCH
is preferred, older methods involved checking the @@ERROR
global variable after each statement and using RAISERROR
to signal errors.