Sql-Server
僅為匹配前綴的數據庫授予 dbcreator
在 Microsoft SQL Server 2017+ 中,我想授予單個使用者的dbcreator角色,但只允許她創建名稱與固定前綴匹配的數據庫。
是否可以使用內置功能或儲存過程在數據庫級別執行此操作?
您可以像這樣使用伺服器級觸發器:
CREATE OR ALTER TRIGGER [database_name_check] ON ALL SERVER FOR CREATE_DATABASE AS BEGIN SET NOCOUNT ON; DECLARE @event_data XML; SET @event_data = EVENTDATA(); IF ((SELECT @event_data.value('(/EVENT_INSTANCE/LoginName)[1]', 'NVARCHAR(256)') ) = 'NADABRUTO\edarl' AND (SELECT @event_data.value('(/EVENT_INSTANCE/DatabaseName)[1]', 'NVARCHAR(256)') ) NOT LIKE '%Stack%') BEGIN RAISERROR('NO CAN DO, BUCKAROO', 0, 1) WITH NOWAIT; ROLLBACK; END END; GO ENABLE TRIGGER [database_name_check] ON ALL SERVER; GO
或者像這樣在數據庫級別:
USE StackOverflow2013 GO CREATE OR ALTER TRIGGER [database_name_check] ON DATABASE FOR CREATE_DATABASE AS BEGIN SET NOCOUNT ON; DECLARE @event_data XML; SET @event_data = EVENTDATA(); IF ((SELECT @event_data.value('(/EVENT_INSTANCE/LoginName)[1]', 'NVARCHAR(255)') ) = 'NADABRUTO\edarl' AND (SELECT @event_data.value('(/EVENT_INSTANCE/DatabaseName)[1]', 'NVARCHAR(255)') ) NOT LIKE '%Stack%') BEGIN RAISERROR('NO CAN DO, BUCKAROO', 0, 1) WITH NOWAIT; ROLLBACK; END END; GO ENABLE TRIGGER [database_name_check] ON DATABASE; GO