The SQL Server Equivalent to GROUP_CONCAT()

Before SQL Server 2017 came along, there wasn’t a T-SQL equivalent of the MySQL GROUP_CONCAT() function. This function allows you to return a result set as a comma-separated list, as opposed to listing each row as a separate row (as with a normal result set).

Prior to SQL Server 2017, if you wanted to put your result into a comma separated list, you’d need to find a workaround, perhaps using a combination of STUFF() and FOR XML PATH().

However, T-SQL now has the STRING_AGG() function which is available from SQL Server 2017. This function does pretty much the same thing as MySQL’s GROUP_CONCAT() function (with some minor differences).

Continue reading

How to Return Query Results as a Comma Separated List in SQL Server – STRING_AGG()

Starting with SQL Server 2017, you can now make your query results appear as a list. This means you can have your result set appear as a comma-separated list, a space-separated list, or whatever separator you choose to use.

While it’s true that you could achieve this same effect prior to SQL Server 2017, it was a bit fiddly.

Transact-SQL now has the STRING_AGG() function, which concatenates the values of string expressions and places separator values between them. This works in much the same way to MySQL’s GROUP_CONCAT() function.

This article provides examples that demonstrate the T-SQL STRING_AGG() function.

Continue reading

How to Return the Number of Rows in a Query Result in SQL Server

When working with databases, sometimes you want to find out how many rows will be returned by a query, without actually returning the results of the query. Or sometimes you might just want to find out how many rows are in a given table.

In SQL Server, you can use T-SQL‘s COUNT() function to return the number of rows that would be returned in a query.

Continue reading

6 Ways to Check the Size of a Database in SQL Server using T-SQL

If you’re using a GUI tool, such as SSMS to manage your databases, you can easily check the size of your database by clicking your way through the GUI (right-click the database, point to Reports, then Standard Reports, and then click Disk Usage).

However, if you prefer to use T-SQL to manage your databases, you’ll need to run a query that returns this information.

This article presents six ways to check the size of a SQL Server database using T-SQL.

Continue reading

How to Add a Log File to a SQL Server Database (T-SQL)

When you create a database in SQL Server using the default options, one data file and one log file are created. The data file stores the data and database objects (such as tables, views, stored procedures, etc). The log file stores the information that is required to recover all transactions in the database. If you have a growing database, you may find yourself in the situation where you need to add a new log file (and/or data file).

Just as you can add more data files to an existing database in SQL Server, you can also add more log files. However, the syntax is slightly different depending on whether you’re creating a data file or a log file. Adding a data file requires ADD FILE while adding a log file requires ADD LOG FILE.

This article demonstrates how to use T-SQL to add a log file to an existing database in SQL Server.

Continue reading

How to Add a Filegroup to a SQL Server Database (T-SQL)

At a minimum, a SQL Server database has two operating system files; the data file, and the log file.

The data file holds the actual data in the database, as well as objects such as tables, indexes, stored procedures, and views. The log file contains the information that is required to recover all transactions in the database.

When you create a database using the default options, both of these files are created. These files are created in the primary filegroup. This filegroup contains the primary data file and any secondary files that are not put into other filegroups. So the primary filegroup is the default filegroup (unless it’s changed by using the ALTER DATABASE statement).

When you create multiple data files, you also have the option of grouping them into a user-defined filegroup. This allows you to group files logically into their own filegroup that you created. This filegroup will then exist in addition to the primary filegroup. Database objects will remain will remain within the primary filegroup.

This article demonstrates how to create a user-defined filegroup and add some data files to it.

Continue reading

How to Add a Data File to a SQL Server Database (T-SQL)

In SQL Server, databases store their data and objects (such as tables, views, stored procedures, etc) in files. When you create a database using the default options, one data file and one log file are created. However, you’re not restricted to just one of each file. You can add data files (and log files) to a database at a later date if required.

This article contains examples of using T-SQL to add a data file to an existing database in SQL Server.

Continue reading

How to Remove a Data File from a SQL Server Database (T-SQL)

SQL Server databases store their data and objects in files. Each database has at least one data file (and a log file), but it’s not restricted to just one – it could have many data files. If you ever find yourself in the situation where you need to remove a data file from a database, you’ll need to empty that file first, before removing it.

But don’t worry, emptying a file doesn’t actually delete the data. It simply migrates the file’s data to other files within the same filegroup.

The examples below demonstrate how to empty a data file, then remove it from the database using Transact-SQL.

Continue reading

How to Reduce the Size of a Data File in SQL Server (T-SQL)

In SQL Server, when using Transact-SQL, reducing the size of a data file requires a different syntax to increasing it. To reduce a file size using T-SQL, use the DBCC SHRINKFILE command, and provide the name of the data file along with the size you’d like to reduce it to.

Below are examples of reducing the file size of a data file in SQL Server using T-SQL.

Continue reading

How to Increase the File Size of a Data File in SQL Server (T-SQL)

If you’re using the SQL Server Management Studio GUI, you can increase the size of a data file by right-clicking on the relevant database, selecting Properties, selecting Files under Database Properties, then increasing the value in the Initial Size (MB) column for the applicable data file.

If you’re using Transact-SQL however, you can increase the data file’s size by using the ALTER DATABASE statement with the MODIFY FILE argument. The following example demonstrates how to do this.

Continue reading