关于以数据库中两结构相同表的同步(用触发器)
来源:互联网 发布:轮状神经 知乎 编辑:程序博客网 时间:2024/05/16 06:48
这是个人总结高人的程序写的~
请问哪位高人能帮我指导,怎样编写个存储过程来同步,本人在做毕业设计,希望
各位大哥的帮忙,也希望我这点程序能让别人学多点的知识
if exists(select name from sysobjects where name='sale' and type='U')
drop table sale
go
if exists(select name from sysobjects where name='newsale' and type='U')
drop table newsale
go
CREATE TABLE [sale] (
[stor_id] [char] (4) COLLATE Chinese_PRC_CI_AS NULL ,
[ord_num] [varchar] (20) COLLATE Chinese_PRC_CI_AS NULL ,
[date] [datetime] NULL ,
[qty] [smallint] NULL ,
[payterms] [varchar] (12) COLLATE Chinese_PRC_CI_AS NULL ,
[title_id] [int] IDENTITY (1, 1) primary key NOT NULL
) ON [PRIMARY]
GO
CREATE TABLE [newsale] (
[stor_id] [char] (4) COLLATE Chinese_PRC_CI_AS NULL ,
[ord_num] [varchar] (20) COLLATE Chinese_PRC_CI_AS NULL ,
[date] [datetime] NULL ,
[qty] [smallint] NULL ,
[payterms] [varchar] (12) COLLATE Chinese_PRC_CI_AS NULL ,
[title_id] [int] IDENTITY (1, 1) primary key NOT NULL
) ON [PRIMARY]
GO
--我建立了两个表,我希望在对sale表进行插入、更新、删除操作时,
--将sale表中的记录同步到表newsale于是我采用了为sale表添加触发器,代码如下
if exists(select name from sysobjects where name='sale_Update' and type='TR')
drop trigger sale_Update
go
if exists(select name from sysobjects where name='sale_insert' and type='TR')
drop trigger sale_insert
go
if exists(select name from sysobjects where name='sale_delete' and type='TR')
drop trigger sale_delete
go
/*******************insert触发器***************************/
CREATE TRIGGER sale_insert ON dbo.sale
FOR INSERT
AS
set identity_insert newsale on
--delete [dbo].[newsale] where title_id in (select title_id from inserted)
insert newsale(
[stor_id],
[ord_num],
[date],
[qty] ,
[payterms],
[title_id]
)
--select * from sale where [title_id] not in (select [title_id] from newsale)
select * from inserted
--select * from inserted
--delete [dbo].[newsale] where title_id in (select title_id from inserted)
set identity_insert newsale off
go
/*******************delete触发器***************************/
CREATE TRIGGER sale_delete ON dbo.sale
FOR delete
AS
delete [dbo].[newsale] where title_id in (select title_id from deleted)
go
/*******************update触发器***************************/
CREATE TRIGGER sale_Update
ON sale
FOR UPDATE
AS
set identity_insert newsale on
IF (UPDATE(stor_id)
or UPDATE(payterms)
or UPDATE(qty)
or UPDATE(date)
or UPDATE(ord_num))
begin
--select * from inserted
delete newsale where title_id in (select title_id from inserted)
insert newsale(
[stor_id],
[ord_num],
[date],
[qty] ,
[payterms],
[title_id]
) select * from inserted
--select * from sale
end
set identity_insert newsale off
go
- 关于以数据库中两结构相同表的同步(用触发器)
- 用游标实现为数据库的每个用户表添加结构相同的触发器
- 从2个不同的数据库中同步表结构相同表的数据
- 使用PL/SQL Developer来同步两个数据库中相同表的结构
- 使用PL/SQL Developer来同步两个数据库中相同表的结构
- 同步相同结构数据库数据
- 关于两组相同的radio同步问题
- oracle触发器(当插入或删除或修改一张表A时,能把新数据同步到另一张结构相同的表B里面)
- 查找数据库中具有相同表结构的表
- 触发器 两表同步更新
- Oracle 触发器两表同步
- [sql server] sql2005 通过 DDL触发器同步数据库表结构
- 两张结构相同的表对比数据。
- oracle合并两张结构相同的表
- 比较相同结构的两表中的数据差异
- 简单的两库同步触发器
- 在Oracle数据库中查询所有触发器、存储过程、视图、表的两种方法
- 在Oracle数据库中查询所有触发器、存储过程、视图、表的两种方法
- GCC 中文手册
- 词法分析自动生成器CScanGenner的设计与实现
- SQL SERVER 2000 系统表的应用
- Delphi 2005 update_3 还有bug没改
- 开天辟地第一篇! (js对象object)
- 关于以数据库中两结构相同表的同步(用触发器)
- C++编程准则(1)
- 怎么样在客户端修改WebGrid的列(Column)的标题
- Emacs常用命令速查(转自ChinaUnix.net)
- 怎么在WebGrid中提供删除确认提示
- JAVA代码编写的30条建议
- 你对我热情,我就喜欢你
- IE编程技巧转载
- 网络售前工程师必须懂的专业术语