CLR Trigger only particular column get updated

独自空忆成欢 提交于 2019-12-11 00:11:24

问题


I wrote a clr trigger whenever a new file get inserted in to my table and then pass the value to my WCF service, now i have to change the process to "update" only the particular column get updated then i have to pull the value from other two tables.

Am just wondering is this anyway i can start the clr trigger just only the particular column get updated ?

The scenario like this

Table 1: Customer Details (Cust.No, Cust.Name,Desc)
Table 2: Address (DoorNo,Street,City,State).

Here what am trying to do, if the "Desc" column in Table1 get updated then the clr trigger get triggered and pass all the values in Table1 and Table2 based on the "Desc".

Here is my code for Insert:

[Microsoft.SqlServer.Server.SqlTrigger(Name = "WCFTrigger",Target = "tbCR", Event = "FOR UPDATE, INSERT")]
public static void Trigger1()
{
    SqlCommand cmd;
    SqlTriggerContext myContext = SqlContext.TriggerContext;
    SqlPipe pipe = SqlContext.Pipe;
    SqlDataReader reader;

    if(myContext.TriggerAction== TriggerAction.Insert)
    {
        using (SqlConnection conn = new SqlConnection(@"context connection=true"))
        {
            conn.Open();
            //cmd = new SqlCommand(@"SELECT * FROM tbCR", conn);
            cmd = new SqlCommand(@"SELECT * FROM INSERTED", conn);
            reader = cmd.ExecuteReader();
            reader.Read();
            //get the insert value's here
            string Cust.No, Cust.Name,Desc;
            Cust.No = reader[0].ToString();
            Cust.Name = reader[1].ToString();
            Desc = reader[2].ToString();
            myclient.InsertOccured(Cust.No, Cust.Name,Desc);
            reader.Dispose();
        }
    }
}

回答1:


You cannot prevent running the trigger selectively, it will always run no matter the columns updated. However, once launched you can consult the COLUMNS_UPDATED() function:

Returns a varbinary bit pattern that indicates the columns in a table or view that were inserted or updated. COLUMNS_UPDATED is used anywhere inside the body of a Transact-SQL INSERT or UPDATE trigger to test whether the trigger should execute certain actions.

So you would adjust your trigger logic to have appropiate action according to what columns where updated.

That being said, calling WCF from SQLCLR is a very very bad idea. Calling WCF from a trigger is even worse. Your server will die in production as transactions will block/abort waiting on some HTTP response to crawl back across the wire. Not to mention that your calls are inherently incorrect in presence of rollbacks, as you cannot undo an HTTP call. The proper way to do such actions is to decouple the operation and the WCF call by means of a queue. You can do this with tables used as queues, you could use true queues or you could use Change Tracking. Any of these would allow you to decouple the change and the WCF call and would allow you to make the call from a separate process, not from SQLCLR



来源:https://stackoverflow.com/questions/9404533/clr-trigger-only-particular-column-get-updated

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