---
title: "sql   触发器"
author: "freellf"
date: "2012-04-22T23:34:37Z"
tags: []
categories: ["数据库"]
slug: "sql"
draft: false
lastmod: "2012-04-22T23:34:19Z"
id: 1494093
url: "https://freellf.iteye.com/blog/1494093"
---

if exists (select 1
  from sysobjects
  where id = object_id('T_accountingsubject')
  and type = 'TR')
  drop trigger T_accountingsubject
go
create trigger T_accountingsubject on 表名
for/after/instead of insert
as
begin
if exists(select 1 from inserted) and not exists(select 1 from deleted)
begin
  update accountingsubjects set isend=0 where id in(select parentid from inserted)
end
end
go



if exists (select 1
  from sysobjects
  where id = object_id('T_accountingsubject')
  and type = 'TR')
  drop trigger T_accountingsubject
go
create trigger T_accountingsubject on 表名
for/after/instead of insert
as
if exists(select 1 from inserted) and not exists(select 1 from deleted)
begin
  update accountingsubjects set isend=0 where id in(select parentid from inserted)
end
go

更正如上