sql触发器

if exists(select [name] from sys.objects where [name]='updateChild' and [type]='TR')
drop trigger updateChild
go
create trigger updateChild
on TB_ProductType
for update
as
if update(Pids) or update(Pid) or update([Level])
begin
    update TB_ProductType set Pids=replace(pt.Pids,d.Pids,i.Pids),
    [Level]=case when (i.[Level]<>d.[Level]) then (pt.[Level]+i.[Level]-d.[Level]) else (pt.[Level]) end
    from TB_ProductType pt,deleted d,inserted i
    where charindex('|'+cast(d.ID as nvarchar(20)) +'|',pt.Pids)>0
end
go
model.pid = int.Parse(ddlLM.SelectedValue);

        string xzPids = "|0|";
        if (model.pid != 0)
        {
            Model.LM xzModel = bll.GetModelBystrWhere("ID=" + model.pid);
            if (xzModel != null)
            {
                xzPids = xzModel.pids + model.pid + "|";
                model.Level = xzModel.Level + 1;
            }
        }
        else 
            model.Level = 1;
        model.pids = xzPids;


if (id > 0)
        {

            Model.LM model_0 = bll.GetModelBystrWhere("ID=" + id);
            if (model_0 != null)
            {
                if (model_0.pid != model.pid) //上级已修改
                {
                    Maticsoft.DBUtility.DbHelperSQL.ExecuteSql("update TB_LM set Pids=replace(Pids,"
                        + "'" + model_0.pids + "','" + model.pids + "') where charindex('|"
                        + model_0.ID + "|',Pids)>0");
                }
            }
        }
原文地址:https://www.cnblogs.com/daixingqing/p/2768273.html