国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數(shù)據(jù)庫 > SQL Server > 正文

SQL Server 2005中的DDL觸發(fā)器的實現(xiàn)

2024-08-31 00:47:50
字體:
供稿:網(wǎng)友
    sql server 2005中,新增加了許多新的特性,其中的ddl觸發(fā)器是個不錯的選擇,根據(jù)資料初步學習如下,現(xiàn)整理之:
   在sql server 2000中,只能為針對表發(fā)出的 dml 語句(insert、update 和 delete)定義 after 觸發(fā)器。sql server 2005 可以就整個服務器或數(shù)據(jù)庫的某個范圍為 ddl 事件定義觸發(fā)器。可以為單個 ddl 語句(例如,create_table)或者為一組語句(例如,ddl_database_level_events)定義 ddl 觸發(fā)器。在該觸發(fā)器內(nèi)部,您可以通過訪問 eventdata() 函數(shù)獲得與激發(fā)該觸發(fā)器的事件有關的數(shù)據(jù)。該函數(shù)返回有關事件的 xml 數(shù)據(jù)。每個事件的架構都繼承了 server events 基礎架構。
    比如,在sql server 2005中,建立一個叫ddltrtest 的數(shù)據(jù)庫,并且建立一個叫mytable的表
和usp_querymytable 的存儲過程,如下所示
    drop database [ddltrtest]
go
create database ddltrtest
go
use [ddltrtest]
go
if  exists (select * from sys.objects
  where object_id = object_id(n'[dbo].[mytable]')
  and type in (n'u'))
drop table [dbo].[mytable]
go
create table mytable(id int, name varchar(100))
go
insert into mytable select 1,'a'
insert into mytable select 2,'b'
insert into mytable select 3,'c'
insert into mytable select 4,'d'
insert into mytable select 5,'e'
insert into mytable select 6,'f'
go
use [ddltrtest]
go
if  exists (select * from sys.objects where object_id =
  object_id(n'[dbo].[usp_querymytable]')
  and type in (n'p', n'pc'))
drop procedure [dbo].[usp_querymytable]
go
create proc usp_querymytable
as
select * from mytable
go

   接下來定義一個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;

,歡迎訪問網(wǎng)頁設計愛好者web開發(fā)。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 陈巴尔虎旗| 甘谷县| 开阳县| 新津县| 凤山市| 汝州市| 新兴县| 鄄城县| 名山县| 凤城市| 墨竹工卡县| 包头市| 紫阳县| 宿松县| 崇阳县| 平利县| 平安县| 新晃| 东宁县| 武宁县| 西华县| 灵璧县| 德州市| 衡山县| 洪泽县| 博客| 广宗县| 治多县| 星子县| 迭部县| 安陆市| 伊通| 黎平县| 德化县| 清涧县| 金门县| 东宁县| 宜兰市| 孟连| 洛阳市| 浦东新区|