简体   繁体   中英

SQL Server Log out / Terminate session of logged in user in a query

Without going into why I would like to do this, is it possible (I'll be using a login trigger) to log out a user that has no write permissions to a certain database?

I am able to find the currently logged in users permission, I just need to know if it's possible to log them out?

DECLARE @HasPermission bit

SELECT @HasPermission =  HAS_PERMS_BY_NAME('RTEST2.dbo.TestTableSize', 'OBJECT', 'INSERT');

IF @HasPermission = 0
   SELECT 'Now this is where id want to log out the user'

One can prevent a user from logging in by executing a ROLLBACK from within a login trigger . As @DavidBrowneMicrosoft mentioned in his comment, it's also a good practice to use a PRINT or RAISERROR statement so that reason for the login failure is logged. This message will not be returned to the client but may be useful for troubleshooting.

IF @HasPermission = 0
BEGIN
    PRINT 'User does not have permissions to login';
    ROLLBACK;
END;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM