I want to modify the table to add a constraint during a SQL Server database upgrade.
This table is usually indexed in a filegroup named "MY_INDEX", but can also be in a database without this filegroup. In this case, I want the indexing to be performed in the "PRIMARY" filegroup.
I tried to execute the following code:
DECLARE @fgName AS VARCHAR(10) SET @fgName = CASE WHEN EXISTS(SELECT groupname FROM sysfilegroups WHERE groupname = 'MY_INDEX') THEN QUOTENAME('MY_INDEX') ELSE QUOTENAME('PRIMARY') END ALTER TABLE [dbo].[mytable] ADD CONSTRAINT [PK_mytable] PRIMARY KEY ( [myGuid] ASC ) ON @fgName
However, the last line fails because it seems that the filegroup cannot be specified by a variable.
Is it possible?
source share