Tuesday, October 11, 2016

how to restore MDF file

Resotring MDF file alone need trick.
1. to copy the mdf file login into DB server.
2. Stop the SQL serivice by clicking on servername.
3. copy and paste MDF file on other drive.
4. start the serive in SQL Jobs (Client)

Login in to server where you want to store MDF file alone.
1. Created the database with same name. (for example I named it TestLDFAttach)
USE [master]
GO
ALTER DATABASE [TestLDFAttach] SET EMERGENCY
GO
ALTER DATABASE [TestLDFAttach] SET SINGLE_USER
GO

2. Now stop the SQL Service.

3. Copy and paste MDF in  in SQL server Data folder locaiton
4.Replace the MDF file.

5. Restart the SQL Service.

6. run the follwing comamnd
 USE [master] 

DBCC CHECKDB ([TestLDFAttach], REPAIR_ALLOW_DATA_LOSS) 
GO 
ALTER DATABASE [TestLDFAttach] SET MULTI_USER 
GO 
ALTER DATABASE [TestLDFAttach] SET ONLINE 
GO 

7. compare the database. it iwll same even we recoved with data loss.

Thursday, October 6, 2016



Get the deadlock Get the deadlock details




SELECT XEvent.query('(event/data/value/deadlock)[1]') AS DeadlockGraph
FROM ( SELECT XEvent.query('.') AS XEvent
       FROM ( SELECT CAST(target_data AS XML) AS TargetData
              FROM sys.dm_xe_session_targets st
                   JOIN sys.dm_xe_sessions s
                   ON s.address = st.event_session_address
              WHERE s.name = 'system_health'
                    AND st.target_name = 'ring_buffer'
              ) AS Data
              CROSS APPLY
                 TargetData.nodes
                    ('RingBufferTarget/event[@name="xml_deadlock_report"]')
              AS XEventData ( XEvent )
      ) AS src;