Azure-Sql-Db

SQL Interview #24: DROP and CREATE vs ALTER for T-SQL Functions

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: You are writing a script to change the code in a T-SQL function. You can choose to either DROP IF EXISTS and CREATE the procedure, or to use an ALTER statement. What advantage would you get from using an ALTER statement?

2025-03-07

T-SQL 101: 125 Executing Stored Procedures in SQL Server Using T-SQL

In T-SQL, the way we execute a stored procedure is to use the EXECUTE statement. We can write it as EXECUTE or as EXEC. You’ll almost always see it written as EXEC. In the first example above, I’ve executed a stored procedure called dbo.GetCinemaChanges. Note that I’ve included the dbo schema in the name of the procedure. For procedures like this, you should always include the schema name. The procedure also takes one parameter called @CurrentDate and set it to the 28th February 2012.

2025-03-06

Calculating sales volume in food wholesaling systems

In a previous post, I described the issues with measuring quantities in warehousing systems. I showed how you could well need to measure more than just quantities purchased or sold. You often need to deal with the hierarchies of containers that goods are supplied in. But while food wholesale systems will need to deal with quantities like I described in that post, they often have another layer of complexity. Items are often sold by:

2025-03-05

T-SQL 101: 124 Understanding Stored Procedures in SQL Server T-SQL

Stored procedures are prepackaged groups of T-SQL statements. They can also be written in other languages, but most of the time they’ll be written in T-SQL. There are pros and cons with using stored procedures. In the example shown, I use the EXEC statement to execute a stored procedure called SDU_Tools.ListAllDataTypesInUse. Parameters The procedure also takes a number of parameters. By using different parameters values, I can change the behaviour of the stored procedure.

2025-03-04

T-SQL 101: 123 Using Multiple CTEs in a Single T-SQL Query

I mentioned earlier that CTEs can be used to clean up or tidy more complicated queries. You will find people though, who really don’t like them at all. My aim here isn’t to argue for or against them, as it’s a complex topic, but to make sure you know how to write queries with them, and more importantly, to be able to read queries that use them. At this point, I want you to understand the syntax.

2025-03-02

Calculating sales volumes in general warehousing systems

After one of my recent posts, I was asked what an outer quantity was. This term is common in warehousing and logistics. So many business applications assume that you sell things by quantity, and that you just count the number in vs the number out. But that’s not how it works. A system that’s useful for these tasks, understands way more than that. The terms unit, inner, outer, shipper, layer, and pallet refer to different levels or units of packaging and organization in the supply chain.

2025-03-01

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