Sql-Server

T-SQL 101: 122 Simplifying Queries with Common Table Expressions (CTEs) in SQL Server T-SQL

One of the challenges with subqueries is they can get very, very messy if you need to use them more than once. So for example, in the first query above, I’ve created a derived table called BulkProducts. But what if I needed to derive that same table again? Then I would end up with yet another copy of all that code. (And obviously it might be far more complex) A more common problem is that in a SELECT statement, I might be selecting subqueries but every single time I might be needing to calculate a sub-query to get the values.

2025-02-28

SDU Tools: List and Retrust Untrusted Foreign Key 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 foreign key constraints that are not currently trusted. So we added a tool that can help to find these. It’s called ListUntrustedForeignKeys. 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-27

T-SQL 101: 121 Using VALUES Clauses as Row Constructors in SQL Server T-SQL

SQL Server 2008 and later support a VALUES clause. We can use this to construct a table on the fly. In the example above, you can pretty much ignore the outer query as I just put it there to select from the table that was derived in the VALUES clause. With VALUES, I put a list of rows that I want to include. They each must have the same number of columns.

2025-02-26

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.

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.

2025-02-19