写一个储发器.
要达到这样的一个效果,有两个数据库A和B中有不同的两个表,表中有相同类似的字段.
其中数据库A中的表form1中的字段名分别为:
[UserName] [varchar] (50) not null,
[Password] [varchar] (8) null,
[FullName] [varchar] (20) null
其中数据库B中的表form2中的字段名分别为:
[UserName] [varchar] (100) not null,
[Password] [varchar] (8) null,
[FullName] [varchar] (30) null
现在就是能实现这样的一个功能.当向数据库A中的表form1中不管是插入,删除,修改里面的信息,那么数据库B中的表form1也跟着改变,也就是实现一个数据同步的功能.
答案:
CREATE TRIGGER UserABTableInsert ON A..form1 '添加
FOR INSERT
AS
declare @username nvarchar(255)
@username=select username from inserted
insert into B..form2 select * from A.form1 where username=@username
CREATE TRIGGER UserABTableInsert ON A..form1 '删除
FOR DELETE
AS
declare @username nvarchar(255)
@username=select username from deleted
delete from B..form2 where Name=@username
CREATE TRIGGER UserABTableInsert ON A..form1 '修改
FOR UPDATE
AS
declare @oldname nvarchar(255),@username nvarchar(255),@password nvarchar(255),@fullname nvarchar(255)
@oldname=select username from deleted
@username=select username from inserted
@password=select password from inserted
@fullname=select fullname from inserted
update B..form2 set Name=@username,pd=@password,fname=@fullname where username=@oldname