The Bit Bucket

SDU Tools: List and Retrust Untrusted Check Constraints in SQL Server

Our free SDU Tools for developers and DBAs, now includes a very large number of tools, with procedures, functions, and views. I do a lot of reviewing of database designs and one thing I always check for is check constraints that are not currently trusted. So we added a tool that can help to find these. It’s called ListUntrustedCheckConstraints.

The procedure takes three parameters.

@DatabaseName is the database to process @SchemasToList is a comma-delimited list of schema names to process @TablesToList is a comma-delimited list of table names to process

2025-02-25

T-SQL 101: 120 Nested vs Correlated Subqueries in SQL Server T-SQL

In the previous T-SQL 101 post, I mentioned that some subqueries depend upon the outer query and others don’t. That’s the distinction between what are called nested and correlated subqueries.

Nested Subquery

I mentioned that you can also use a subquery to return a table of values. In the first example above, I’ve selected some columns from order lines and then said inner join to a derived table of products where their outer quantity is greater than 24.

2025-02-24

SQL Interview 23: Logical issues related to ISNULL in SQL Server T-SQL

This is a post in the SQL Interview series. These aren’t trick or gotcha questions, they’re just questions designed to scope out a candidate’s knowledge around SQL Server and Azure SQL Database.

Section: Development Level: Medium

Question:

A query included within a stored procedure, has a predicate written like this:

WHERE ISNULL(ProductSize, '') = ISNULL(@ProductSize, '')

A problem has been identified with this predicate. The system needs to be able to differentiate between NULL values for product size and blank strings. Currently both are treated as though they are the same.

2025-02-23

T-SQL 101: 119 Checking for Existence with EXISTS Subqueries

Sometimes what we’re interested in is just whether or not a row exists.

In the example shown, I’m saying I want to select product ID and description from the products table where there is at least one order line that has the picked quantity less than the ordered quantity. That means that someone ordered a certain number, but for some reason, we didn’t pick (i.e. got ready for shipment) the quantity that they wanted.

2025-02-22

SQL Interview 22: Working with Credentials in Azure SQL DB

This is a post in the SQL Interview series. These aren’t trick or gotcha questions, they’re just questions designed to scope out a candidate’s knowledge around SQL Server and Azure SQL Database.

Section: Security Level: Medium

Question:

With SQL Server on-premises, you can create a CREDENTIAL and they are stored in the master database.

What option do you have with Azure SQL Database?

Answer:

You can create a DATABASE SCOPED CREDENTIAL in both SQL Server and in Azure SQL Database.

2025-02-21

T-SQL 101: 118 Creating Lists from Subqueries in SQL Server T-SQL

Another way that we can use a subquery is in place of a list of values. I’ve previously shown how the IN operator allows us to supply a set of values, and we check if something or some value is contained in that list.

In this example query, I’m saying that I want a list of all the cinemas that have a credit rating that isn’t OK. Perhaps it’s something to do with not paying their bills, or defaulted in some way.

2025-02-20

SDU Tools: SQL Server Type

Our free SDU Tools for developers and DBAs, now includes a very large number of tools, with procedures, functions, and views. A request that I had a while back was to have a function that returned what type of SQL Server the code was being executed on. So we added the SQLServerType function.

It takes no parameters.

As this is an enumerated value, it’s just a CASE statement.

Find out more

You can see it in action in the main image above, and in the video here. The full current version of the code is also shown below:

2025-02-19

T-SQL 101: 117 Creating Scalar Subqueries in SQL Server T-SQL

The first example of subqueries types in this series is what’s called a scalar subquery. In this case, I’m trying to work out which orders were on the last day that we delivered any orders. We deliver orders on various days, but on the last day that we did deliver orders, tell me which orders they were.

The problem with this is that we don’t know what the delivery date was. If I did, I could just use that date value in a WHERE clause. So what we can do instead is to use a subquery, where we work out the maximum delivery date from the orders table, and then that will return us back a date. Finally, we ask for all the orders for that particular day.

2025-02-18

SDU Tools: Last SQL Server Restart

Our free SDU Tools for developers and DBAs, now includes a very large number of tools, with procedures, functions, and views. A request that I had a while back was to have a function that returned the last time that SQL Server was restarted. So we added the LastSQLServerRestart function.

It takes no parameters.

I used to have a more complex way to work this out, but thanks to a great suggestion from our buddy Rob Wylie, it now just looks for the creation time of tempdb. That’s pretty good and simple to implement.

2025-02-17

T-SQL 101: 116 Using Subqueries in SQL Server T-SQL

Subqueries allow us to reuse data from one query in another query. There are three basic ways that this can work:

Row sets (tables) - a subquery can return an entire row set with columns. The basic structure of that is shown in the example above.

Lists - a type of row set that has a single column. It’s often used with an IN clause.

Scalar values - a single value returned from a query i.e., one row with one column. These are often used in place of values in expressions or predicates.

2025-02-16