Domanda

I have a user who has db_datareader, db_datawriter permissions on a DB. I want to set the isolation levels to the DB to which the user has access to. What permissions will my user need to be able to set these. DB used: SQL SERVER 2008

È stato utile?

Soluzione

This is not setting an isolation level:

ALTER DATABASE dbname SET ALLOW_SNAPSHOT_ISOLATION ON;

That is altering the database. For that you need to provide them with ALTER rights on the database:

GRANT ALTER ON DATABASE::dbname TO username;

Otherwise you get this error:

Msg 5011, Level 14, State 9, Line 1
User does not have permission to alter database 'dbname', the database does not exist, or the database is not in a state that allows access checks.
Msg 5069, Level 16, State 1, Line 1
ALTER DATABASE statement failed.

Now, ALTER is all or nothing - you can't use it to allow them to change the allow snapshot setting but not other settings like forced parameterization, compatibility level, etc. You can do this much more granularly, though; perhaps you could create a stored procedure that does something like this:

CREATE PROCEDURE dbo.SetIsolationLevel
WITH EXECUTE AS OWNER
AS
BEGIN
  SET NOCOUNT ON;

  DECLARE @sql NVARCHAR(MAX) = N'ALTER DATABASE ' 
      + QUOTENAME(DB_NAME())
      + ' SET ALLOW_SNAPSHOT_ISOLATION ON;';

  EXEC sp_executesql @sql;
END
GO

Now you just have to give the user EXEC permissions on that procedure, which your user can now call (instead of the ALTER DATABASE command explicitly and instead of giving them full ALTER DATABASE privileges):

GRANT EXEC ON dbo.SetIsolationLevel TO username;
GO

You can simulate them calling this stored procedure by logging in as them, or using the EXECUTE AS feature directly:

EXECUTE AS USER = 'username';
GO
EXEC dbo.SetIsolationLevel;
GO
REVERT;
GO

Another idea is to simply set the model database to have this setting, than any new databases that get created for your users will automatically inherit it, then you don't have to worry about making them turn it on.

ALTER DATABASE model SET ALLOW_SNAPSHOT_ISOLATION ON;
GO
CREATE DATABASE splunge;
GO
SELECT snapshot_isolation_state_desc FROM sys.databases WHERE name = N'splunge';

Result:

ON
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top