表级触发器不支持ddl语句

In this article, we will un-riddle the ways to make use of the data definition language trigger (DDL Trigger), in order to monitor the progressions made to the database programming objects, View, Procedure or Function with a few real-time examples.

在本文中,我们将阐明使用数据定义语言触发器(DDL触发器)的方法,以便通过一些实时示例来监视对数据库编程对象,视图,过程或函数进行的进度。

为什么要使用DDL触发器? (Why DDL Triggers?)

SQL Server DDL triggers are specifically used to control and review the changes taking place in the database. These triggers can be used to put the limit on the unauthorized clients to make DDL type of changes such as DROP VIEW, DROP PROCEDURE, DROP Function and so on using DDL Trigger. These triggers can also give permission to be able to make changes on specific database objects during a pre-decided time frames. The “Audit” is a majorly used for the implementation purpose of DDL triggers in the SQL Server. The Object Schema changes audit helps to follow who has performed the DDL proclamation. For instance, if we are keen on distinction who has dropped the Object or who has made changes to the Objects. The DDL triggers will be executed because as the Transact-SQL event all set on the database or on the server with characterize ON ALL SERVER or ON DATABASE. Here we need to know that the extent of the trigger depends upon the event.

SQL Server DDL触发器专门用于控制和查看数据库中发生的更改。 这些触发器可用于限制未授权客户端进行DDL类型的更改,例如使用DDL触发器进行DROP VIEW,DROP PROCEDURE,DROP Function等。 这些触发器还可以授予在预定时间范围内对特定数据库对象进行更改的权限。 “审核”主要用于SQL Server中DDL触发器的实现目的。 对象架构更改审核有助于跟踪谁执行了DDL声明。 例如,如果我们热衷于区分谁丢弃了对象或谁更改了对象。 之所以将执行DDL触发器,是因为所有Transact-SQL事件都是在数据库或服务器上设置的,其特征是ON ALL SERVER或ON DATABASE。 在这里,我们需要知道触发的程度取决于事件。

DDL触发器如何工作? (How DDL Trigger Works?)

Every DDL operation generates one Transaction in case of the DDL Trigger have been applied at the Database or the Server level. The SQL Server generates the events with relevant information in the same transaction following the operation. Prepare a metric with extracting the DDL event function(EVENTDATA()) to wraps a policy or standards for deployment:

如果在数据库或服务器级别应用了DDL触发器,则每个DDL操作都会生成一个事务。 该操作之后,SQL Server将在同一事务中生成具有相关信息的事件。 通过提取DDL事件函数( EVENTDATA() )准备一个度量标准,以包装用于部署的策略或标准:

The EVENTDATA() is an inbuilt function of the DDL trigger in SQL Server and that would return exchange occasion subtleties with the number of the fields in XML format

EVENTDATA()是SQL Server中DDL触发器的内置函数,它将以XML格式的字段数返回交换时机的细微差别

  • EventType (Create View, Alter View, Drop View, etc…) EventType (创建视图,更改视图,放置视图等)
  • PostTime (Event trigger time) PostTime (事件触发时间)
  • SPID (SQL Server session ID) SPID (SQL Server会话ID)
  • ServerName (SQL Server instance name) ServerName (SQL Server实例名称)
  • LoginName (SQL Server Login name) LoginName (SQL Server登录名)
  • UserName (username for login, by default dbo schema as username)
  • 用户名 (用于登录的用户名,默认情况下为dbo模式作为用户名)
  • DatabaseName (name of database where trigger was executed) DatabaseName (执行触发器的数据库的名称)
  • SchemaName (schema name of the View) SchemaName (视图的模式名称)
  • ObjectName (Name of the View) ObjectName (视图名称)
  • ObjectType (Object types. such as Table, view, procedure, etc…) ObjectType (对象类型,例如表格,视图,过程等)
  • TSQLCommand (Schema deployment Query which is executed by user) TSQLCommand (由用户执行的架构部署查询)
  • SetOptions (SET Option which are applied while Creating View or Modify it) SetOptions (在创建视图或修改视图时应用的SET选项)
  • CommandText (Create, Alter or Drop object command) CommandText (创建,更改或删除对象命令)

