--01.get all database name
SELECT DBNAME = DB_NAME(s_mf.database_id) INTO #DN
FROM
sys.master_files s_mf
WHERE
s_mf.state = 0 and -- ONLINE
HAS_DBACCESS(DB_NAME(s_mf.database_id)) = 1 -- Only look at databases to which we have access
GROUP BY s_mf.database_id
ORDER BY 1
--02.use cursor to fetch db name
-- ---02.01 declare cursor
DECLARE cur CURSOR FOR
SELECT DBNAME FROM #DN
-----02.02 open cursor & declare var
OPEN cur
DECLARE @DbName VARCHAR(200 )
-----02.03 loop db bane
FETCH NEXT FROM cur into @DbName
WHILE(@@FETCH_STATUS=0 )
BEGIN
print 'shrinking ' + @DbName + ' ...'
-------02.04 only shink log file
exec (
'
declare @dn varchar(200);
declare @ln varchar(210);
select @dn = name from '+@DbName+' .dbo.sysfiles where fileid=1
select @ln = name from '+@DbName+' .dbo.sysfiles where fileid=2
use ['+ @DbName + '] backup log ['+ @DbName +' ] with no_log dbcc shrinkfile (@ln)
'
)
-------02.04 shink db file & log file
-- dbcc SHRINKDATABASE (@DbName)
print @DbName + ' done'
FETCH NEXT FROM cur INTO @DbName
END
-----02.05 colse cursor
CLOSE cur
DEALLOCATE cur
-----03.drop temp table
DROP TABLE #DN
-------------------------------
SELECT DBNAME = DB_NAME(s_mf.database_id) INTO #DN
FROM
sys.master_files s_mf
WHERE
s_mf.state = 0 and -- ONLINE
HAS_DBACCESS(DB_NAME(s_mf.database_id)) = 1 -- Only look at databases to which we have access
GROUP BY s_mf.database_id
ORDER BY 1
--02.use cursor to fetch db name
-- ---02.01 declare cursor
DECLARE cur CURSOR FOR
SELECT DBNAME FROM #DN
-----02.02 open cursor & declare var
OPEN cur
DECLARE @DbName VARCHAR(200 )
-----02.03 loop db bane
FETCH NEXT FROM cur into @DbName
WHILE(@@FETCH_STATUS=0 )
BEGIN
print 'shrinking ' + @DbName + ' ...'
-------02.04 only shink log file
exec (
'
declare @dn varchar(200);
declare @ln varchar(210);
select @dn = name from '+@DbName+' .dbo.sysfiles where fileid=1
select @ln = name from '+@DbName+' .dbo.sysfiles where fileid=2
use ['+ @DbName + '] backup log ['+ @DbName +' ] with no_log dbcc shrinkfile (@ln)
'
)
-------02.04 shink db file & log file
-- dbcc SHRINKDATABASE (@DbName)
print @DbName + ' done'
FETCH NEXT FROM cur INTO @DbName
END
-----02.05 colse cursor
CLOSE cur
DEALLOCATE cur
-----03.drop temp table
DROP TABLE #DN
-------------------------------
公告
本文转自
我的程序人生博客园博客,原文链接:
http://www.cnblogs.com/wenjl520/archive/2009/01/05/1368657.html,如需转载请自行联系原作者
【推荐】腾讯云新用户域名抢购1元起,抓紧抢购
· 精准率首次超过人类!阿里巴巴机器阅读理解打破世界纪录
· 技术帖:每天被今日头条推送文章 背后的算法技术是什么?
· 支付宝实体版老黄历问世:全球限量1000册
· 趣店被蚂蚁金服送上纽交所,现在是时候该独立了
· 蚂蚁宝卡升级:支付宝/微博即将免流
» 更多新闻...
· 以操作系统的角度述说线程与进程
· 软件测试转型之路
· 门内门外看招聘
· 大道至简,职场上做人做事做管理