Steps to delete the TLOG

  • I need to drop a TLOG after I have backed it up.  It is not shrinking enough and we have a space issue.  What are the steps to do this?  Is it ok to drop the one and only tlog file?  Will a new one be created?  Thank you,

     

    David

    Best Regards,

    ~David

  • the transactionlog wil only shrink if the active block is at the beginning of the trx-log file !

    So you may have to repeat this sequence until you succeed !

    -- getest door JOBI en OK !

    ---------------------------

    -- Shrink_TrxLog.SQL

    --INF: How to Shrink the SQL Server 7.0 Transaction Log

    -- zie http://support.microsoft.com/support/kb/articles/q256/6/50.asp?id=256650&SD

    -- select db_name()

    -- select * from sysfiles

    USE     Your_Database_Name              -- This is the name of the database for which the log will be shrunk.

    go

    SET NOCOUNT ON

    DECLARE @LogicalFileName sysname,

            @MaxMinutes INT,

            @NewSize INT

    -- *** MAKE SURE TO CHANGE THE NEXT 3 LINES WITH YOUR CRITERIA. ***

    SELECT  @LogicalFileName = 'Your_log',  -- Use sp_helpfile to identify the logical file name that you want to shrink.

            @MaxMinutes = 10,               -- Limit on time allowed to wrap log.

            @NewSize = 100                  -- in MB

    -- Setup / initialize

    DECLARE @OriginalSize int

    SELECT @OriginalSize = size -- in 8K pages

      FROM sysfiles

      WHERE name = @LogicalFileName

    SELECT 'Original Size of ' + db_name() + ' LOG is ' +

            CONVERT(VARCHAR(30),@OriginalSize) + ' 8K pages or ' +

            CONVERT(VARCHAR(30),(@OriginalSize*8/1024)) + 'MB'

      FROM sysfiles

      WHERE name = @LogicalFileName

    CREATE TABLE DummyTrans

      (DummyColumn char (8000) not null)

    -- Wrap log and truncate it.

    DECLARE @Counter   INT,

            @StartTime DATETIME,

            @TruncLog  VARCHAR(255)

    SELECT  @StartTime = GETDATE(),

            @TruncLog = 'BACKUP LOG ' + db_name() + ' WITH TRUNCATE_ONLY'

    -- Try an initial shrink.

    DBCC SHRINKFILE (@LogicalFileName, @NewSize)

    EXEC (@TruncLog)

    -- Wrap the log if necessary.

    WHILE     @MaxMinutes > DATEDIFF (mi, @StartTime, GETDATE()) -- time has not expired

          AND @OriginalSize = (SELECT size FROM sysfiles WHERE name = @LogicalFileName)  -- the log has not shrunk   

          AND (@OriginalSize * 8 /1024) > @NewSize  -- The value passed in for new size is smaller than the current size.

      BEGIN -- Outer loop.

        SELECT @Counter = 0

        WHILE  ((@Counter < @OriginalSize / 16) AND (@Counter < 50000))

          BEGIN -- update

            INSERT DummyTrans VALUES ('Fill Log')  -- Because it is a char field it inserts 8000 bytes.

            DELETE DummyTrans

            SELECT @Counter = @Counter + 1

          END   -- update

        EXEC (@TruncLog)  -- See if a trunc of the log shrinks it.

      END   -- outer loop

    SELECT 'Final Size of ' + db_name() + ' LOG is ' +

            CONVERT(VARCHAR(30),size) + ' 8K pages or ' +

            CONVERT(VARCHAR(30),(size*8/1024)) + 'MB'

      FROM sysfiles

      WHERE name = @LogicalFileName

    DROP TABLE DummyTrans

    PRINT '*** Perform a full database backup ***'

    SET NOCOUNT OFF

    Johan

    Learn to play, play to learn !

    Dont drive faster than your guardian angel can fly ...
    but keeping both feet on the ground wont get you anywhere :w00t:

    - How to post Performance Problems
    - How to post data/code to get the best help[/url]

    - How to prevent a sore throat after hours of presenting ppt

    press F1 for solution, press shift+F1 for urgent solution 😀

    Need a bit of Powershell? How about this

    Who am I ? Sometimes this is me but most of the time this is me

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply