The Bit Bucket

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

Cosmos Down Under show 15 with guest Saranya Sriram from the Cosmos DB team discussing AI and vector search is now released!

I really enjoyed recording another new Cosmos Down Under podcast this afternoon. It’s now edited and released.

Show 15 features Saranya Sriram from the Azure Cosmos DB team.

In the show, we discussed the AI and vector search capabilities in Azure Cosmos DB.

About Saranya

Saranya is the Asia product lead, and a principal product manager on the engineering team for Azure Cosmos DB, the database that powers OpenAI’s Chat GPT and similar AI-related workloads.

2025-03-03

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. Here’s a breakdown of each term:

2025-03-01

PG Down Under show 4 on avoiding mistakes in PostgreSQL with guest Jimmy Angelakos is published!

I had the pleasure of recording a new PG Down Under podcast tonight. My guest was Jimmy Angelakos.

With over 25 years of experience in software architecture and a deep expertise in PostgreSQL, Jimmy is a recognized authority in the world of database systems. His career has spanned key roles at industry leaders like 2ndQuadrant and EDB, where he contributed to cutting-edge solutions and open-source advancements.

Background

Jimmy’s journey began with a Computer Science degree from the University of Aberdeen, and since then, he has worked with and significantly contributed to numerous open-source tools. His passion for the PostgreSQL community shines through as an active member of PostgreSQL Europe and a regular contributor to the PostgreSQL project. A sought-after speaker, he shares his insights at conferences and events dedicated to databases and open-source software.

2025-02-28

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. There can be up to 1000 rows at a time.

2025-02-26