Azure-Sql-Db

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

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