Technical Article

Shrink databases

,

This procedure is used to shrink the databases. Many times we use shrink option to shrink the databases for backup. and we do it N number of times, when we take the back of databses or make free space of drive.

instructions:- Follwo the step.

1. run script.
2. And execute the statement, exec shrink_databases


I m new, if you found any better option or script then please tell me. I always need ur guidance and help.

thx





if exists(select 1 from sysobjects where id=object_id('shrink_databases') and xtype='p')
drop procedure shrink_databases
GO


/*      
Purouse:- This procedure is used to shrink the databases.
*/      

CREATE procedure shrink_databases       
as      
      
set nocount on      
      
declare @dbname varchar(50)      
declare @dbid int       
declare @sqlstr varchar(50)      
      
set @dbid=1      
      
create table #tempdb      
(       
 dbid int identity (1,1),       
 dbname varchar(100) not null,      
)      
      
insert into #tempdb (dbname)      
select [name] from [master].[dbo].sysdatabases      
      
--drop table [tempdb].[dbo].#tempdb      
--select * from [tempdb].[dbo].#tempdb      
--select max(dbid) from #tempdb      
      
print 'List of databses, that are shrinked -->'      
  
while (select max(dbid) from #tempdb)>=@dbid      
begin      
      
      
 select @dbname=dbname from #tempdb where dbid = @dbid      
 set @sqlstr=' dbcc shrinkdatabase ( ''' +@dbname + ''' )'      
 print @sqlstr      
 execute (@sqlstr)      
 set @dbid=@dbid+1      
end      
      
      
      
drop table #tempdb      
      
set nocount off      
      
      
      
      
      
-- exec shrink_databases

Rate

3 (1)

You rated this post out of 5. Change rating

Share

Share

Rate

3 (1)

You rated this post out of 5. Change rating