Do the following to create a certificate-based login with the rights necessary to view and kill SPIDs:
-- =============================================
-- Create AuthorizedKiller Login and Certificate
-- =============================================
-- This script creates the secure certificate and associated login.
-- <Password goes here> should be replaced with the an appropriate password.
-- This script should be run once only.
-- Create the certificate in the database and back it up to a file.
USE [DATABASE]
GO
CREATE CERTIFICATE [AuthorizedKillerCertificate]
ENCRYPTION BY PASSWORD = '<Password goes here>'
WITH SUBJECT = 'Certificate for signing Stored Procedure',
EXPIRY_DATE = '12/1/2099';
GO
-- This will not overwrite the file. If it exists, you must log on to the server and delete it first.
BACKUP CERTIFICATE [AuthorizedKillerCertificate]
TO FILE = 'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\Backup\AuthorizedKillerCertificate.CER';
GO
-- Copy the certificate to the master database and create a login associated with it.
USE [master]
GO
CREATE CERTIFICATE [AuthorizedKillerCertificate]
FROM FILE = 'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\Backup\AuthorizedKillerCertificate.CER';
GO
CREATE LOGIN [AuthorizedKiller]
FROM CERTIFICATE [AuthorizedKillerCertificate];
Go
GRANT AUTHENTICATE SERVER TO [AuthorizedKiller];
GO
-- Grant the roles necessary to view and kill tasks.
GRANT VIEW SERVER STATE TO [AuthorizedKiller]
GO
GRANT ALTER ANY connection TO [AuthorizedKiller]
GO
-- Create a user associated with the login in the database.
USE [DATABASE]
GO
CREATE USER [AuthorizedKiller] FROM LOGIN [AuthorizedKiller]
GO
Create a stored procedure:
CREATE PROCEDURE [dbo].[RestrictedKillProcedure]
-- Add arguments here.
BEGIN
-- Add restricted kill code here.
END
After creating the stored procedure with appropriate code to restrict kills, sign it and grant permission to a role to execute it:
ADD SIGNATURE TO OBJECT::[RestrictedKillProcedure]
BY CERTIFICATE [AuthorizedKillerCertificate]
WITH PASSWORD = '<Password goes here>';
GO
GRANT EXECUTE ON [RestrictedKillProcedure] TO [AuthorizedKillerRole]
Finally, add the user to the role.
sys.dm_exec_requeststo identify thesession_id(s) to kill, say by hostname or login name, but I don't know of any way to make the permissions granular enough to only identify your own sessions that way. That seems like very unfriendly behavior by SAS (sorry, I have no idea what that acronym means). – Aaron Bertrand Jan 14 '13 at 15:26