-
Notifications
You must be signed in to change notification settings - Fork 683
/
Copy pathdbo.usp_EnableAndAddFileStreamForDatabase.sql
96 lines (78 loc) · 3.25 KB
/
dbo.usp_EnableAndAddFileStreamForDatabase.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
--EXEC ('USE master; EXEC [dbo].[usp_EnableAndAddFileStreamForDatabase] ''OpalTAQ''')
USE [master];
GO
CREATE PROCEDURE [dbo].[usp_EnableAndAddFileStreamForDatabase] (@NameOfDatabase VARCHAR(100) = '')
AS
BEGIN
IF(COALESCE(@NameOfDatabase,'') <> '')
BEGIN
SET NOCOUNT ON;
/*************************************************************/
PRINT '1. CREATE TEMP DB FOR FILESTREAM SETUP'
EXEC('USE [Master];');
IF EXISTS (SELECT 1
FROM master.INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE='BASE TABLE'
AND TABLE_NAME='fsSetupInfo')
DROP TABLE dbo.fsSetupInfo;
CREATE TABLE dbo.fsSetupInfo ( DBName VARCHAR(100) NULL,
FilePath VARCHAR(255) NULL,
DirPath VARCHAR(255) NULL
);
INSERT INTO dbo.fsSetupInfo (DBName, FilePath, DirPath)
VALUES (@NameOfDatabase, '', '');
PRINT '********** COMPLETED 1. CREATE TEMP DB FOR FILESTREAM SETUP'
/*************************************************************/
PRINT '2. CONFIGURE SQL INSTANCE FOR FILESTREAM'
/*
0 -Disables FILESTREAM
1 -Enables FILESTREAM for T-SQL
2 -Enables FILESTREAM for T-SQL and Win32 streaming access
*/
EXEC('USE master;')
EXEC sp_configure filestream_access_level, 2;
RECONFIGURE WITH OVERRIDE;
PRINT '********** COMPLETED - 2. CONFIGURE SQL INSTANCE FOR FILESTREAM'
/*************************************************************/
PRINT '3. GET/SET FILEPATH/FILEDIRECTORY OF DATABASE PHYSICAL FILE'
DECLARE @DBName VARCHAR(100) = ''
,@DBFilePath VARCHAR(255) = ''
,@DBDirPath VARCHAR(255) = ''
SELECT TOP 1 @DBName = DBName
FROM master.dbo.fsSetupInfo;
SELECT TOP 1 @DBFilePath = physical_name
FROM sys.master_files
WHERE FILE_ID = 1
AND physical_name LIKE '%' + @DBName + '.mdf';
UPDATE master.dbo.fsSetupInfo SET FilePath = @DBFilePath;
UPDATE master.dbo.fsSetupInfo SET DirPath = (SELECT SUBSTRING(@DBFilePath, 1, LEN(@DBFilePath) - CHARINDEX('\', REVERSE(@DBFilePath))) + '\');
PRINT '********** COMPLETED 3. GET/SET FILEPATH/FILEDIRECTORY OF DATABASE PHYSICAL FILE'
/*************************************************************/
PRINT '4. CONFIGURE SQL DATABASE FOR FILESTREAM'
/* Add Filegroup to database*/
SELECT TOP 1 @DBDirPath = DirPath, @DBName = DBName
FROM master.dbo.fsSetupInfo;
EXEC('USE ' + @DBName +'; '
+ 'IF NOT EXISTS (SELECT * FROM sys.filegroups WHERE name = ''fs' + @DBName + ''')'
+ ' ALTER DATABASE ' + @DBName + ' ADD FILEGROUP fs' + @DBName + ' CONTAINS FILESTREAM'
);
/* Add a file to the database */
EXEC('USE ' + @DBName +'; '
+ 'IF NOT EXISTS(SELECT physical_name FROM sys.database_files WHERE type_desc = ''FILESTREAM'')'
+ ' ALTER DATABASE ' + @DBName + '
ADD FILE (NAME = ''fs' + @DBName + ''', FILENAME = ''' + @DBDirPath + 'fs' + @DBName + ''')
TO FILEGROUP fs' + @DBName);
PRINT '********** COMPLETED 4. CONFIGURE SQL DATABASE FOR FILESTREAM'
/*************************************************************/
PRINT 'CLEAN UP'
IF EXISTS (SELECT 1
FROM master.INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE='BASE TABLE'
AND TABLE_NAME='fsSetupInfo')
DROP TABLE dbo.fsSetupInfo;
PRINT '********** CLEANUP COMPLETED'
SET NOCOUNT OFF;
END
ELSE
PRINT 'NO DATABASE NAME PROVIDED'
END;