EVENTDATA() returns multiple fields in XML format as shown above and using those fields, we are able to create such metrics to track various events of DDL over the objects. In general, each DDL event of the object schema changes can be appended into the table, these event types are mentioned in the header body of ä trigger with the FOR CREATE_, ALTER_, DROP_,…

EVENTDATA()返回XML格式的多个字段,如上所示,并且使用这些字段,我们能够创建此类指标来跟踪对象上DDL的各种事件。 通常,可以将对象模式更改的每个DDL事件附加到表中,这些事件类型在ä触发器的标头主体中用FOR CREATE_,ALTER_,DROP_等表示。

Trigger:

触发:

CREATE TRIGGER audit_objects
ON database
FOR CREATE_VIEW, DROP_VIEW, ALTER_VIEW, CREATE_PROCEDURE, DROP_PROCEDURE, ALTER_PROCEDURE, CREATE_FUNCTION, DROP_FUNCTION, ALTER_FUNCTION
AS
BEGIN
INSERT INTO master.dbo.event_object_data(in_)--Inserting data into the table in XML format
SELECT EVENTDATA();
END
GO

View Script:

查看脚本:

CREATE VIEW vw_roles
AS
(SELECT role_id, role_nameFROM tbl_roles
);

Using the above trigger for Creating, Altering or Dropping the View, Function or procedure, the transactions that were finished successfully or not can be monitored. Furthermore, at this moment we can check the event_object_data table to get the latest event data. We can see here that each detail of the above transaction has been included in the XML design:

使用上面的触发器来创建,更改或删除视图,函数或过程,可以监视是否成功完成的事务。 此外,此刻我们可以检查event_object_data表以获取最新的事件数据。 我们可以在这里看到上述事务的每个细节都已包含在XML设计中:

<EVENT_INSTANCE><EventType>CREATE_VIEW</EventType><PostTime>2019-09-20T14:47:21.070</PostTime><SPID>58</SPID><ServerName>JERRY\jignesh</ServerName><LoginName>sa</LoginName><UserName>dbo</UserName><DatabaseName>auth</DatabaseName><SchemaName>dbo</SchemaName><ObjectName>vw_roles</ObjectName><ObjectType>VIEW</ObjectType><TSQLCommand><SetOptions ANSI_NULLS="ON" ANSI_NULL_DEFAULT="ON" ANSI_PADDING="ON" QUOTED_IDENTIFIER="ON" ENCRYPTED="FALSE" /><CommandText>CREATE VIEW vw_roles
AS
(SELECT role_id, role_nameFROM tbl_roles
);</CommandText></TSQLCommand>
</EVENT_INSTANCE>

In the above instance, we have rightfully sent out of the XML in the table. Despite that, in one of the correct methods for checking that XML has to be pulled out in the Column arrangement of the table. Talking of the same, underneath the XML command can assist with gathering the required columns of the event data.

在上面的例子中,我们正确地发送了表中的XML。 尽管如此,在检查XML的正确方法之一中,还是必须在表的Column排列中将其拔出。 谈论同样的事情,在XML命令下面可以帮助收集事件数据的所需列。

@xml.value('EventType[1]', 'VARCHAR(128)') AS 'Event Type'

Here, @xml is a EVENTDATA() with in a trigger only.

在这里,@ xml是仅在触发器中的EVENTDATA()。

授权登录以使用DDL部署对象 (Authorize Login to Deploy Objects using DDL)

The Development or the QA Server does not require restricting execution of the procedure or any other programming objects; however, on the production server, sometimes the client does not have access to some contents of the table or the associated database objects. The procedure above will allow getting executed from the application itself and no one can alter or drop the objects apart from an authorized login or authorized member of the designated role; hence, in a few certain situations, the confidential object can also be restricted in order to secure the privacy of the client’s data.

开发或QA服务器不需要限制过程或任何其他编程对象的执行; 但是,在生产服务器上,有时客户端无法访问表或关联的数据库对象的某些内容。 上面的过程将允许从应用程序本身执行,并且除了指定角色的授权登录或授权成员之外,没有人可以更改或删除对象; 因此,在某些情况下,也可以限制机密对象,以保护客户数据的私密性。

