USE [UFDATA_001_2017] GO /****** Object: Trigger [dbo].[JDS_Customer_UPDATECITY] Script Date: 07/06/2018 16:53:04 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TRIGGER [dbo].[JDS_Customer_UPDATECITY] ON [dbo].[Customer] FOR INSERT, DELETE ,UPDATE(去掉,不然會報錯嵌套層數不超過32) NOT FOR REPLICATION AS BEGIN SET NOCOUNT ON; --前提:地區檔案裏的城市名稱必須規範統一 --從客戶檔案裏獲取地區編碼,從地區分類裏獲取城市 declare @CUScode varchar(20) declare @city varchar(20) declare @cProvince varchar(20) select @CUScode=ccuscode from inserted select @city=cDCName from DistrictClass where cDCCode = (select cdccode from customer where cCusCode=@CUScode) --從省份表裏獲取省份 select @cProvince=vsimplename from HR_CT007 where ccodeID =( select distinct cpCodeID from HR_CT007 where ilevels in(1) and vdescription like '%'+@city+'%') --最後更新客戶檔案裏的省份和城市 update customer set cProvince=@cProvince,cCity=@city where cCusCode=@CUScode SET NOCOUNT OFF; END GO