In SQL Server, you can use the FILEGROUP_NAME()
function to return the name of a filegroup, based on its ID.
To return the filegroup name, simply pass its ID to the function.
Example 1 – Basic Query
Here’s an example to demonstrate.
USE WideWorldImporters; SELECT FILEGROUP_NAME(1) AS Result;
Result:
+----------+ | Result | |----------| | PRIMARY | +----------+
Example 2 – More Filegroups
This database has three filegroups, so here’s an example that outputs all three.
SELECT FILEGROUP_NAME(1) AS FG_1, FILEGROUP_NAME(2) AS FG_2, FILEGROUP_NAME(3) AS FG_3, FILEGROUP_NAME(4) AS FG_4;
Result:
+---------+----------+-------------------+--------+ | FG_1 | FG_2 | FG_3 | FG_4 | |---------+----------+-------------------+--------| | PRIMARY | USERDATA | WWI_InMemory_Data | NULL | +---------+----------+-------------------+--------+
Here, I attempted to output a forth filegroup, but in this case, there’s no filegroup with an ID of 4, so we get NULL
for that column.
Return All Filegroups
If you want to return all filegroups for a given database, see How to Return All Filegroups in SQL Server.
Create a Filegroup
If you want to create a new filegroup, see How to Add a Filegroup in SQL Server.
Return Filegroup ID
You can also use FILEGROUP_ID()
to return a filegroup’s ID based on its name.