-------------------------------------------------------
-- Partitioned Tables Whitepaper Examples
-- Creating Filegroups
-- September 2005
-- Written by Jason Wong
-------------------------------------------------------

-------------------------------------------------------
-- This script adds 24 FILEGROUPS and 24 FILES
-- (one file for each filegroup) to Sales database. 

-- This script uses a table to build the filegroups - make sure
-- to modify the INSERT statements to use the appropriate disks. 
-- Also, the initial size (1MB), maximum size (100MB) and the
-- growth rate (5MB) for these filegroups is different (MUCH smaller)
-- than the whitepaper showed. If desired, change the 
-- string to use the desired sizes.
-- 
-- Make sure you CREATE the database first and make sure 
-- you create a C:\PerfmonHistory subdir (you can modify this location in 
-- this script) before executing this!
-------------------------------------------------------

-------------------------------------------------------
-- Add 24 FILEGROUPS to the Sales Database
-------------------------------------------------------
GO
-- DROP DATABASE PerfmonHistory -- for testing
-- CREATE DATABASE PerfmonHistory -- for testing
GO

USE [PerfmonHistory]
GO
ALTER DATABASE PerfmonHistory
ADD FILEGROUP [FG1]
GO

ALTER DATABASE PerfmonHistory
ADD FILEGROUP [FG2]
GO

ALTER DATABASE PerfmonHistory
ADD FILEGROUP [FG3]
GO

ALTER DATABASE PerfmonHistory
ADD FILEGROUP [FG4]
GO

ALTER DATABASE PerfmonHistory
ADD FILEGROUP [FG5]
GO

ALTER DATABASE PerfmonHistory
ADD FILEGROUP [FG6]
GO

ALTER DATABASE PerfmonHistory
ADD FILEGROUP [FG7]
GO

ALTER DATABASE PerfmonHistory
ADD FILEGROUP [FG8]
GO

ALTER DATABASE PerfmonHistory
ADD FILEGROUP [FG9]
GO

ALTER DATABASE PerfmonHistory
ADD FILEGROUP [FG10]
GO

ALTER DATABASE PerfmonHistory
ADD FILEGROUP [FG11]
GO

ALTER DATABASE PerfmonHistory
ADD FILEGROUP [FG12]
GO

ALTER DATABASE PerfmonHistory
ADD FILEGROUP [FG13]
GO

ALTER DATABASE PerfmonHistory
ADD FILEGROUP [FG14]
GO

ALTER DATABASE PerfmonHistory
ADD FILEGROUP [FG15]
GO

ALTER DATABASE PerfmonHistory
ADD FILEGROUP [FG16]
GO

ALTER DATABASE PerfmonHistory
ADD FILEGROUP [FG17]
GO

ALTER DATABASE PerfmonHistory
ADD FILEGROUP [FG18]
GO

ALTER DATABASE PerfmonHistory
ADD FILEGROUP [FG19]
GO

ALTER DATABASE PerfmonHistory
ADD FILEGROUP [FG20]
GO

ALTER DATABASE PerfmonHistory
ADD FILEGROUP [FG21]
GO

ALTER DATABASE PerfmonHistory
ADD FILEGROUP [FG22]
GO

ALTER DATABASE PerfmonHistory
ADD FILEGROUP [FG23]
GO

ALTER DATABASE PerfmonHistory
ADD FILEGROUP [FG24]
GO

-------------------------------------------------------
-- Add Files to each Filegroup
-------------------------------------------------------
USE PerfmonHistory
go
CREATE TABLE PerfmonHistory.dbo.FilegroupInfo
(
	PartitionNumber	smallint,
	FilegroupNumber	tinyint,
	Location	nvarchar(50)
)
GO