The Table and Information access can be handled with the help of different methodologies in SQL Server. However, for programming object schema changes, we are required to manage user and role policies to allow members to perform CREATE, ALTER and DROP operations (DDL). The DDL trigger allows us to fiddle with a particular type of object to authorized members and roles in the SQL Server.

可以使用SQL Server中的不同方法来处理对表和信息的访问。 但是,对于编程对象架构更改,我们需要管理用户和角色策略,以允许成员执行CREATE,ALTER和DROP操作(DDL)。 DDL触发器使我们能够对SQL Server中授权的成员和角色进行特殊类型的对象摆弄。

Most of the companies manage the release code branch to track the schema changes in the SQL Server to track down the information like, who, when and for what situation applied? In order to make it easy to get some reports or get details in tabular ways, the event data can be extracted to the table in a database. As the earlier mentioned EVENTDATA() returns each detail of transactions with the type of event with an object.

大多数公司都管理发布代码分支,以跟踪SQL Server中的架构更改,以跟踪信息,例如,谁,何时以及在什么情况下应用? 为了便于以表格方式获取一些报告或获取详细信息,可以将事件数据提取到数据库的表中。 如前所述,EVENTDATA()返回带有对象事件类型的事务的每个细节。

Event type can be differentiated by XML to supervise DDL events over the objects. As an example, authorized role members will only make schema changes or DROP afterward. Now using the below DDL Trigger we will be able to make authorization wrapper over the objects:

XML可以区分事件类型,以监督对象上的DDL事件。 例如,授权角色成员将仅在之后进行架构更改或DROP。 现在,使用下面的DDL触发器,我们将能够对对象进行授权包装:

CREATE TRIGGER audit_objects
ON database
FOR CREATE_VIEW, DROP_VIEW, ALTER_VIEW, CREATE_PROCEDURE, DROP_PROCEDURE, ALTER_PROCEDURE, CREATE_FUNCTION, DROP_FUNCTION, ALTER_FUNCTION
AS
BEGINDECLARE @var_xml XML = EVENTDATA();DECLARE @error_msg VARCHAR(1024);IF(@var_xml.value('(EVENT_INSTANCE/ObjectType)[1]', 'VARCHAR(128)') = 'VIEW' AND @var_xml.value('(EVENT_INSTANCE/ObjectName)[1]', 'VARCHAR(128)') IN ('vw_roles')AND @var_xml.value('(EVENT_INSTANCE/LoginName)[1]', 'VARCHAR(128)') = 'myel')BEGINSET @error_msg = @var_xml.value('(EVENT_INSTANCE/LoginName)[1]', 'VARCHAR(128)') + ' is not allowed to ' + @var_xml.value('(EVENT_INSTANCE/EventType)[1]', 'VARCHAR(128)') + ' '+ @var_xml.value('(EVENT_INSTANCE/ObjectName)[1]', 'VARCHAR(128)') +'.';PRINT @error_msg;ROLLBACK;INSERT INTO master.dbo.event_object_data(in_)SELECT @var_xml;END
END
GO

In the above Trigger we have validated it using DDL with the condition that If the Object Type is ‘VIEW’ and the Object Name is ‘vw_roles’ and the Logged in user is ‘myel’ then DDL trigger audit_objects will not permit to make changes and the user will be acknowledged with the error message:

在上面的触发器中,我们已使用DDL对其进行了验证,条件是:如果对象类型为“ VIEW”,对象名称为“ vw_roles”,并且登录用户为“ myel”,则DDL触发器audit_objects将不允许进行更改,并且错误消息将确认用户:

As can be seen, the user gets an error on altering the view and event_object_data will get inserted as above with the event data. In the above example, we have one object name only in condition but that could be combined with multiple objects or the object types and user login as well.

可以看出,用户在更改视图时遇到错误,并且event_object_data将与事件数据如上插入。 在上面的示例中,我们仅在一个条件下有一个对象名称,但是可以与多个对象或对象类型和用户登录名结合使用。

