标签:reac star 分离库 数据 select 提前 dbid sys _id
1 --分离代码 2 USE master; 3 DECLARE @temp NVARCHAR(4000); 4 SET @temp = ‘‘; 5 DECLARE cursorname CURSOR FORWARD_ONLY 6 FOR 7 SELECT name 8 FROM sys.databases 9 WHERE database_id > 4 10 AND name LIKE ‘test_%‘ 11 AND state_desc = ‘online‘ 12 AND is_read_only = 0 13 AND is_published = 0 14 AND name IN ( ‘test_1012986‘, ‘test_1012846‘, ‘test_1012852‘, 15 ‘test_1012766‘, ‘test_1012744‘, ‘test_1012743‘, 16 ‘test_1012601‘, ‘test_1012448‘, ‘test_1012302‘, 17 ‘test_1012187‘, ‘test_1012051‘, ‘test_1011929‘, 18 ‘test_1011919‘, ‘test_1011880‘, ‘test_1011735‘, 19 ‘test_1011734‘, ‘test_1011587‘, ‘test_1011440‘, 20 ‘test_1011439‘, ‘test_1011127‘, ‘test_1011061‘, 21 ‘test_1011021‘, ‘test_1011006‘, ‘test_1010999‘, 22 ‘test_1010916‘, ‘test_1010833‘, ‘test_1010663‘ 23 ); 24 25 OPEN cursorname; 26 DECLARE @curname NVARCHAR(50); 27 FETCH NEXT FROM cursorname 28 INTO @curname; 29 WHILE ( 30 @@fetch_status = 0 31 ) 32 BEGIN 33 ---提前判断一下该库是否有链接,如有则kill对应的库的链接 34 IF 35 ( 36 SELECT COUNT(1) 37 FROM sys.sysprocesses 38 WHERE DB_NAME(dbid) = @curname 39 ) >= 1 40 BEGIN 41 DECLARE @spid VARCHAR(200); 42 DECLARE cur_spid CURSOR FORWARD_ONLY 43 FOR 44 SELECT spid 45 FROM sys.sysprocesses 46 WHERE DB_NAME(dbid) = @curname; 47 OPEN cur_spid; 48 FETCH NEXT FROM cur_spid 49 INTO @spid; 50 WHILE ( 51 @@fetch_status = 0 52 ) 53 BEGIN 54 EXEC (‘ kill ‘ + @spid); 55 -- PRINT (‘ kill ‘ + @spid); 56 FETCH NEXT FROM cur_spid 57 INTO @spid; 58 END; 59 CLOSE cur_spid; 60 DEALLOCATE cur_spid; 61 SET @temp 62 = N‘ EXEC sp_detach_db ‘ + ‘‘‘‘ + @curname + ‘‘‘‘ + ‘, true‘; 63 EXEC (@temp); 64 -- PRINT @temp; 65 END; 66 ELSE 67 BEGIN 68 SET @temp 69 = N‘ EXEC sp_detach_db ‘ + ‘‘‘‘ + @curname + ‘‘‘‘ + ‘, true‘; 70 EXEC (@temp); 71 -- PRINT @temp; 72 END; 73 FETCH NEXT FROM cursorname 74 INTO @curname; 75 END; 76 CLOSE cursorname; 77 DEALLOCATE cursorname;
1 --验证代码 2 SELECT name FROM sys.databases
标签:reac star 分离库 数据 select 提前 dbid sys _id
原文地址:https://www.cnblogs.com/jil-wen/p/9166360.html