Attaching database without transaction log file

  • Hi,

       I accidentally deleted the Transaction log file(Shift delete). Now I am not able to attach the database without this log file. Is there any way by which this is possible ??

      

  • Hi,

    There shouldn't be too much of a problem, just go ahead and re-attach, SQL will see that the transaction log file is missing and should recreate it for you.

  • Hi Mike,

           I tried doing it but it throws up an error and it does not create a new Log file for me. Can U try it in ur machine and update me? Thanks in Advance....

  • Hi there,

    You can have a look in books online for the correct syntax, or failing that here's something that should do it for you using SQLDMO inside a script file. 

    Basically create an empty text file and copy everything between the lines (but not including the lines).  Obviously you'll need to amend the name of the DB and the location of the file etc (the bits in blue) but that's not exactly rocket science.

    Rename the file to attach.vbs and run it

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

    on error resume next

    dim oSQL

    dim oDB

    dim strDBName

    strDBName = "YourDB"

    strLogin = "sa"

    strPassword = "yourpassword"

    strMDFFile = "c:\datafile\YourDB_Data.mdf"

    set oSQL = createobject("SQLDMO.SQLServer")

    oSQL.Connect "(local)", strLogin , strPassword

    set oDB = oSQL.Databases.Item(strDBName )

    if isobject(oDB) then

     msgbox "database already exists"

     

    else

     oSQL.AttachDB strDBName, strMDFFile

     msgbox "done"

    end if

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

  • You can use the stored proc

    sp_attachsinglefiledb

    Copied from books online...

    Send feedback about this pageKeyboard shortcutsSee AlsoTransact-SQL Reference

    sp_attach_single_file_db

    Attaches a database having only one data file to the current server.

    Syntax

    sp_attach_single_file_db [ @dbname = ] 'dbname'

        , [ @physname = ] 'physical_name'

    Arguments

    [@dbname =] 'dbname'

    Is the name of the database to be attached to the server. dbname is sysname, with a default of NULL.

    [@physname =] 'phsyical_name'

    Is the physical name, including path, of the database file. physical_name is nvarchar(260), with a default of NULL.

    Return Code Values

    0 (success) or 1 (failure)

    Result Sets

    None

    Remarks

    When sp_attach_single_file_db attaches the database to the server, it builds a new log file and performs additional cleanup work to remove replication from the newly attached database.

    Use sp_attach_single_file_db only on databases that were previously detached from the server using an explicit sp_detach_db operation.

    Use sp_attach_single_file_db only on databases that have a single log file. Do not use this stored procedure on databases that have multiple log files.

    Permissions

    Only members of the sysadmin and dbcreator fixed server roles can execute this procedure.

    Examples

    This example detaches pubs and then attaches one file from pubs to the current server.

    EXEC sp_detach_db @dbname = 'pubs'EXEC sp_attach_single_file_db @dbname = 'pubs',    @physname = 'c:\Program Files\Microsoft SQL Server\MSSQL\Data\pubs.mdf'

    See Also

    sp_attach_db

    sp_detach_db

    sp_helpfile

    System Stored Procedures

    ©1988-2004 Microsoft Corporation. All Rights Reserved.

  • Thank you for the attach_db explanation!:D It really worked!

Viewing 6 posts - 1 through 5 (of 5 total)

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