SQL Server : DDL trigger, controlling table creation

风格不统一 提交于 2019-12-23 15:15:58

问题


I'm using SQL Server 2008.

I'm creating a DDL trigger like this:

CREATE  TRIGGER  tName ON database FOR CREATE_TABLE
as
  print 'A table has been created'

Can I get that table that has been created !?

Something like inserted or deleted in the normal table triggers ?!


回答1:


Try this:

CREATE TRIGGER TRG_TABLES
ON DATABASE 
AFTER 
    CREATE_TABLE
AS 
BEGIN
    SET NOCOUNT ON

    DECLARE @TABLE_NAME SYSNAME

    SELECT 
        @TABLE_NAME = EVENTDATA().value('(/EVENT_INSTANCE/ObjectName)[1]','SYSNAME')

    ...


END
GO



回答2:


I believe you would need to extract it from the CommandText in the EventData().

http://msdn.microsoft.com/en-us/library/ms187909.aspx



来源:https://stackoverflow.com/questions/9797739/sql-server-ddl-trigger-controlling-table-creation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!