Skip to main content

[SQL]DB user remove (kick out)

 

DB user remove (kick out)👎

Here is the Query.  Change your dbname to use. 


USE master; -- get out of dbname myself

GO

-- kick all other users out:

ALTER DATABASE [dbname] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;

GO


ALTER DATABASE [dbname] SET OFFLINE; -- option 1 prevent sessions from re-establishing connection:


--After finishing DB kick out mode

ALTER DATABASE [dbname] SET ONLINE; -- when you used option 1 use this to return online from offline

ALTER DATABASE [dbname] SET MULTI_USER; --return multi user from single user



Here is another method to kill the user's process ID.

   ALTER LOGIN report DISABLE


--Kill existing sessions

DECLARE @kill varchar(8000) = '';

SELECT @kill = @kill + 'kill ' + CONVERT(varchar(5), spid) + ';'

FROM master..sysprocesses 

WHERE dbid = db_id('dbname') AND spid <> @@spid --dbname need to update

EXEC(@kill);

ALTER LOGIN report ENABLE


You can use both methods to prevent this. See the below Query. 


Declare @Database varchar(50) = 'dbname' --dbname need to update

    ALTER LOGIN report DISABLE


--Kill existing sessions

DECLARE @kill varchar(8000) = '';

SELECT @kill = @kill + 'kill ' + CONVERT(varchar(5), spid) + ';'

FROM master..sysprocesses 

WHERE dbid = db_id(@Database) AND spid <> @@spid --dbname need to update

EXEC(@kill);

        

        --set DB for SINGLE_USER

IF (SELECT user_access_desc FROM sys.databases WHERE name = @Database) = 'MULTI_USER'

ALTER DATABASE @Database SET SINGLE_USER WITH ROLLBACK IMMEDIATE

        --after finishing all kick out process

IF (SELECT user_access_desc FROM sys.databases WHERE name = @Database) = 'SINGLE_USER' ALTER DATABASE @Database SET multi_user ALTER LOGIN report ENABLE





Comments

Popular posts from this blog

[Cisco] command and CDR log backup by FTP (gw-accounting file)

Below command has been tested with ISR4331 devices.  1. check Cisco device file size command :  a. show current folder dir flash:  b. show all folder and files dir flash 2. check file only start with "cdr." dir flash:cdr.* 3. Remove file (Add option [ /recursive /force]  when you delete file is not recovering free space) delete /recursive /force  flash:cdr.* 4. check subfolder file dir flash:\tracelogs 5. example for log back to ftp https://www.cisco.com/c/en/us/td/docs/ios/voice/cdr/developer/manual/cdrdev/cdrcsv.html#marker-1163166 enable conf t gw-accounting file primary ftp 192.168.1.51/cisco/cdr username cisco password xxxxx secondary sftp 192.168.1.52/cisco/cdr username cisco password xxxxx acct-template callhistory-detail maximum buffer-size  30 maximum retry-count 3 (note : if these setting is not working. check these area, FTP password can not be used @ and some of special characters for this command. Second, ftp location is case sensitive. Please...

[SharePoint] Versioning Settings menu location for sharepoint 2013

  https://support.microsoft.com/en-gb/office/enable-and-configure-versioning-for-a-list-or-library-1555d642-23ee-446a-990a-bcab618c7a37#OfficeVersion=2016,_2013 1. From top menu Select "PAGE" 2. Select "Library Settings" 3. Select "Versioning settings" 4. Versioning Settings are listed. 

[Cisco Network] connect by serial and usb & setup IP

Accessing the CLI via PuTTY with a Console Connection Step 1. Connect the switch to the computer using a standard 9-pin serial cable. The Cisco DB9 to RJ45 Console Cable also supports console connections, but only if the switch has an RJ45 Console port. An RJ45 Console port resembles an Ethernet port and is labeled CONSOLE on the back of the switch. Newer laptops don’t have Serial ports on them, so in this case you have to use a USB to Serial adapter. When plugging that into a computer it assigns a COM port number to it that is not COM1. If this is the case for you, you need to know where to look to find the correct COM port number when setting up the connection with PuTTY. Right-click on the Windows logo/Start menu and click on Device Manager to open it. In the Device Manager, you would look to see what COM port is given to the USB adapter. In this case you would need to use COM4 for the Serial line to make the connection.     Step 2. Open the PuTTY application. The  PuT...