接下来定义一个ddl触发器如下
create trigger stop_ddl_on_table_and_proc
on database
for create_table,drop_table,
alter_table,create_procedure,
alter_procedure,drop_procedure
as
select eventdata().value
('(/event_instance/tsqlcommand/commandtext)[1]',
'nvarchar(max)')
print 'you are not allowed to create,alter and drop
any tables and procedures'
rollback;
接下来,我们尝试如下的操作:
alter table mytable add x int
结果如下,出现错误提示
alter table mytable add x int
(1 row(s) affected)
you are not allowed to create,alter and drop any tables and procedures
msg 3609, level 16, state 2, line 1
the transaction ended in the trigger. the batch has been aborted.
再执行drop的操作,同样触发警告
drop table mytable
(1 row(s) affected)
you are not allowed to create,alter and drop any tables and procedures
msg 3609, level 16, state 2, line 1
the transaction ended in the trigger. the batch has been aborted.
因为我们的触发器规定了不能使用create_table,drop_table,
alter_table,create_procedure,
alter_procedure,drop_procedure等操作。 如果我们要关掉这个触发器,可以这样做: disable trigger stop_ddl_on_table_and_proc
on database 当然,我们要对整个服务器采取策略的话,也是很简单的,和上面的方法大致相同只不过将on database的参数改为on server,比如 create trigger stop_ddl_on_table_and_proc
on all server
for create_database,alter_database,drop_database
as
print 'you are not allowed to create,alter and drop any databases'
rollback;
新闻热点
疑难解答