Friday, July 23, 2021

How do you set database ONLINE and OFFLINE in sql server?

SQL Server database can be made OFFLINE by running the below SQL statement. 

If there are any critical or important transactions running then it's recommended to let them complete first and then run the below queries down.


USE [master];

GO

--Disconnect all existing sessions.

ALTER DATABASE SampleDB SET SINGLE_USER WITH ROLLBACK IMMEDIATE

GO

--Change database into OFFLINE mode.

ALTER DATABASE SampleDB SET OFFLINE


Now your database has moved to offline mode.


=>If incase set single user doesn't work, use the below deadlock priority high command


SET DEADLOCK_PRIORITY HIGH

GO

ALTER DATABASE SampleDB SET MULTI_USER WITH ROLLBACK IMMEDIATE

GO


How do we bring back our database online again?

Once we are done with updating the physical file names, the next step is to bring the database ONLINE.

Run the below SQL statement to bring the target database ONLINE. 


ALTER DATABASE SampleDB SET ONLINE

Go

ALTER DATABASE SampleDB SET MULTI_USER

Go 


No comments:

Post a Comment

Top 15 SQL interview questions for 2-5 years experienced

  1. What is the difference between UNION and UNION ALL? UNION: To select related information from two tables UNION command is used. It is s...