In the above example, myel is not allowed to ALTER_VIEW vw_roles. The message gets logged and roll-back the transaction is performed. Simultaneously event data will get inserted in event_object_data table in the master database in XML format as shown below:

在上面的示例中,myel不允许使用ALTER_VIEW vw_roles。 该消息将被记录并回滚事务。 同时,事件数据将以XML格式插入主数据库的event_object_data表中,如下所示:

<EVENT_INSTANCE><EventType>ALTER_VIEW</EventType><PostTime>2019-09-20T15:12:20.077</PostTime><SPID>57</SPID><ServerName>JERRY\jignesh</ServerName><LoginName>myel</LoginName><UserName>myel</UserName><DatabaseName>auth</DatabaseName><SchemaName>dbo</SchemaName><ObjectName>vw_roles</ObjectName><ObjectType>VIEW</ObjectType><TSQLCommand><SetOptions ANSI_NULLS="ON" ANSI_NULL_DEFAULT="ON" ANSI_PADDING="ON" QUOTED_IDENTIFIER="ON" ENCRYPTED="FALSE" /><CommandText>ALTER VIEW vw_roles
AS
(SELECT role_id, role_nameFROM tbl_roles
);</CommandText></TSQLCommand>
</EVENT_INSTANCE>

The database administrator can drop or modify the trigger within SSMS as well, however, the user needs to have permissions to execute that action:

数据库管理员也可以在SSMS中删除或修改触发器,但是,用户需要具有执行该操作的权限:

As a contraposition to this, the database authorized operators should get an alert in case a user who is trying to deploy the schema changes however the user is not permitted to deploy. Now to avoid this circumstance and to follow an add-on step, in a certain diverted way, a mail alert will be triggered inside the same DDL trigger with essential information. Anyhow, event data will get exported into the table to get detailed information about the occurrence; however, an alert that makes sense to keep an eye on the database as part of the security monitor:

与此相反,万一试图部署架构的用户发生更改,但不允许该用户部署,则数据库授权操作员应收到警报。 现在,为了避免这种情况,并按照某种附加的方式执行附加步骤,将在同一DDL触发器内触发包含基本信息的邮件警报。 无论如何,事件数据将被导出到表中以获取有关事件的详细信息。 但是,作为安全监视器的一部分,有必要注意数据库的警报:

EXEC msdb.dbo.sp_send_dbmail@profile_name = 'Database Administrator Alert',@recipients = 'dba.group@test.com',@body = @body_html,@subject = @alert_subject

Finally, the subject can be obtained with the name of the object, object type, database name and server name for just simplicity to identify the mail alert. The recipients should be part of the group mail address of the responsible database administrator group. In this article code snippet, we explained views only, however, it can be other database objects as well like: procedures and functions mentioned in the DDL trigger header.

最后,可以使用主题名称,对象类型,数据库名称和服务器名称来获取主题,以简化邮件警报的识别。 收件人应该是负责的数据库管理员组的组邮件地址的一部分。 在本文的代码片段中,我们仅说明了视图,但是,它也可以是其他数据库对象,例如:DDL触发器标头中提到的过程和函数。

目录 (Table of contents)

Limit SQL Server Login Authentication scope using a Logon Trigger
Database Level DDL Triggers on Tables
Database Level DDL Triggers for Views, Procedures and Functions
使用登录触发器限制SQL Server登录身份验证范围
表上的数据库级DDL触发器
用于视图,过程和函数的数据库级DDL触发器

翻译自: https://www.sqlshack.com/database-level-ddl-triggers-for-views-procedures-and-functions/

表级触发器不支持ddl语句

