How sys.dm_exec_describe_first_result_set Works in SQL Server

In SQL Server, the sys.dm_exec_describe_first_result_set dynamic management function returns the metadata of the first result set for a given T-SQL statement or statements.

This function uses the same algorithm as the sp_describe_first_result_set system stored procedure, and does pretty much the same thing.

It accepts three parameters, the first of which is the T-SQL statement/s you’re analysing.

Continue reading

SQL Server Error 111: “…must be the first statement in a query batch”

In SQL Server, a batch is a group of one or more T-SQL statements sent at the same time from an application to SQL Server for execution.

If you encounter an error like this:

Msg 111, Level 15, State 1, Line 2
'CREATE VIEW' must be the first statement in a query batch.

It’s probably because you’re combining the statement with other statements in the same batch, which is not allowed in batches.

The first part of the error message will depend on the actual statement that you’re using in your batch. In my case it’s CREATE VIEW, but it could just as easily be CREATE PROCEDURE, CREATE FUNCTION, etc if those are the statements you’re using.

Continue reading

Compress a Specific Partition within a Table in SQL Server (T-SQL)

When you compress a partitioned table in SQL Server, you can compress all partitions, some, or just one partition.

To do this, use the REBUILD PARTITION syntax within the ALTER TABLE statement.

When doing this, you can rebuild just the specified partition or all partitions. Alternatively, you can rebuild all partitions, while compressing only a specific partition or list of partitions.

Continue reading

Create a Table with Compression in SQL Server (T-SQL)

When you create a table in SQL Server, you have the option of using data compression.

Data compression helps reduce the size of the database. It can also help improve performance of I/O intensive workloads due to the data being stored in fewer pages, therefore reducing the number of pages that queries need to read from disk.

To do this, use the DATA_COMPRESSION option when creating the table.

Continue reading

Estimate Data Compression Savings in SQL Server

SQL Server has a system stored procedure called sp_estimate_data_compression_savings, which allows you to check an object’s size and its estimated size with various levels of compression.

If the object is already compressed, you can use this procedure to estimate its size when recompressed.

Objects can be compressed by using row, page, columnstore or columnstore archive compression.

Compression can be evaluated for whole tables or parts of tables. This includes heaps, clustered indexes, nonclustered indexes, columnstore indexes, indexed views, and table and index partitions.

Continue reading

SQL Server Error 110: There are fewer columns in the INSERT statement than values specified in the VALUES clause.

Error message 110 is a commonly encountered error in SQL Server when inserting data into a table. The full error looks like this:

Msg 110, Level 15, State 1, Line 1
There are fewer columns in the INSERT statement than values specified in the VALUES clause. The number of values in the VALUES clause must match the number of columns specified in the INSERT statement.

This happens when you specify less columns in the INSERT statement than the number of values that you’re trying to insert with the VALUES clause.

This will occur if you accidentally omit one or more columns from the INSERT statement.

You’d get a similar (but technically different) error if you tried to do the opposite – specify more columns in the INSERT statement than you try to insert.

Continue reading