Truncating MSSQL Transaction Log

Every wanted to truncate the transaction log of a MS SQL database? We do that all the time when moving a database from one server to the other. Remember that the transaction log can take up a LOT of space in the backup file which you don’t need when you are simply moving a database to a new server (and when you don’t have the option to simply DTS).

Open up Query Analyser and run the following, substituting real database, file and log names.

I’m using these names in the example:
Database name: [myDatabase]
Data file: [myDatabase_Data]
Log file: [myDatabase_Log]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Use Master
go

Backup Log [myDatabase] with truncate_only
go

Use [myDatabase]
go

DBCC shrinkfile ([myDatabase_Log], 2)
go

-- data
Use Master
go

Backup Log [myDatabase] with truncate_only
go

Use [myDatabase]
go

DBCC shrinkfile ([myDatabase_Data], 1)
go

-- Get fileid
use [myDatabase]
go

select * from sysfiles

Comments