forked from SQLUndercover/UndercoverToolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmove SQL files in bulk.sql
90 lines (70 loc) · 2.59 KB
/
move SQL files in bulk.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
/******************************************************************
Author: David Fowler
Revision date: 22/04/2020
Version: 1.1
© www.sqlundercover.com
This script is for personal, educational, and internal
corporate purposes, provided that this header is preserved. Redistribution or sale
of this script,in whole or in part, is prohibited without the author's express
written consent.
The software is provided "as is", without warranty of any kind, express or
implied, including but not limited to the warranties of merchantability,
fitness for a particular purpose and noninfringement. in no event shall the
authors or copyright holders be liable for any claim, damages or other
liability, whether in an action of contract, tort or otherwise, arising from,
out of or in connection with the software or the use or other dealings in the
software.
******************************************************************/
--config variables
DECLARE @logpath NVARCHAR(260) = 'G:\SQLLogs'
DECLARE @datapath NVARCHAR(260) = 'E:\SQLData'
DECLARE @movelogs BIT = 1
DECLARE @movedata BIT = 0
--runtime variables
DECLARE @STMT NVARCHAR(4000)
--uncomment the predicates to include and exclude databases as required
DECLARE Files CURSOR STATIC FORWARD_ONLY
FOR
SELECT DB_NAME(database_id)
,type
,name
,REVERSE(SUBSTRING(REVERSE(physical_name), 0, CHARINDEX('\', REVERSE(physical_name))))
FROM sys.master_files
WHERE type IN (0,1)
--AND DB_NAME(database_id) IN ('sqlundercover') --uncomment to include databases
--AND DB_NAME(database_id) NOT IN ('master','tempdb','msdb','model') --uncomment to exclude databases
DECLARE @DBName SYSNAME
DECLARE @type TINYINT
DECLARE @logicalname SYSNAME
DECLARE @physicalname NVARCHAR(260)
--check filepaths finish with a \ and add if they don't
IF (SUBSTRING(@datapath, LEN(@datapath), 1) != '\')
SET @datapath += N'\'
IF (SUBSTRING(@logpath, LEN(@logpath), 1) != '\')
SET @logpath += N'\'
OPEN Files
FETCH NEXT
FROM Files
INTO @DBName
,@type
,@logicalname
,@physicalname
WHILE @@FETCH_STATUS = 0
BEGIN
SET @STMT = N'ALTER DATABASE ' + QUOTENAME(@DBName) + N' MODIFY FILE (NAME = ' + QUOTENAME(@logicalname) + N', FILENAME = '''
SET @STMT += CASE
WHEN @type = 0 AND @movedata = 1
THEN @datapath + @physicalname + ''')'
WHEN @type = 1 AND @movelogs = 1
THEN @logpath + @physicalname + ''')'
END
PRINT @STMT
FETCH NEXT
FROM Files
INTO @DBName
,@type
,@logicalname
,@physicalname
END
CLOSE Files
DEALLOCATE Files