SQL SERVER database shrinking and batch shrinking

Source: Internet
Author: User

Stored procedure method

The code is as follows: Copy code

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 '))
BEGIN
SET @ SQL = 'Clare @ 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

Another method is that this may be more suitable for beginners.

1. Clear logs
Dump transaction database name WITH NO_LOG
2. Truncate transaction logs:
Backup log database name WITH NO_LOG
3. Compress database files (if not compressed, the database files will not be reduced
Enterprise Manager -- right-click the database you want to compress -- all tasks -- contract database -- contract file
-- Select Log File -- select to shrink to XXM in the contraction mode. Here, a minimum number of MB allowed to shrink is displayed. Enter

Number. OK.
-- Select data file -- select to shrink to XXM in the contraction mode. Here, a minimum number of MB allowed to shrink is displayed, and you can directly enter this

Number. OK.
You can also use SQL statements to complete
-- Shrink database
Dbcc shrinkdatabase (customer profile)
-- Contract the specified data file. 1 is the file number. You can use this statement to query: select * from sysfiles
Dbcc shrinkfile (1)
4. To minimize log files (for SQL 7.0, this step can only be performed in the query analyzer)
A. Separate the database:
Enterprise Manager -- Server -- database -- right-click -- detach database
B. Delete LOG files in my computer
C. Additional database:
Enterprise Manager -- Server -- database -- right-click -- attach database
This method generates a new LOG with a size of more than 500 K.
Or use the code:
The following example separates pubs and attaches a file in pubs to the current server.
A. Separation
E x e c sp_detach_db @ dbname = 'pubs'
B. Delete log files
C. Attach
E x e c sp_attach_single_file_db @ dbname = 'pubs ',
@ Physname = 'C: Program FilesMicrosoft SQL ServerMSSQLDatapubs. mdf'
5. In order to automatically contract in the future, make the following settings:
Enterprise Manager -- Server -- right-click database -- property -- option -- Select "auto contract"
-- SQL statement setting method:
E x e c sp_dboption 'database name', 'autowrite', 'True'
6. If you want to prevent the log from increasing too much in the future
Enterprise Manager -- Server -- right-click database -- properties -- Transaction log
-- Limit file growth to xM (x is the maximum data file size you allow)
-- SQL statement settings:
Alter database name modify file (name = logical file name, maxsize = 20)
Note:
Follow these steps. Do not follow these steps.
Otherwise, your database may be damaged.
Generally, steps 4 and 6 are not recommended.
Step 3 is not safe, and may damage the database or lose data
If the log reaches the upper limit in step 1, the subsequent database processing will fail and the log can be restored after being cleared.

In addition, we provide a simpler method, which I have tried and tried repeatedly. We recommend that you use it.
Simpler method:
1. Right-built database properties window-fault recovery model-set to simple
2. Right-built database all tasks-shrink database
3. Right-built database properties window-fault recovery model-set as large-capacity log records

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.