To enforce
business rules in SQL Server, you first need a clear definition of the rules and when they must be applied.
Once you know the rules, you can divide them into logical units of work and implement those units in stored procedures.
This approach helps ensure that the same validations and side effects occur consistently, regardless of which application
submits the request.
Consider these two rules:
-
When a new employee is hired, the employee’s contact information is inserted into the
Employees table,
and the department manager is notified.
-
When an employee is transferred, the employee’s new department number is recorded in the
CompanyOrg table,
and the department manager is notified.
Both rules involve data changes plus a required notification. Stored procedures are a natural fit because they can validate
inputs, perform the database updates, and ensure those steps execute as one business transaction.
The following series of images shows different ways to structure stored procedures to meet the business rules above.
Where your business rules are implemented depends on your application architecture. In practice, many systems combine database-level
enforcement (constraints, triggers, procedures) with application-layer enforcement (service logic). The key is to keep the rules
consistent across layers.
Classic client/server architectures are often described in tiers:
-
Two-tier: a client application connects directly to SQL Server. In this model, stored procedures run on the database
server and are a primary way to enforce rules close to the data.
-
Three-tier: a middle tier (application server / API) contains significant business logic. Stored procedures may still
enforce critical data integrity rules and transactional operations, while the middle tier orchestrates workflow and integrates external services.
-
N-tier: multiple middle tiers exist (for example, API + services + messaging). Stored procedures typically focus on
database-centric validation and transactional updates, while higher-level workflow and integration are handled outside the database.
Design guidance: keep business rules that protect data integrity and transactional correctness close to the database, and use procedures
to provide stable, permission-controlled operations that callers can execute consistently. In the next lesson, you will practice creating
stored procedures.