Summary
In this module, you learned advanced T-SQL techniques that help you write more expressive, efficient, and maintainable database code. These capabilities address common database development scenarios involving complex analytics, hierarchical data, JSON processing, and error handling.
You learned how to:
- Write Common Table Expressions (CTEs) for organizing complex queries and using recursive patterns to traverse hierarchical data structures
- Apply window functions for ranking, running totals, moving averages, and analytical calculations that preserve row-level detail
- Use JSON functions including
JSON_OBJECT,JSON_ARRAY,JSON_ARRAYAGG,OPENJSON, andJSON_VALUEto parse, construct, and transform JSON data - Implement regular expressions with
REGEXP_LIKE,REGEXP_REPLACE,REGEXP_SUBSTR, and related functions for pattern matching and text manipulation - Find approximate matches using fuzzy string functions like
EDIT_DISTANCE,EDIT_DISTANCE_SIMILARITY, andJARO_WINKLER_DISTANCE - Create graph tables and write queries using the
MATCHoperator andSHORTEST_PATHfor relationship traversal - Write correlated subqueries for row-by-row comparisons, existence checks, and per-row calculations
- Implement structured error handling with
TRY...CATCH, error functions,THROW, and proper transaction management
Key takeaways
- Recursive CTEs are the standard approach for traversing hierarchical data like organizational charts or bill-of-materials structures
- Window functions with
OVERclauses enable analytical calculations without collapsing rows. Use them instead of self-joins for running totals and rankings JSON_OBJECTandJSON_ARRAYAGGconstruct JSON from relational data, whileOPENJSONparses JSON into relational rowsJARO_WINKLER_DISTANCEis optimized for name matching;EDIT_DISTANCE_SIMILARITYworks better for longer strings- Always check
@@TRANCOUNTbeforeROLLBACKinCATCHblocks to handle cases where no transaction is active - Combine
SET XACT_ABORT ONwithTRY...CATCHfor full transaction protection