How to have table reside in a filegroup that is different from the filegroup of indices

I'm using SQL Server 2014.  Can someone tell me how to make the table reside in one filegroup while its primary key and subsequent keys reside in another filegroup?

I wrote the following code specifying the primary key is to reside in a filegroup named @ndxFileGrp and the table in @dbsFileGrp.  But when I execute the code, the table and index are stored in the same filegroup named @ndxFileGrp.  Thank you for help.

ALTER PROCEDURE [dbo].[CreateTables] 
	-- Add the parameters for the stored procedure here
	@dataSrc char(5), 
	@yr int
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

	SET ANSI_PADDING ON;

	DECLARE @tblName    VARCHAR(MAX), 
	        @tblPKey    VARCHAR(MAX),
		@dbsFileGrp VARCHAR(MAX),
		@ndxFileGrp VARCHAR(MAX),
		@sql        NVARCHAR(MAX);

    

	-- =============================
	-- CHARGE Table
	-- =============================
		

	IF UPPER(@dataSrc) <> 'ST_PA'
		BEGIN
			SELECT @tblName = 'dbo.' + UPPER(@dataSrc) + '_CHRG_' + STR(@yr,2);
			SELECT @tblPkey = UPPER(@dataSrc) + '_CHRG_' + STR(@yr,2) + '_PK';
			SELECT @dbsFileGrp = 'SQL_' + SUBSTRING(UPPER(@dataSrc), 1, 2) + SUBSTRING(UPPER(@dataSrc), 4, 2) + '_DBS_FG';
			SELECT @ndxFileGrp = 'SQL_' + SUBSTRING(UPPER(@dataSrc), 1, 2) + SUBSTRING(UPPER(@dataSrc), 4, 2) + '_IDX_FG';
			
			SELECT @sql = 'CREATE TABLE ' + RTRIM(@tblName) + 
			                          '(  [CNVRSN_SRC_CD]	varchar(5)		NOT NULL, ' +
						  '   [LHS_REC_NUM]		numeric(9, 0)	NOT NULL, ' +
						  '   [REV_SQNC_NUM]	numeric(3, 0)	NOT NULL, ' +
						  '   [REV_CHRG_AMT]	numeric(9, 2)	NULL, ' +
						  '   [REV_COST_AMT]	numeric(9, 2)	NULL, ' +
						  '   [RCV_DT]			datetime2(0)	NULL, ' +
						  '   [UNIT_OF_SRVC]	numeric(6, 0)	NULL, ' +
						  ' CONSTRAINT ' + @tblPkey + ' PRIMARY KEY CLUSTERED ' +
						  ' ( [LHS_REC_NUM]    ASC, ' +
						  '	  [CNVRSN_SRC_CD]  ASC, ' +
						  '   [REV_SQNC_NUM]   ASC  ' +
						  ' ) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [' + @ndxFileGrp  + '] ' +
						  ') ON ' + @dbsFileGrp;

			IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES 
                          WHERE TABLE_NAME = @tblName)
			BEGIN
			    EXEC sp_executesql @sql;			      
			END
		
		END 

	SET ANSI_PADDING OFF

END

Open in new window

Maria TorresData AnalystAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Vitor MontalvãoMSSQL Senior EngineerCommented:
PRIMARY KEY CLUSTERED
All clustered keys can't stay in another different filegroup from the table, as being clustered means to be physically in the table.

For non-clustered keys and indexes you can create or move them to any different filegroup.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Maria TorresData AnalystAuthor Commented:
Thank you for clarifying.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
SQL

From novice to tech pro — start learning today.