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
Related
We have some scripts that are executed during the installation of our application.
One of them is used for setting Database Collation, the other one is used for setting compatibility level. We use the following construction for these scripts:
ALTER DATABASE [DB_NAME] SET SINGLE_USER WITH ROLLBACK IMMEDIATE
GO
ALTER DATABASE [DB_NAME] SET COMPATIBILITY_LEVEL = DB_COMPATIBILITY_LEVEL
-- ALTER DATABASE [DB_NAME] COLLATE 'Collation Name'
GO
ALTER DATABASE [DB_NAME] SET MULTI_USER
GO
The question is:
Is it possible for some background process to get an access after setting single user mode? In this case our script couldn't be executed. I saw this issue and it seems similar, so I decided to ask a question.
https://social.msdn.microsoft.com/Forums/sqlserver/en-US/950c9b85-67f9-4272-8dff-14aa3590bc8a/single-user-mode-session-lost-after-backgound-processes-jump-in?forum=sqldatabaseengine
This issue was related to restoring database.
alter database [test-db] set single_user with rollback immediate; --This sql is run using test-db
use master;restore database [test-db] from database_snapshot = 'snapshot_test-db';
alter database [test-db] set multi_user;
But I don't fully understand the difference between restoring and altering the database in the single user mode.
In the first case will be enough to set the database offline.
Any help will be appreciated.
Thanks in advance.
I would like to somehow protect databases on my SQL Server from being deleted without entering a password, even by someone with administrative access. There are times where a database has been deleted accidentally (for example, when two databases have similar names) and I'd like to prevent this from being an easy mistake to make.
I'm also open to any suggestions or alternative ideas on how to handle this. Thank you!
Create a Server Level Trigger that Rolls back any attempt to delete a database.
The Trigger will need to be disabled then re-enabled to perform any legitimate deletions.
USE [master]
GO
CREATE TRIGGER [Trig_Prevent_Drop_Database] ON ALL SERVER
FOR DROP_DATABASE
AS
RAISERROR('Dropping of databases has been disabled on this server.', 16,1);
ROLLBACK;
GO
DISABLE TRIGGER [Trig_Prevent_Drop_Database] ON ALL SERVER
GO
Or as a process:
Create a single-column, one row table in Master that will hold a database name.
Insert the name of the database in the Table.
Add an If statement to the trigger to check if the Database being dropped is identical to the Database in the table created in step 1. Otherwise Roll-back.
In this case you wouldn't need to disable the Trigger. But you're creating 2 points in the process where you define the database name.
Capturing the Database Name in a Server Level Trigger should be possible with:
SELECT CAST(eventdata().query('/EVENT_INSTANCE/DatabaseName[1]/text()') as NVarchar(128))
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
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/
I'm not sure how to even ask this question. We are running SQL Server 2008 R2. I'm not the admin, but a programmer. I need to write an application that updates some database stuff at night. I'm going to set a flag to disable logins to the database, but I want to make a particular database unavailable to anyone except me, even if someone is already logged in to the database. My program will run nightly, as a batch file, presumably with admin privileges.
I'm expecting to produce something like a script of SQL commands. I could take the database offline, except I need to make modifications to it myself. Not sure the best way to handle this.
You can basically just set the database to "single-user" mode and use it exclusively - this T-SQL will do this:
USE master;
GO
ALTER DATABASE AdventureWorks2012
SET SINGLE_USER
WITH ROLLBACK IMMEDIATE;
GO
(of course - replace AdventureWorks2012 with your own database name!)
This will make the database "single-user", e.g. only you have access to it, and it will boot off any users that's currently online, and will rollback all open transactions.
Read more about single user mode on MSDN!
This example sets the database to SINGLE_USER mode to obtain exclusive access. The termination option WITH ROLLBACK IMMEDIATE is specified in the first ALTER DATABASE statement. This will cause all incomplete transactions to be rolled back and any other connections to the AdventureWorks2012 database to be immediately disconnected.
Since you are using an administrative account to perform the updates, I'll assume the account is in one of these roles: db_owner, dbcreator, sysadmin. Use the ALTER DATABASE SET ... syntax to control database access during the DML operations.
The assumption is that database users you want to lock out aren't in the above mentioned roles.
USE master;
-- only allow members of db_owner, dbcreator, or sysadmin roles to access
-- database, allowing current transactions time to complete. if you want to
-- drop access immediately, add WITH ROLLBACK IMMEDIATE
ALTER DATABASE SET RESTRICTED_USER;
-- data load
-- return database to normal operating state
ALTER DATABASE SET MULTI_USER;