Trigger on extended properties? SQL SERVER - sql-server

Does SQL Server allow triggers on extended properties, table level or column level?
If not, is there anything like a trigger that can detect an 'add' or 'update' on extended properties and execute a stored procedure automatically?
Thank you!

You can use a DDL trigger for this.
CREATE TRIGGER foo
ON DATABASE
FOR CREATE_EXTENDED_PROPERTY, ALTER_EXTENDED_PROPERTY
AS
BEGIN
/*TODO: Something useful here*/
SELECT EVENTDATA()
END

Related

what is the purpose of database level trigger in the DDL trigger?

I am confused that what is the on database in the below trigger what is the purpose of using that please let me know what is the use of that
create trigger trmyfirsttrigger
on database
for create_table,alter_table,drop_table
as
begin
rollback
print 'you can not create ,alter,drop table.'
end
The trigger is defined at the database level and is intended to prevent any create, alter and drop statements on any table. That means you cannot create a new table, alter or delete existing tables on the database. This is generally used by DBAs to lock the database from any changes during maintenance & patching.

understanding a database trigger

I am new to triggers and cursors and would like to understand what a particular trigger is doing. Here is the Trigger.
CREATE TRIGGER [dbo].[trg_XOnUpdate]
ON TableX
AFTER UPDATE
AS
BEGIN
SET NOCOUNT ON
DECLARE #XID INT, #XKey NVARCHAR(33)
DECLARE updated_cursor CURSOR FOR
SELECT XID, XKey
FROM INSERTED
WHERE XStatus
IN ('AA', 'BB', 'CC')
OPEN updated_cursor
FETCH NEXT FROM updated_cursor INTO #XID, #XKey
WHILE ##FETCH_STATUS = 0
BEGIN
EXECUTE usp_UpdateXData #XID, #XKey
FETCH NEXT FROM updated_cursor INTO #XID, #XKey
END
CLOSE updated_cursor
DEALLOCATE updated_cursor
END
Understanding the Basic of Triggers
A trigger is an operation that is executed when some kind of event occurs to the database. It can be a data or object change. Listed below are the different types of tiggers:
Types of Triggers
DML(data manipulation language) triggers (SQL Server 2000- 80.0)
DDL(data definition language) triggers (SQL Server 2005- 90.0)
SQLCLR triggers (SQL Server 2005- 90.0)
Rules of Triggers
cannot create or modify Database objects using triggers
cannot perform any administrative tasks
cannot pass any kind of parameters
cannot directly call triggers
Advantage of Triggers
Triggers are useful for auditing data changes or auditing database as well as managing business rules. Below are some examples:
Triggers can be used to enforce referential integrity (For example you may not be able to apply foreign keys)
Can access both new values and old values in the database when going to do any insert, update or delete
Drill down further about triggers...
Understanding the Basic of Triggers
Understanding SQL Server inserted and deleted tables for DML triggers

SQL SERVER 2008 TRIGGER ON CREATE TABLE

Is there a way to run some function like a trigger when a table is created in the database in SQL SERVER 2008?
Yes, it's called a DDL trigger. The documentation for CREATE TRIGGER has a sample for DROP_SYNONYM (a very questionable choice for an example) but you'll want the CREATE_TABLE event instead. A better starting point in understanding how they work is probably here:
http://msdn.microsoft.com/en-us/library/ms190989.aspx
If you have more specific details, e.g. what exactly do you want to pass to this function (I assume you mean procedure), or what does the procedure do, we can provide more useful and specific help.
Yes a DDL Trigger. For example, here is some code I written to prevent some tables from being modified:
PRINT N'Create DDL trigger to prevent changes to various tables'
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TRIGGER NoEditCertainTables ON DATABASE
FOR DROP_TABLE, ALTER_TABLE, CREATE_TABLE
AS
SET CONCAT_NULL_YIELDS_NULL ON
DECLARE #AffectedTable varchar(255)
SELECT #AffectedTable = EVENTDATA().value('(/EVENT_INSTANCE/ObjectName)[1]','nvarchar(100)')
--This is the name of the table(s) you dont want messed with
IF (#AffectedTable IN ('Table1','Table2','Table3'))
BEGIN
ROLLBACK;
END
SET CONCAT_NULL_YIELDS_NULL OFF
GO

SQL Server Trigger - Need to Alter

I need to alter a trigger in SQL Server. After I am doing, do I just execute the trigger similar to how I would do for a Stored Procedure?
ALTER TRIGGER
Yes, that is right, just use ALTER. If you right-click on your trigger in Object Explorer in SSMS and select Script Trigger as/ALTER To, you will see the ALTER statement created for your trigger.
ALTER TRIGGER triggerName
ON tableName
FOR INSERT -- or update & delete
AS
-- sql here
http://msdn.microsoft.com/en-us/library/ms176072.aspx
You don't "execute" a trigger. Triggers are "triggered" at certain points depending upon your definition of them.
For example an AFTER UPDATE trigger would run for all rows updated after you send an UPDATE command to the table on which the trigger is created.

SQL CLR trigger: name of the context DB

I'd like to turn trigger recursion on/off in my SQL CLR trigger. According to http://www.devx.com/tips/Tip/30031, I have to call
EXEC sp_dboption '<name of db>', 'recursive triggers', 'true'/'false'
Is there a way to get to know what the current DB name is? When creating trigger, I ask users to choose one, but I don't want to write it in a table.
Regards,
There is a very simple way to find the name of the database in which the SQLCLR Trigger is being fired: just make a connection to the Context Connection and get the Database property. You don't even need to execute a query :-).
The following should work in all SQLCLR object types (Stored Procedure, Function, User-Defined Aggregate, User-Defined Type, and Trigger):
string _DatabaseName;
using (SqlConnection _Connection = new SqlConnection("Context Connection = true;"))
{
_Connection.Open();
_DatabaseName = _Connection.Database;
}
That's it! I just tried it in a SQLCLR Trigger and it works great.
Another thing to keep in mind for limiting Triggers firing other Triggers is the TRIGGER_NESTLEVEL function. This works better in T-SQL Triggers where the value of ##PROCID is available and contains the [object_id] of the Trigger. So in T-SQL Triggers you can limit the recursion of each trigger individually but still allow Triggers to fire other Triggers on other Tables.
In SQLCLR it can still be used, but without the name of the Trigger you can only limit all Triggers. Meaning, you can prevent any Trigger from firing any other Trigger on any Table, including on the same Table, but there is no way to limit the firing of only that same Trigger while allowing Triggers on other tables that might be modified by the Trigger in question. Just use a Context Connection and run SELECT TRIGGER_NESTLEVEL(); via SqlCommand.ExecuteScalar().
You know what the database is when you create the trigger...
CREATE TRIGGER etc
....
GO
DECLARE #db varchar(100)
SET #db = DB_NAME()
EXEC sp_dboption #db, 'recursive triggers', 'true'/'false'
I've found a better solution.
I have to avoid calling EXEC sp_dboption at all. Instead, I have to create a temp table as a flag "no recursion", then check existing of the table at the beginning of the trigger and exit, if the table exists.
Why temporary table?
It's being killed at the end of the session. No need to reset the flag (in exceptional situation), which is necessary otherwise to avoid trigger being off permanently.
AFAIK, it's being created and killed independently for every connection. So, if the user changes data the same time, there will be no conflict (which is inevitable for EXEC sp_dboption).

Resources