What is the command to truncate a SQL Server log file?

后端 未结 7 1892
青春惊慌失措
青春惊慌失措 2020-12-07 06:57

I need to empty an LDF file before sending to a colleague. How do I force SQL Server to truncate the log?

相关标签:
7条回答
  • 2020-12-07 07:24

    backup log logname with truncate_only followed by a dbcc shrinkfile command

    0 讨论(0)
  • 2020-12-07 07:27

    Another option altogether is to detach the database via Management Studio. Then simply delete the log file, or rename it and delete later.

    Back in Management Studio attach the database again. In the attach window remove the log file from list of files.

    The DB attaches and creates a new empty log file. After you check everything is all right, you can delete the renamed log file.

    You probably ought not use this for production databases.

    0 讨论(0)
  • 2020-12-07 07:31

    For SQL 2008 you can backup log to nul device:

    BACKUP LOG [databaseName]
    TO DISK = 'nul:' WITH STATS = 10
    

    And then use DBCC SHRINKFILE to truncate the log file.

    0 讨论(0)
  • 2020-12-07 07:34

    For SQL Server 2008, the command is:

    ALTER DATABASE ExampleDB SET RECOVERY SIMPLE
    DBCC SHRINKFILE('ExampleDB_log', 0, TRUNCATEONLY)
    ALTER DATABASE ExampleDB SET RECOVERY FULL
    

    This reduced my 14GB log file down to 1MB.

    0 讨论(0)
  • 2020-12-07 07:35

    Since the answer for me was buried in the comments. For SQL Server 2012 and beyond, you can use the following:

    BACKUP LOG Database TO DISK='NUL:'
    DBCC SHRINKFILE (Database_Log, 1)
    
    0 讨论(0)
  • 2020-12-07 07:39

    if I remember well... in query analyzer or equivalent:

    BACKUP LOG  databasename  WITH TRUNCATE_ONLY
    
    DBCC SHRINKFILE (  databasename_Log, 1)
    
    0 讨论(0)
提交回复
热议问题