-
Notifications
You must be signed in to change notification settings - Fork 23
/
attach_db_script.sql
46 lines (33 loc) · 1.32 KB
/
attach_db_script.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
/*
select * from filespacestats
order by dbname
exec dbm_filespacestats
*/
------------------------------------------------------------------------------
Declare @dbname varchar(32)
Declare @dataname varchar(48)
Declare @datafile varchar(260)
Declare @logname varchar(48)
Declare @logfile varchar(260)
Declare @sql varchar(1024)
DECLARE db_cursor CURSOR FOR SELECT DISTINCT dbname FROM filespacestats
WHERE name not in ('dbamaint','master','model','msdb','OPS')
OPEN db_cursor
FETCH NEXT FROM db_cursor INTO @dbname
WHILE (@@fetch_status <> -1)
BEGIN
--print @dbname
SET @datafile = (SELECT Filename from filespacestats where dbname = @dbname and FileID = '1')
SET @dataname = (SELECT Name from filespacestats where dbname = @dbname and FileID = '1')
SET @logfile = (SELECT Filename from filespacestats where dbname = @dbname and FileID = '2')
SET @logname = (SELECT Name from filespacestats where dbname = @dbname and FileID = '2')
select @sql = 'CREATE DATABASE ' + @dbname + ' ON ' + char(13) +
char(9) + '(NAME = ''' + @dataname + ''', FILENAME = ''' + @datafile + '''),' + char(13) +
char(9) + '(NAME = ''' + RTRIM(@logname) + ''', FILENAME = ''' + RTRIM(@logfile) + ''')' + char(13) +
' FOR ATTACH' + char(13) +
'GO'
print(@sql)
FETCH NEXT FROM db_cursor INTO @dbname
END
CLOSE db_cursor
DEALLOCATE db_cursor