如何在TRIGGER运行时运行UPDATE Query?

时间:2014-03-07 11:52:25

标签: c# sql sql-server-2008

我有两个表(客户端和用户)。当客户端表中的EmailID更新时,EmailID应该在用户表中更新,因为我正在使用TRIGGER。但我得到了以下错误。

INSERT,UPDATE,DELETE或MERGE语句的目标表不允许使用NOLOCK和READUNCOMMITTED锁提示。

我的代码是

C#:

  protected void grdPreparer_UpdateCommand(object sender, GridCommandEventArgs e)
    {
        try
        {
            GridEditableItem item = (GridEditableItem)e.Item;
            String ClientID = item.GetDataKeyValue("ClientID").ToString();
            SqlConnection Con = new SqlConnection(connectionString);
            TextBox txtboxEmailID = item["EmailID"].Controls[0] as TextBox;
            TextBox txtboxMobile = item["Mobile"].Controls[0] as TextBox;
            Con.Open();
            SqlCommand cmd = new SqlCommand("update Clients  set EmailID='" + txtboxEmailID.Text + "',Mobile='" + txtboxMobile.Text + "' where ClientID='" + ClientID + "'", Con);
            cmd.ExecuteNonQuery();
            RadGrid1.AllowAutomaticUpdates = false;
            RadGrid1.Rebind();
        }
        catch { }
    } 

触发:

CREATE TRIGGER UpdateEmailIDForClients
ON Clients
FOR UPDATE
as
begin
DECLARE @OldEmail varchar(50),@NewEmail varchar(50)
SELECT @NewEmail = EmailID FROM INSERTED
SELECT @OldEmail = EmailID FROM DELETED
UPDATE Users SET EmailID = @NewEmail 
    WHERE EmailID = @OldEmail
END

请帮帮我。感谢..

1 个答案:

答案 0 :(得分:0)

您的用户表已被交易锁定。

检查以下链接

http://www.sql-server-performance.com/2009/nolock-hint-not-allowed-for-target-table/