T-SQL

JSON Data Type Is Not Comparable!

I was really not happy when I hit this with the new JSON data type. In SQL Server, set operators like UNION, INTERSECT, and EXCEPT have been a very reliable part of many generic scripts. We already survived older non-comparable types like text and image, and things became much cleaner…

Read more
T-SQL

SET vs SELECT Variable Assignment When No Rows Match

This one can be easy to miss, but it can change your logic in a silent way. SET and SELECT look similar when assigning a variable, but they do not behave the same in all cases. The difference appears when the subquery returns no rows. With SELECT @var = …the…
T-SQL

SQL Server Allows Empty Stored Procedures

I found one small SQL Server behavior that surprised me more than I expected. You can create a stored procedure with an empty body, and SQL Server will accept it. No SELECT, no INSERT, no PRINT, nothing inside. Even more surprisingly, the procedure can be executed without any error. I…
T-SQL

Managing String Content in View Definitions

The hidden text (this_is_hidden_text_in_the_view) in the view definition is an intriguing addition. Anything after the SELECT * FROM sys.tables query that is not a valid SQL statement (like a comment or plain text) will not be executed or parsed by SQL Server. It essentially becomes invisible to the engine and…
T-SQL

Numbered Stored Procedures

Numbered stored procedures are a lesser-known feature that allows multiple procedures to share the same name but are distinguished by a numeric suffix. This feature can be useful in certain scenarios, although it is not commonly used in modern database development. Numbered stored procedures allow you to create a series…
T-SQL

IDENTITY Cheat Sheet

Today we will look at a brief overview of all important functions, global variables, and DBCC commands for working with IDENTITY values in SQL Server. Programmers often make mistakes regarding when to use each of them, particularly due to not understanding how the individual functions behave within the context in…

Read more
T-SQL

Generating Empty GUIDs

In SQL Server, a Globally Unique Identifier (GUID) is a 16-byte binary data type that is commonly used to uniquely identify records in a database. The UNIQUEIDENTIFIER data type in SQL Server is equivalent to a GUID, and there are occasions where you might need to generate an empty GUID,…
T-SQL

Exploring the Power of the JSON_OBJECT() Function

SQL Server 2022 enhances its JSON capabilities with the JSON_OBJECT() function, providing a streamlined approach to transforming SQL query results into JSON format directly within SQL queries. This functionality is invaluable for developers working in environments where SQL Server interacts with web services, applications, or systems that consume or produce…
T-SQL

Storing Hierarchical Data in SQL Server (Available Options)

There are various options on how to save hierarchical data in SQL Server. This article is an overview of them demonstrating their usage on a simple dataset of employees and their managers. I will extend this article when a new option will be available or if you will advise it…

Read more