各位老大标题有点混乱,情况是这样的
现有 表a
- SQL code
CREATE TABLE [a]( [id] [int] IDENTITY(1,1) NOT NULL, [name] [varchar](50) NULL, [age] [varchar](50) NULL, CONSTRAINT [PK_a] PRIMARY KEY CLUSTERED ( [id] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]
表b
- SQL code
CREATE TABLE [dbo].[b]( [id] [int] IDENTITY(1,1) NOT NULL, [aid] [int] NULL, [price] [numeric](18, 0) NULL, [count] [numeric](18, 0) NULL, CONSTRAINT [PK_b] PRIMARY KEY CLUSTERED ( [id] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]GOALTER TABLE [dbo].[b] WITH CHECK ADD CONSTRAINT [FK_b_a] FOREIGN KEY([id])REFERENCES [dbo].[a] ([id])GOALTER TABLE [dbo].[b] CHECK CONSTRAINT [FK_b_a]GO
表C
- SQL code
CREATE TABLE [dbo].[c]( [id] [int] IDENTITY(1,1) NOT NULL, [aid] [int] NULL, [bid] [int] NULL, [oldname] [varchar](50) NULL, [name] [varchar](50) NULL, [oldage] [varchar](50) NULL, [age] [varchar](50) NULL, [oldprice] [numeric](18, 0) NULL, [price] [numeric](18, 0) NULL, [oldcount] [numeric](18, 0) NULL, [count] [numeric](18, 0) NULL, CONSTRAINT [PK_c] PRIMARY KEY CLUSTERED ( [id] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]
前端在插入,删除,修改的时候 a,b两个表示同时用事务操作。
我要实现的功能是, 建立一个触发器(不管是a还是b的触发器都行)
当a,b的值有更新的时候将数据插入到c中
例如我在前端执行 下面的sql语句
- SQL code
update a set name = '测试',age='10' where id = 1update b set price = 500,[count] = 300 where id = 1 and aid = 1
表C中数据应该为
- SQL code
id aid bid oldname name oldage age oldprice price oldcount count1 1 1 名称 测试 20 10 100 500 50 300
请教该如何处理
------解决方案--------------------
你是不知道怎么写触发器还是什么问题?
------解决方案--------------------
- SQL code
Create Trigger upd_adTOc on afor updateasif exists (select 1 from inserted,a where inserted.colname=a.colname)begin insert into c select col1,col2... from a end
------解决方案--------------------
楼主如果只有一个表更新呢,那怎么办?
还是说一定是2个表一定是一起更新
如果是第二种情况的话,楼主为何要写出发器,直接在写一条插入语句即可,触发器真是多此一举
------解决方案--------------------
------解决方案--------------------
想了一下,你这个需求有点不是很好实现,可以尝试使用SQLServer自带的CDC功能,可以把指定的表上数据的更改记录到某些表中,当你需要查的时候从那些表中组合出来就可以了,你的这个触发器要求的是实时性,不好处理,一旦A/B两表都需要更新对方的时候,那容易造成死锁,并且后续维护成本较大。
我个人觉得CDC功能已经可以满足了。不过不知道08之前有没有