Stored Procedures Basics & CRUD Syntax
1. Description
A Stored Procedure (SP) is a precompiled set of one or more SQL statements that are stored in a database. It can be executed by calling its name, much like a function in a programming language. Stored procedures can accept input parameters and return output parameters, result sets, or a status value.
They are commonly used to encapsulate the logic for CRUD (Create, Read, Update, Delete) operations.
2. Why It Is Important
Using stored procedures offers several key benefits:
- Performance: SPs are compiled once and stored in the database, leading to faster execution as the query plan is reused.
- Security: They help prevent SQL injection attacks because the parameters are treated as data, not as executable code. You can also grant permissions to execute a stored procedure without granting permissions to the underlying tables.
- Centralized Logic: Business logic can be encapsulated within the stored procedure. If the logic changes, you only need to update the SP, not all the applications that use it.
- Reduced Network Traffic: Instead of sending complex queries over the network, an application only needs to send the name of the SP and its parameters, which can reduce network load.
- Maintainability: They help in organizing the database code and making it more modular.
3. Real-World Examples
- A
usp_CreateOrderstored procedure that takes order details as input, creates a new order, and updates the inventory in a single transaction. - A
usp_GetCustomerDetailsSP that takes aCustomerIdand returns the customer's information along with their recent orders. - A nightly batch process that calls a stored procedure like
usp_ArchiveOldDatato move old records to an archive table.
4. Syntax & Explanation (T-SQL for SQL Server)
Create (Insert)
This stored procedure inserts a new product and returns the ID of the new record using an OUTPUT parameter.
CREATE PROCEDURE usp_Product_Create
-- Input parameters
@Name NVARCHAR(200),
@Price DECIMAL(18, 2),
-- Output parameter
@NewId INT OUTPUT
AS
BEGIN
-- The SET NOCOUNT ON statement prevents the sending of DONE_IN_PROC messages for each statement in a stored procedure.
SET NOCOUNT ON;
INSERT INTO Products (Name, Price, CreatedDate)
VALUES (@Name, @Price, GETDATE());
-- SCOPE_IDENTITY() returns the last identity value inserted into an identity column in the same scope.
SET @NewId = SCOPE_IDENTITY();
END
Read (Select)
This SP retrieves a single product by its ID.
CREATE PROCEDURE usp_Product_GetById
@Id INT
AS
BEGIN
SET NOCOUNT ON;
SELECT Id, Name, Price, CreatedDate
FROM Products
WHERE Id = @Id;
END
Update
This SP updates an existing product's name and price.
CREATE PROCEDURE usp_Product_Update
@Id INT,
@Name NVARCHAR(200),
@Price DECIMAL(18, 2)
AS
BEGIN
SET NOCOUNT ON;
UPDATE Products
SET
Name = @Name,
Price = @Price,
UpdatedDate = GETDATE()
WHERE
Id = @Id;
END
Delete
This SP deletes a product by its ID.
CREATE PROCEDURE usp_Product_Delete
@Id INT
AS
BEGIN
SET NOCOUNT ON;
DELETE FROM Products
WHERE Id = @Id;
END
Calling a Stored Procedure from C# (using ADO.NET)
using System.Data;
using System.Data.SqlClient;
// Assume 'connectionString' is defined elsewhere
int newProductId;
using (var connection = new SqlConnection(connectionString))
{
using (var command = new SqlCommand("usp_Product_Create", connection))
{
// Specify that the command is a stored procedure
command.CommandType = CommandType.StoredProcedure;
// Add input parameters
command.Parameters.AddWithValue("@Name", "New Gadget");
command.Parameters.AddWithValue("@Price", 29.99m);
// Add the output parameter
var outParam = new SqlParameter("@NewId", SqlDbType.Int)
{
Direction = ParameterDirection.Output
};
command.Parameters.Add(outParam);
// Open the connection and execute the command
connection.Open();
command.ExecuteNonQuery();
// Get the value of the output parameter
newProductId = (int)outParam.Value;
}
}
Console.WriteLine($"New product created with ID: {newProductId}");
5. Mini Practice Task
- Create a simple
Customerstable with columns forId,FirstName,LastName, andEmail. - Write a stored procedure named
usp_Customer_Createthat takesFirstName,LastName, andEmailas input and inserts a new customer. - Write a stored procedure named
usp_Customer_GetByEmailthat takes anEmailas input and returns the matching customer's record. - Test both of your stored procedures using SQL Server Management Studio (SSMS) or a similar tool.
- (Bonus) Write C# code to call your
usp_Customer_Createstored procedure.