Azure-Sql-Db

T-SQL 101: 131 What are Identity Columns in SQL Server?

In the previous post, I talked about how you could add default values for columns.

One special type of default value is called an IDENTITY constraint. These columns are one way to achieve automatic numbering in a column. There are many pros and cons about using these but at this point, I want to make sure you know how to use them.

The main difference from other columns is that you don’t put them in your INSERT statements. In fact, unlike other types of default constraint, you cannot just put the values in there. It will raise an error.

2025-03-18

T-SQL 101: 130 What are Default Constraints in T-SQL?

Default constraints allow us to automatically provide values for columns when:

  • we don’t supply them ourselves
  • they are marked as NOT NULL so they are required

When you’re building an INSERT statement, you don’t have to give values for all columns. Columns that are declared as NULL aren’t mandatory, so you don’t have to supply those columns at all. Those columns can just be NULL.

But if you have a column that is declared as NOT NULL, so it requires a value, but you haven’t supplied it in the INSERT statement, then a default constraint can provide the value. This is configured as part of the table definition, or added as a constraint later by altering the table definition.

2025-03-16

SQL Interview: 26 Whole of data operations when adding columns in SQL Server

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: Administration Level: Medium

Question:

You need to add additional columns to a table. You are concerned that you do not want to lock the table for long periods. You plan to avoid any operation that would rewrite every row i.e., avoid whole of data operations.

2025-03-15

T-SQL 101: 129 Inserting Multiple Rows at Once with VALUES clause in SQL Server T-SQL

Prior to SQL Server 2008, we could only insert a single row at a time. SQL Server 2008 added the ability to have multiple rows of data in the VALUES clause.

Each row is created from what are typically called row constructors. In the example above, I’m inserting 3 rows into the Cinema Groups table. The syntax allows for up to 1000 rows to be inserted at once.

Before you get to that number of rows though, you might exceed the maximum allowable length for a T-SQL statement, if you have a lot of columns, each with a lot of data. The maximum length of a statement is in theory 2GB but most people run into the batch size limit of 65536 * network packet size or about 268MB, first. Either way, it’s a lot of data in a single statement, and running into these limits is uncommon.

2025-03-14

SDU Tools: Reset Sequence using SQL Server T-SQL

Our free SDU Tools for developers and DBAs, now includes a very large number of tools, with procedures, functions, and views. Users that are new to working with sequences often don’t have simple options for resetting them back to a specific value. Many business intelligence ETL or ELT processes need to have a way to do this. So we added a tool that can help. It’s called ResetSequence.

The procedure takes three parameters.

2025-03-13

T-SQL 101: 128 Inserting Data into a Table using SQL Server T-SQL

To insert new rows into a table, you will mostly use the INSERT statement.

In the example above, I’ve inserted one row into the dbo.CinemaGroups table. There are a few aspects to this statement.

First, you should always use two-part names for the table i.e., use dbo.CinemaGroups not just CinemaGroups.

Second, there is an optional INTO keyword for INSERT statements. You can say INSERT INTO dbo.CinemaGroups. While I’m generally pretty pedantic about these things, I can’t see any value that the word INTO adds here and I don’t use it. (Worth noting that other dialects of SQL like the one in Access, insist that it’s there).

2025-03-12

SQL Interview 25: Extended Stored Procedures in SQL Server

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: Advanced

Question:

You are reviewing stored procedures in the master database.

You note both Stored Procedures, and Extended Stored Procedures.

What is the difference between these types of procedures?

Answer:

Stored Procedures are written in T-SQL or SQL CLR and run in the standard user memory space of a SQL Server session.

2025-03-11

T-SQL 101: 127 Querying the SQL Server System Catalog

There are times that you need to be able to query the database and its internal structures rather than querying the user tables.

In the example above, I’ve asked for details of all the databases, and then for details of all the schemas in the current database, followed by details for all the tables and columns in the current database.

These are a well-designed set of system views that are easy to work with. They are often referred to as catalog views. You can even see all of them as part of querying sys.views.

2025-03-10

SDU Tools: List User-Defined Data Types 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 the use of user-defined data types. I’ve had situations where these have caused me substantial issues. So we added a tool that can help to find these. It’s called ListUserDefinedDataTypes.

The procedure takes one parameter.

2025-03-09

T-SQL 101: 126 Executing Dynamic SQL Statements in SQL Server T-SQL

It’s also possible to create the command dynamically before you execute it.

In the example above, I’ve set a number of different parts of a SQL statement into variables, and then used them to construct a complete SQL statement that I’ve then executed. I just have to create a valid SQL statement as a string.

Warning

While this might seem really, really convenient, and it can be incredibly useful, it is something you need to be extraordinarily careful with. One of the problems with this is that it can easily open you up to what’s called SQL injection attacks.

2025-03-08