The compressed database log file used by the cursor

Source: Internet
Author: User

declare @databasename nvarchar (100)
--Define cursors and assign values to get all the online database Name
Declare getdatabasecursor cursor for select name from master.sys.databases where state_desc= ' online '
--You must open the cursor before using the cursor
Open Getdatabasecursor
--Using the FETCTH statement to get the data, a fetch statement can put a record in the specified variable at one time to achieve shrink per db effect
FETCH NEXT from Getdatabasecursor to @databasename
While @ @FETCH_STATUS = 0
Begin
declare @RecoveryModel nvarchar (20)
declare @sql nvarchar (100)
DECLARE @logfileid int
Select @RecoveryModel = Cast (DATABASEPROPERTYEX (name, ' RECOVERY ') as varchar) from master: sysdatabases where name [email protected]
IF @RecoveryModel <> ' simple '
Begin
EXEC (' ALTER DATABASE ' [email protected] + ' SET RECOVERY simple ')
End
declare @dsql nvarchar (200)
Set @dsql =n ' Select @logfileid = file_id from ' + @databasename + N '. sys.database_files where type_desc = ' LOG '
EXEC sp_executesql @dsql, N ' @logfileid int output ', @logfileid output
EXEC (' use ' [email protected]+ ' DBCC shrinkfile (' [email protected]+ ') ')
FETCH NEXT from Getdatabasecursor to @databasename
End
--At the end of the cursor operation, do not forget to close the cursor so that the system frees the resources that the cursor occupies
CLOSE Getdatabasecursor
--Delete cursor
Deallocate getdatabasecursor

The compressed database log file used by the cursor

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.