question

dhurandharsingh avatar image
dhurandharsingh asked

Alert on table create

Hi Guys, I want to create an alert/trigger on 'create table/Select * into' and want to record it into a maintenance log table. Development users have rights on database and can create table by any of below command a. Select * into newtable from oldtable b. Create Table Tablename So i want to trace it into a maintenance log table with below information. Since user unnecessarily create new tables every time and it increase the database size and blame on others. TableName/Object Name | CreateDateTime | CreatedBy | Schema | DatabaseName | IP/Hostname Please if any one can suggest and provide the solution on it i tried through trigger but not able to do.
trigger
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Magnus Ahlkvist avatar image
Magnus Ahlkvist answered
You can do it with a database level ddl-trigger. use GO CREATE TRIGGER trg_ddl_create_table ON DATABASE FOR CREATE_TABLE AS PRINT 'Table created'; In order to do something more useful than just a PRINT, you ned to look at the XML-structure of Eventdata related to a DDL-trigger. It will require some xpath-querying against an XML-datatype. Here's some documentation about EVENTDATA, along with a few examples: https://docs.microsoft.com/en-us/sql/t-sql/functions/eventdata-transact-sql
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.