表级触发器不支持ddl语句_用于视图,过程和函数的数据库级DDL触发器相关推荐

  1. 表级触发器不支持ddl语句_表上的数据库级DDL触发器

    表级触发器不支持ddl语句 This article portrays the different utilization of database DDL Triggers for the Table ...

  2. java 执行ddl语句_在JDBC中,如何知道DDL语句是否成功执行?

    我正在尝试使用JDBC在Oracle 11g数据库上执行DDL语句.我现在用的是这样做boolean execute(String SQL)的的Statement类. 以下是执行查询并尝试确定查询结果 ...

  3. 人大金仓数据库sql语句_人大金仓KingbaseES与主流数据库的兼容性

    KingbaseES针对Oracle.DB2.SQL Server等为代表的国外主流数据库产品,在服务器.接口.工具等各组件中全面改进了兼容性,屏蔽KingbaseES与这些产品之间的差异,从而减少现 ...

  4. drop sql语句_用于从表中删除数据SQL Drop View语句

    drop sql语句 介绍 (Introduction) This guide covers the SQL statement for dropping (deleting) one or more ...

  5. 千万级大表如何更快速的创建索引_分享一份生产环境mysql数据库大表归档方案,值得收藏...

    概述 分享下最近做的一个mysql大表归档方案,仅供参考. 整体思路 一.明确哪些大表需做归档 1.数据库表概要信息统计 SELECTt1.table_schema,t1.table_name,`EN ...

  6. pdm生成mysql sql语句_如何用Powerdesigner的PDM生成数据库

    展开全部 工具: Sybase PowerDesigner 15.1 Microsoft SQL Server 2005 第一步概要设计: 打开PowerDesigner软件,设计"概念数据 ...

  7. this调用语句必须是构造函数中的第一个可执行语句_谈谈JavaScript中的函数构造式和new关键字...

    您是否曾困惑于 Javascript 中的new关键字呢?是否曾想理解关于 function 和 constructor 的区别是什么呢? 大多数 Javascript 的新开发者不太想要使用new关 ...

  8. mysql 存储过程 预处理语句_用于预处理语句的MySQL存储过程游标

    我有一些坏消息和好消息 . 首先是坏消息 . MySQL手册说游标不能用于使用PREPARE和EXECUTE准备和执行的动态语句 . 在游标创建时检查游标的语句,因此该语句不能是动态的 . 所以到目前 ...

  9. left函数未定义_关于子过程或函数未定义的问题 求大神帮忙看看

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 1.FJ?是什么啊 2.find在vba里是查找,并不是像工作表函数那样.可以改成application.find或着instr(FJ,"-&q ...

最新文章

  1. Mysql For Windows安装图解
  2. python3精要(31)-生成器函数yield
  3. eclipse远程连接Hadoop,用户认证失败
  4. 基于Android的闹钟的软件
  5. Cannot find 'Enhance Component' button in BSP Workbench
  6. java svg 读取dom结构_SVG基础以及使用Javascript DOM操作SVG
  7. OpenShift 4 - 向OpenShift添加新的SSH Key
  8. rust种的南瓜为什么老是消失_科技的力量!3种“奇葩”的发明,你都见过吗?...
  9. 用户研究三部曲:有关用户研究的战略思考
  10. 20180914 文件和目录的权限以及属性
  11. 复化辛普森公式求二重积分matlab源码及例题
  12. HDU - 5699(79/600)
  13. Chloe 蔻依 恋旅
  14. MCE | “神药”二甲双胍后,糖尿病药物研究谁将是下一个顶流?
  15. STM32实战(1):搭建模板工程
  16. 有哪些高性价比的LoRa模块?
  17. 被迫选择了到了外包公司
  18. Linux centos7 搭建k8s集群步骤详解
  19. keil工具栏错乱,图标显示不正常,工具栏不小心删除,等keil界面问题
  20. 如何获取微信小程序中动态渲染的列表中的某一个数据

热门文章

  1. Windows 10 使用问题
  2. [学习笔记] 七步从AngularJS菜鸟到专家(6):服务 [转]
  3. [转载] 七龙珠第一部——第086话 打进前八强
  4. 【博客项目】—登录功能实现( 四)
  5. jQuery学习(十二)—jQuery中对象的查找方法总结
  6. JavaScript实现中国地图圆点标注(二十四)
  7. 对象已死?及其判断算法
  8. G-SYNC技术是什么
  9. 语文好的人是怎么做到的?
  10. 20多年前我住的平房