sql server – Only grant dbcreator to databases that match the prefix

You can use a server-level trigger as follows:

CREATE OR CHANGE TRIGGER [database_name_check] On all servers
FOR CREATE_DATABASE
AS
BEGIN
SET NOCOUNT ON;

DECLARE @event_data XML;
SET @event_data = EVENTDATA ();
IF ((SELECT @ event_data.value (& # 39; / / EVENT_INSTANCE / LoginName)[1]& # 39;, & # 39; NVARCHAR (256)?) = & # 39; NADABRUTO  edarl & # 39;
AND (SELECT @ event_data.value (& # 39; / / EVENT_INSTANCE / DatabaseName)[1]& # 39;, & # 39; NVARCHAR (256) & # 39;)) NOT LIKE & # 39;% Stack% & # 39;
BEGIN
RAISERROR (& # 39; NO CAN DO, BUCKAROO & # 39 ;, 0, 1) WITH NOWAIT;
ROLLBACK;
THE END

THE END;

GO
ACTIVATE TRIGGER [database_name_check] On all servers;
GO

Or at the database level as follows:

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 (& # 39; / / EVENT_INSTANCE / LoginName)[1]& # 39;, & # 39; NVARCHAR (255) & # 39;)) = & # 39; NADABRUTO  edarl & # 39;
AND (SELECT @ event_data.value (& # 39; / / EVENT_INSTANCE / DatabaseName)[1]& # 39;, & # 39; NVARCHAR (255) & # 39;)) NOT LIKE & # 39;% Stack% & # 39;
BEGIN
RAISERROR (& # 39; NO CAN DO, BUCKAROO & # 39 ;, 0, 1) WITH NOWAIT;
ROLLBACK;
THE END

THE END;

GO

ACTIVATE TRIGGER [database_name_check] ON DATABASE;
GO