SQL statement implementation SQL Server 2000 and SQL Server 2005 log shrink (bulk) _mssql

Source: Internet
Author: User
Tags rtrim

Copy Code code as follows:

DECLARE @name VARCHAR (25)
DECLARE @SQL VARCHAR (1000)
DECLARE @logid INT

DECLARE sysdatabase_name CURSOR for SELECT name from master.dbo.sysdatabases

OPEN Sysdatabase_name


FETCH NEXT from Sysdatabase_name into @name

While @ @FETCH_STATUS = 0
BEGIN

IF (@name not in (' xxx ')-database name that does not require log shrinkage
BEGIN
SET @SQL = ' DECLARE @logid INT
Use ' + @name + '
SELECT @logid = Fileid from Sysfiles WHERE right (RTrim (filename), 3) = ' ldf '

BACKUP LOG ' + @name + ' with no_log
DBCC shrinkfile (@logid) '

EXEC (@SQL)

End

FETCH NEXT from Sysdatabase_name into @name

End

Close Sysdatabase_name
Deallocate Sysdatabase_name

SQL statement Implementation SQL Server 2005 log shrink (bulk)
Copy Code code as follows:

-->title: Generating test data
-->author:wufeng4552
-->date:2009-09-15 08:56:03
declare @dbname nvarchar (20)--Data library name
declare @sql nvarchar (max)
Declare sysdbname cursor for select name from master.dbo.sysdatabases
Open Sysdbname
FETCH NEXT from Sysdbname into @dbname
While @ @fetch_status =0
Begin
if (@dbname not in (' xxx ')-database name that does not require log shrinkage
Begin
Set @sql =--Journal file ID
N ' Declare @logid int ' +
N ' use ' + @dbname +
N ' select @logid =fileid from Sysfiles where right (LTrim (RTrim (filename), 3) = ' ldf ' +
--Truncate transaction log
N ' backup log ' + @dbname + ' with no_log ' +
--Shrinking the specified data file
N ' DBCC SHRINKFILE (@logid) '
EXEC (@sql)
End
FETCH NEXT from Sysdbname into @dbname
End
Close Sysdbname
Deallocate Sysdbname

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.