sql server 触发器

shuangfushui 2013-11-12 05:03:41
表具有带层叠的update的foreing key时该怎么处理??、
...全文
104 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
shuangfushui 2013-11-12
  • 打赏
  • 举报
回复
嗯嗯,嘿嘿,谢谢大哥
發糞塗牆 2013-11-12
  • 打赏
  • 举报
回复
你这代码也太长了吧。你按我那个文章改改,试试用with cascade来级联更新试试,我也没在触发器里面试过
shuangfushui 2013-11-12
  • 打赏
  • 举报
回复
GO /****** Object: Trigger [dbo].[tb_instrument_table_Update] Script Date: 11/12/2013 16:38:27 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER trigger [dbo].[tb_instrument_table_Update]//错误时无法对dbo.tb_instrument_table创建tb_instrument_table_Update触发器因为表具有带层叠的update的foreing key on [dbo].[tb_instrument_table] AFTER update as DECLARE @_instrumentID bigint, @_element_name varchar(50), @_instrument_name nvarchar(50), @_metadata_identifiers varchar(50), @_start_observation_time varchar(20), @_date_time datetime, @_instrument_ground_platform_height int, @_Observation_Platform_Hight int if update (element_name)--当要素字段被更新时,才会触发此触发器 begin select @_instrumentID=[instrumentID], @_element_name=[element_name] , @_instrument_name=[instrument_name], @_metadata_identifiers=[metadata_identifiers], @_start_observation_time=[start_observation_time] , @_date_time=cast([start_observation_time]as datetime), @_instrument_ground_platform_height=[instrument_ground_platform_height], @_Observation_Platform_Hight=[Observation_Platform_Hight] from Inserted declare @ID varchar(100) set @ID='' select @ID=instrument_equipmentID from tb_station_element_table where charindex(';'+rtrim([element_name])+';',';'+rtrim(@_element_name)+';')>0 and metadata_identifiers=@_metadata_identifiers --如果ID为空则更新ID if(@ID='') begin --if(charindex('传感器',rtrim(@_instrument_name))>0 or charindex(rtrim(@_instrument_name),'传感器')>0) if(PATINDEX ('%传感器%',@_instrument_name)>0) begin print(@_instrument_name) update [tb_station_element_table] set [instrument_equipmentID]=@_instrumentID,observation_method='0' where charindex(';'+rtrim([element_name])+';',';'+rtrim(@_element_name)+';')>0 and [metadata_identifiers] like @_metadata_identifiers and cast([start_observation_time]as datetime)<=@_date_time and cast([stop_observation_time]as datetime)>=@_date_time end else begin update [tb_station_element_table] set [instrument_equipmentID]=@_instrumentID,observation_method='1' where charindex(';'+rtrim([element_name])+';',';'+rtrim(@_element_name)+';')>0 and [metadata_identifiers] like @_metadata_identifiers and cast([start_observation_time]as datetime)<=@_date_time and cast([stop_observation_time]as datetime)>@_date_time end end --否则判断当前仪器的开始观测时间大于该ID对应仪器的开始时间则更新观测要素仪器ID字段 else begin declare @obstime varchar(20),--当前仪器开始时间 @Iname nvarchar(50)--当前仪器名称 select @obstime=start_observation_time ,@Iname=instrument_name from tb_instrument_table where instrumentID=cast(@ID as bigint) --当前要素的观测仪器开始时间小于等于 新插入仪器的开始时间 if(cast(@obstime as datetime)<=cast(@_start_observation_time as datetime)) begin --如果当前仪器不为传感器被更新 if((PATINDEX ('%传感器%',@Iname)<=0)) begin --如果新仪器为传感器则跟新为自动 if(PATINDEX ('%传感器%',@_instrument_name)<=0) begin print(@_instrument_name) update [tb_station_element_table] set [instrument_equipmentID]=@_instrumentID,observation_method='1' where charindex(';'+rtrim([element_name])+';',';'+rtrim(@_element_name)+';')>0 --where(PATINDEX ('%'+@_element_name+'%',';'+rtrim([element_name])+';')>0) and [metadata_identifiers] like @_metadata_identifiers and cast([start_observation_time]as datetime)<=@_date_time and cast([stop_observation_time]as datetime)>=@_date_time end --否则为人工 else begin update [tb_station_element_table] set [instrument_equipmentID]=@_instrumentID,observation_method='0' where charindex(';'+rtrim([element_name])+';',';'+rtrim(@_element_name)+';')>0 and [metadata_identifiers] like @_metadata_identifiers and cast([start_observation_time]as datetime)<=@_date_time and cast([stop_observation_time]as datetime)>@_date_time end end end --当前要素的观测仪器开始时间大于新仪器的开始时间 else begin --新仪器为传感器且当前的仪器不是传感器 if((PATINDEX ('%传感器%',@_instrument_name)>0)and(PATINDEX ('%传感器%',@Iname)<=0) ) begin update [tb_station_element_table] set [instrument_equipmentID]=@_instrumentID,observation_method='0' where charindex(';'+rtrim([element_name])+';',';'+rtrim(@_element_name)+';')>0 and [metadata_identifiers] like @_metadata_identifiers and cast([start_observation_time]as datetime)<=@_date_time and cast([stop_observation_time]as datetime)>@_date_time end end end end
發糞塗牆 2013-11-12
  • 打赏
  • 举报
回复
我擦,烂CSDN搞得没有提示了,什么错误?
shuangfushui 2013-11-12
  • 打赏
  • 举报
回复
引用 3 楼 DBA_Huangzj 的回复:
有个级联删除的例子,update的类似http://blog.csdn.net/dba_huangzj/article/details/8042999
大哥,可不可以再麻烦麻烦您,这个错误在触发器里怎么修改。
shuangfushui 2013-11-12
  • 打赏
  • 举报
回复
对的,我这个是在触发器中报的错,就不知道怎么改了
LongRui888 2013-11-12
  • 打赏
  • 举报
回复
引用 楼主 u012770396 的回复:
表具有带层叠的update的foreing key时该怎么处理??、
你是想,比如你修改了主表的主键id的值,附表的外键id也要跟这修改是吗
shuangfushui 2013-11-12
  • 打赏
  • 举报
回复
恩恩,谢谢,大哥
發糞塗牆 2013-11-12
  • 打赏
  • 举报
回复
有个级联删除的例子,update的类似http://blog.csdn.net/dba_huangzj/article/details/8042999
shuangfushui 2013-11-12
  • 打赏
  • 举报
回复
我可不可以说,我听不懂
發糞塗牆 2013-11-12
  • 打赏
  • 举报
回复
有外键的就用级联更新

34,588

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