My program failed by this exception:
System.Data.SqlClient.SqlException:
The transaction log for database 'MyDB' is full.
To find out why space in the log cannot be reused, see the log_reuse_wait_desc column in sys.databases
I noticed that my tables doesn't load in SQL Management Studio and i cann't open database properties window
Then I try to change my log file to autogrowth by this statement:
ALTER DATABASE MyDB
MODIFY FILE
(NAME=MyDB_Log,MAXSIZE=2TB,FILEGROWTH=20MB);
this statement executed successfully but doesn't help me to recover my database
Then i try to set offline MyDB the operation failed by some exception
Then i set the database in single user mode but the exception still exists
Then i try this statement:
ALTER DATABASE MyDB SET EMERGENCY;
GO
ALTER DATABASE MyDB set single_user
GO
DBCC CHECKDB (MyDB, REPAIR_ALLOW_DATA_LOSS) WITH ALL_ERRORMSGS;
GO
ALTER DATABASE MyDB set multi_user
GO
This statement failed too on first line.
And now i don't know what should I do.
Follow the following steps.
Open up SQL Management Studio and connect to your database server
Right-click your database
Click Properties
Click the Options link
Set the Recovery Model to Simple as follows
Click OK
Once this is complete, right click on the database again
Click Tasks>Shrink>Files
On the Shrink Database window select the file type as 'Log' . The file name appears in the filename drop down as databasename_log as follows:
The space used versus the space allocated displays. After you set the recovery model to Simple, the majority of the space in the transaction log released.
Ensure that the Release unused space radio button is selected.
Click OK on this window to shrink the transaction log.
You might also want to read through this short post
http://sqlity.net/en/556/t-sql-tuesday-25-%E2%80%93-sql-server-tips-tricks/
Related
I implemented log shipping using dbatools command and was testing it out today. What ive noticed is when i query the restored/standby database, like a select on a table, the restore of transaction log fails until i disconnect from the instance.
Message
2021-09-24 23:00:23.60 *** Error: Could not apply log backup file '\\devops\c$\Users\devops_user\Documents\DBLogShipping\Backups\LogShippingFiles\test12345\test12345_20210924225000.trn' to secondary database 'test12345'.(Microsoft.SqlServer.Management.LogShipping) ***
2021-09-24 23:00:23.60 *** Error: Exclusive access could not be obtained because the database is in use.
RESTORE LOG is terminating abnormally.(.Net SqlClient Data Provider) ***
I found out that by setting the database to SINGLE_USER mode:
USE master
GO
ALTER DATABASE test12345
SET SINGLE_USER
--This rolls back all uncommitted transactions in the db.
WITH ROLLBACK IMMEDIATE
GO
the restore job will actually work fine whilst querying the database, so no need to disconnect from the server. I mean that is great! However, when i disconnected and reconnected later to the instance, I saw the SINGLE_USER mode disappeared...it looks like it reverted back to the default multi_user mode?
Is there a way to permanently have the database set in SINGLE USER mode?
I am trying to rename a database through SSMS and getting this below error:-
I am not realizing from the additional information
You need to perform such statements as below.
USE master;
GO
ALTER DATABASE MyTestDatabase SET SINGLE_USER WITH ROLLBACK IMMEDIATE
GO
ALTER DATABASE MyTestDatabase MODIFY NAME = MyTestDatabaseCopy ;
GO
ALTER DATABASE MyTestDatabaseCopy SET MULTI_USER
GO
It can be done in SSMS.
1.right click your database name.
2. properties
3. Options.
5. Restrict Access Select single.
then repeat the step to change again into MULTI_USER After renaming has been done.
You need to close all existing connections before to do that..
For this you can
use SP_WHO and Kill all open connections to your database
OR
Take database offline: Right Click -> Tasks -> Take Offline -> Check Drop All Active Connections
I have a program need to create and drop a database multiple time, but sometime when dropping a database I get a exception here:
ALTER DATABASE failed because a lock could not be placed on database ...
And the command is like this:
USE master;
IF EXISTS(select * from sys.databases where name='{0}')
BEGIN
ALTER DATABASE [{0}] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
DROP DATABASE [{0}]
END
Why it happens?
What is the better way to do this (drop database)?
You can't DROP a database if any one else is connected to it. Simply running DROP DATABASE MyDatabase; doesn't close those connections, thus the DROP fails.
Changing the database to SINGLE USER will drop any existing connections (WITH ROLLBACK IMMEDIATE causes any transactions to be rolled back immediately, which is a problem here, as your about the DROP the database). Then, because it's instantly the next statement, the database is dropped before anyone gets the opportunity to reconnect.
I have created a sample database earlier and now I want to delete that database, but it is not getting deleted. I searched online but I didn't find any solution which is working.
Using T-SQL, I tried:
USE [Sample]
ALTER DATABASE [Sample]
SET SINGLE_USER
WITH ROLLBACK IMMEDIATE;
GO
DROP DATABASE [Sample]
Using the GUI, I am getting this below error:
I closed existing connection then also this is happening and this is my local machine. Please help me here!
use this code:
USE MASTER
GO
ALTER DATABASE Sample
SET multi_user WITH ROLLBACK IMMEDIATE
GO
ALTER DATABASE Sample
SET SINGLE_USER WITH ROLLBACK IMMEDIATE
GO
DROP DATABASE Sample
GO
Close your SSMS , open a new instance and then try the following, Copy paste the whole thing into a new query window and execute at once:
USE [master];
GO
ALTER DATABASE [Sample] --<-- go back into Multi-user mode
SET MULTI_USER;
GO
ALTER DATABASE [Sample] --<-- Will disconnect everyone 1st
SET SINGLE_USER -- and will leave the database in single user mode
WITH ROLLBACK IMMEDIATE;
GO
USE [Sample]; -- quickly grab that single connection before any
GO -- other process does
USE [master]; -- Connect to master db, connection
GO -- This also means disconnect Sample DB
DROP DATABASE [Sample] -- At this point there should be no active connections
GO -- to this database and can be dropped
Another way to delete the Database (without coding)
In Microsoft SQL Server Management Studio, Right-click the Database that you want to delete (In your case, Sample Database) and then Select Properties.
Select the 'Options' Page.
Under the other option, Find the 'Restrict user' option under 'State'.
Change it value MULTI_USER to SINGLE_USER
Then Press Okay and Try again (Or right-click the database again and click Delete)
Use this Code should help.
ALTER DATABASE [dbname]
SET SINGLE_USER --or RESTRICTED_USER
WITH ROLLBACK IMMEDIATE;
GO
DROP DATABASE [dbname];
GO
Using the code below I'm unable to turn off snapshot isolation. I'm using a SQL Server 2012 box. I can create brand new empty db, turn snapshot isolation on, but I can't turn it back off.
The "allow_snapshot_isolation OFF" line just spins it's wheels.
ALTER DATABASE SNAP SET SINGLE_USER WITH ROLLBACK IMMEDIATE
ALTER DATABASE SNAP SET allow_snapshot_isolation OFF
ALTER DATABASE SNAP SET read_committed_snapshot off
ALTER DATABASE SNAP SET MULTI_USER
Are you sure that no other transactions were ran on the database? Remeber about implicit transactions which are used eg. by JDBC drivers (when you setAutocommit to false).
Snapshot isolation cannot be turn off if any of the previous transactions is pending. This is, because it has to be sure that any other transaction will not try to use previous row versions. However, it is possible to make queries spanning through more than one database so setting snapshot isolation is not only taking care of transaction on one database.
You can check if this is the case by using sp_who2 and SELECT * FROM sys.sysprocessesand searching for your altering process. sp_who2 will be showing that the process is suspended and by using sys.sysprocesses you will probably find out that its lastwaiting type is 'DISABLE_VERSIONING'.
So your solution is to rollback all transactions (or close conections in case of implicit transactions).
Be awared, that if you are using a connection pooling from some program, you may have, for example, 40 implicit transactions opened in two or three dbs. If they were opened after the snapshot mode had been turned on then turning it off will be impossible until they end.
I followed below for the same issue I was getting
USE master;
GO
ALTER DATABASE MYDB SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
GO
ALTER DATABASE MYDB MODIFY NAME = MYDB_1;
GO
ALTER DATABASE MYDB_1 SET READ_COMMITTED_SNAPSHOT OFF;
GO
ALTER DATABASE MYDB_1 MODIFY NAME = MYDB;
GO
ALTER DATABASE MYDB SET MULTI_USER;
I ran the MSSQL query below to turn off ALLOW_SNAPSHOT_ISOLATION for master database:
ALTER DATABASE master SET ALLOW_SNAPSHOT_ISOLATION OFF;
GO;
Then, I got this error below:
SNAPSHOT ISOLATION is always enabled in this database.
And, I ran the MSSQL query below to turn on READ_COMMITTED_SNAPSHOT for master database:
ALTER DATABASE master SET READ_COMMITTED_SNAPSHOT ON;
GO;
Then, I got this error below:
Option 'READ_COMMITTED_SNAPSHOT' cannot be set in database 'master'.
Because the documentation says below:
The ALLOW_SNAPSHOT_ISOLATION option is automatically set ON in the
master and msdb databases, and cannot be disabled.
Users cannot set the READ_COMMITTED_SNAPSHOT option ON in master,
tempdb, or msdb.