接下來定義一個ddl觸發(fā)器如下 
   
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 
 結果如下,出現(xiàn)錯誤提示 
  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. 
再執(zhí)行drop的操作,同樣觸發(fā)警告
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.
   因為我們的觸發(fā)器規(guī)定了不能使用create_table,drop_table, 
  alter_table,create_procedure,
  alter_procedure,drop_procedure等操作。     如果我們要關掉這個觸發(fā)器,可以這樣做:  disable trigger stop_ddl_on_table_and_proc 
on database   當然,我們要對整個服務器采取策略的話,也是很簡單的,和上面的方法大致相同只不過將on database的參數(shù)改為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;
新聞熱點
疑難解答
圖片精選