-- All files go to DRIVE C:\PerfmonHistory - for testing...
INSERT dbo.FilegroupInfo VALUES (1, 1, N'C:\PerfmonHistory')
INSERT dbo.FilegroupInfo VALUES (2, 2, N'C:\PerfmonHistory')
INSERT dbo.FilegroupInfo VALUES (3, 3, N'C:\PerfmonHistory')
INSERT dbo.FilegroupInfo VALUES (4, 4, N'C:\PerfmonHistory')
INSERT dbo.FilegroupInfo VALUES (5, 5, N'C:\PerfmonHistory')
INSERT dbo.FilegroupInfo VALUES (6, 7, N'C:\PerfmonHistory')
INSERT dbo.FilegroupInfo VALUES (7, 8, N'C:\PerfmonHistory')
INSERT dbo.FilegroupInfo VALUES (8, 9, N'C:\PerfmonHistory')
INSERT dbo.FilegroupInfo VALUES (9, 9, N'C:\PerfmonHistory')
INSERT dbo.FilegroupInfo VALUES (10, 10, N'C:\PerfmonHistory')
INSERT dbo.FilegroupInfo VALUES (11, 11, N'C:\PerfmonHistory')
INSERT dbo.FilegroupInfo VALUES (12, 12, N'C:\PerfmonHistory')
INSERT dbo.FilegroupInfo VALUES (13, 13, N'C:\PerfmonHistory')
INSERT dbo.FilegroupInfo VALUES (14, 14, N'C:\PerfmonHistory')
INSERT dbo.FilegroupInfo VALUES (15, 15, N'C:\PerfmonHistory')
INSERT dbo.FilegroupInfo VALUES (16, 16, N'C:\PerfmonHistory')
INSERT dbo.FilegroupInfo VALUES (17, 17, N'C:\PerfmonHistory')
INSERT dbo.FilegroupInfo VALUES (18, 18, N'C:\PerfmonHistory')
INSERT dbo.FilegroupInfo VALUES (19, 19, N'C:\PerfmonHistory')
INSERT dbo.FilegroupInfo VALUES (20, 20, N'C:\PerfmonHistory')
INSERT dbo.FilegroupInfo VALUES (21, 21, N'C:\PerfmonHistory')
INSERT dbo.FilegroupInfo VALUES (22, 22, N'C:\PerfmonHistory')
INSERT dbo.FilegroupInfo VALUES (23, 23, N'C:\PerfmonHistory')
INSERT dbo.FilegroupInfo VALUES (24, 24, N'C:\PerfmonHistory')
GO


DECLARE @PartitionNumber	smallint,
	@FilegroupNumber	tinyint,
	@Location		nvarchar(50),
	@ExecStr		nvarchar(300)

DECLARE FilegroupsToCreate CURSOR FOR 
	SELECT * FROM dbo.FileGroupInfo
	ORDER BY PartitionNumber
OPEN FilegroupsToCreate 
FETCH NEXT FROM FilegroupsToCreate INTO @PartitionNumber, @FilegroupNumber, @Location

WHILE (@@fetch_status <> -1) -- (-1) you've fetched beyond the cursor
BEGIN
    IF (@@fetch_status <> -2) -- (-2) you've fetched a row that no longer exists
    BEGIN  -- Fetch_status must be 0
	SELECT @ExecStr = N'ALTER DATABASE PerfmonHistory ' + 
			  N'	ADD FILE ' + 
			  N'	  (NAME = N''PerformanceHistoryFG' + CONVERT(nvarchar, @PartitionNumber) + N'File1'',' +
			  N'       FILENAME = N''' + @Location + N'\PerformanceHistoryFG' + CONVERT(nvarchar, @PartitionNumber) + 'File1.ndf'',' + 
			  N'       SIZE = 1MB,' +  
			  N'       MAXSIZE = 100MB,' +
			  N'       FILEGROWTH = 5MB)' +
			  N' TO FILEGROUP ' + N'[FG' + CONVERT(nvarchar, @PartitionNumber) + N']'
	--SELECT (@ExecStr)
	EXEC (@ExecStr) -- DO NOT UNCOMMENT THIS UNTIL YOU ARE SURE THE NAMES and LOCATIONS are correct.
    END

FETCH NEXT FROM FilegroupsToCreate INTO @PartitionNumber, @FilegroupNumber, @Location
END

DEALLOCATE FilegroupsToCreate 
GO

-- Each of the twenty four files should be added with the information you used within the FileGroupInfo table
-- ALTER DATABASE PerfmonHistory 	
-- ADD FILE 	  
-- 	(NAME = N'PerfmonHistoryFG1File1',
--         FILENAME = N'E:\PerfmonHistory\PerfmonHistoryFG1File1.ndf'
--   	SIZE = 1MB,
--         MAXSIZE = 100MB,
--         FILEGROWTH = 5MB) 
-- TO FILEGROUP [FG1]

-------------------------------------------------------
-- Verify all files and filegroups
-------------------------------------------------------
USE PerfmonHistory
GO

EXEC sp_helpfilegroup
exec sp_helpfile
GO
