Create a reusable library of SQL validation rules that returns offending rows in a standard format, and use the SSIS Data Profiling Task to discover which rules and thresholds each dataset requires.
1. Standardize the output of every quality rule
Have each check return at least:
-
RuleName
-
TableName
-
RecordKey
-
ColumnName
-
BadValue
-
IssueDescription
-
DetectedAt
This makes rules easy to combine with UNION ALL, persist in an exceptions table, report, and use as an SSIS quality gate.
SELECT
'CUSTOMER_EMAIL_REQUIRED' AS RuleName,
'dbo.Customer' AS TableName,
CAST(CustomerID AS varchar(50)) AS RecordKey,
'Email' AS ColumnName,
Email AS BadValue,
'Email is null or blank' AS IssueDescription,
SYSUTCDATETIME() AS DetectedAt
FROM dbo.Customer
WHERE Email IS NULL OR LTRIM(RTRIM(Email)) = '';
Implement stable rules as views or stored procedures—for example, dq.Customer_EmailRequired—rather than repeatedly writing ad hoc queries.
2. Use focused SQL patterns
Duplicates
Group by the columns that define business uniqueness, not necessarily the physical primary key.
SELECT Email, COUNT(*) AS DuplicateCount
FROM dbo.Customer
WHERE Email IS NOT NULL
GROUP BY Email
HAVING COUNT(*) > 1;
To retrieve every affected row, use a window function:
WITH d AS
(
SELECT *,
COUNT(*) OVER (PARTITION BY Email) AS DuplicateCount
FROM dbo.Customer
WHERE Email IS NOT NULL
)
SELECT *
FROM d
WHERE DuplicateCount > 1;
Invalid values and ranges
Use explicit domain rules. TRY_CONVERT is useful when source values arrive as text.
SELECT CustomerID, Status, BirthDate
FROM dbo.Customer
WHERE Status NOT IN ('Active', 'Inactive', 'Pending')
OR BirthDate > CAST(GETDATE() AS date)
OR BirthDate < '19000101';
For text length, formats, value distributions, numeric ranges, and date ranges, SSIS provides corresponding Length Distribution, Pattern, Value Distribution, and Statistics profiles ^1^.
Missing parent rows
Use NOT EXISTS to identify orphaned foreign-key values:
SELECT o.OrderID, o.CustomerID
FROM dbo.[Order] AS o
WHERE o.CustomerID IS NOT NULL
AND NOT EXISTS
(
SELECT 1
FROM dbo.Customer AS c
WHERE c.CustomerID = o.CustomerID
);
This corresponds to the SSIS Value Inclusion Profile, which measures overlap between columns and can reveal child values absent from the parent table ^1^.
Cross-column inconsistencies
Encode relationships that should always hold:
SELECT OrderID, OrderDate, ShipDate, Status
FROM dbo.[Order]
WHERE ShipDate < OrderDate
OR (Status = 'Shipped' AND ShipDate IS NULL)
OR (Status = 'Pending' AND ShipDate IS NOT NULL);
For rules such as “each postal code must always map to the same state,” group by the determinant and count distinct dependent values:
SELECT PostalCode, COUNT(DISTINCT StateCode) AS StateCount
FROM dbo.Address
GROUP BY PostalCode
HAVING COUNT(DISTINCT StateCode) > 1;
SSIS calls this a Functional Dependency Profile. Candidate Key, Functional Dependency, and Value Inclusion profiles cover uniqueness, cross-column consistency, and cross-table relationships respectively ^1^.
3. Profile unfamiliar SQL Server datasets in SSIS
In SSDT/Visual Studio:
- Add a Data Profiling Task to the package Control Flow.
- Open the task and, on General, select an XML output file or package variable.
- For initial discovery, select Quick Profile for a table or view.
- On Profile Requests, configure targeted profiles:
- Column Null Ratio — missing values
- Candidate Key — duplicates
- Length/Pattern/Value Distribution — malformed or unexpected values
- Column Statistics — abnormal numeric or date ranges
- Functional Dependency — inconsistent column relationships
- Value Inclusion — unmatched foreign-key-like values
- Run the package and open the output with Data Profile Viewer.
The task supports SQL Server data only—not file-based or third-party sources—and its execution account requires read/write permissions, including CREATE TABLE permission in tempdb ^2^.
The viewer shows summaries, details, and violations. For Candidate Key, Functional Dependency, and Value Inclusion profiles, the Details pane exposes relationship violations and can drill down to matching source rows when the source remains available ^3^.
4. Operationalize the checks
- Store thresholds and rule ownership in a configuration table.
- Write violations to a history table with batch ID and detection time.
- Distinguish warning rules from reject rules.
- Run checks against staging tables before loading production targets.
- Fail or redirect the SSIS load when reject-rule counts exceed thresholds.
- Add database
PRIMARY KEY, UNIQUE, FOREIGN KEY, CHECK, and NOT NULL constraints after existing data is clean; profiling detects issues, while constraints prevent recurrence.
- Protect profiling output because it can contain sensitive source data ^2^.
References
- Data Profiling Task
- Setup of the Data Profiling Task
- Data Profile Viewer