September 18, 2010 at 9:28 am
how can i exec this trigger in all db's of the server ?
alter TRIGGER [Trg_DenyPerm]
ON DATABASE
FOR
CREATE_TABLE
AS
BEGIN
SET NOCOUNT ON
DECLARE
@eventData XML,
@DBName SYSNAME,
@EVENTDATE DATETIME,
@USERNAME SYSNAME,
@SYSTEMUSER VARCHAR(128),
@CURRENTUSER VARCHAR(128),
@ORIGINALUSER VARCHAR(128),
@HOSTNAME VARCHAR(128),
@APPLICATIONNAME VARCHAR(128),
@SCHEMANAME SYSNAME,
@OBJECTNAME SYSNAME,
@OBJECTTYPE SYSNAME,
@EVENTTYPE VARCHAR(128),
@COMMANDTEXT VARCHAR(max),
@NAMEFORDEFINITION VARCHAR(261)
SET @eventData = eventdata()
SELECT
@DBName = db_name(),
@EVENTDATE = GETDATE(),
@USERNAME = @eventData.value('data(/EVENT_INSTANCE/UserName)[1]', 'SYSNAME'),
@SYSTEMUSER = SUSER_SNAME(),
@CURRENTUSER = CURRENT_USER,
@ORIGINALUSER = ORIGINAL_LOGIN(),
@HOSTNAME = HOST_NAME(),
@APPLICATIONNAME = APP_NAME(),
@SCHEMANAME = @eventData.value('data(/EVENT_INSTANCE/SchemaName)[1]', 'SYSNAME'),
@OBJECTNAME = @eventData.value('data(/EVENT_INSTANCE/ObjectName)[1]', 'SYSNAME'),
@OBJECTTYPE = @eventData.value('data(/EVENT_INSTANCE/ObjectType)[1]', 'SYSNAME'),
@COMMANDTEXT = @eventData.value('data(/EVENT_INSTANCE/TSQLCommand/CommandText)[1]','VARCHAR(MAX)'),
@EVENTTYPE = @eventData.value('data(/EVENT_INSTANCE/EventType)[1]', 'nvarchar(128)')
IF @OBJECTTYPE ='TABLE'
BEGIN
IF @OBJECTNAME in ('EligDates')
BEGIN
IF SUSER_SNAME() NOT IN('sa','sysadmin','state\MLevan')
BEGIN
RAISERROR('You do not have to this table', 16, 1)
ROLLBACK
END
ELSE
BEGIN
EXEC master.dbo.DenyPerm @DBName
END
END
END
END
September 18, 2010 at 11:23 am
Are you familiar with the SP (In master DB) sp_FOREACHDB? To read how to use this go to:
September 18, 2010 at 12:50 pm
I have alreayd tried it but i think it will not work as i try it says " TRIGGER should be the first statement"
September 20, 2010 at 9:14 pm
This is not the most efficient way of doing it but it works
SELECT ' USE ' + Name +
char (10) + ' go ' + char(10)
+ ' alter statement ' + char (10) + ' go'
from sys.databases
output results to text
October 10, 2010 at 4:35 am
You can make use of sqlcmd and provide a input file(with sql script) to it and run it on desired databases.
MJ
October 10, 2010 at 5:38 pm
You collect a bunch of data and never use it. I'm just curious as to why not.
Also, it's a form of "Death by SQL" to hardcode a user name anywhere in the database. Spend some time looking up privs and roles instead of using a trigger to attempt to overcome the problem. It's just going to get worse down the line if you use that trigger.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply