表Acreate table A(
WorkID int identity(1,1) primary key not null,
AgentID varchar(36) null,
AgentName nvarchar(64) null,
WorkStateID varchar(4) null,
Summary varchar(100) null,
Worklog varchar(500) null,
CreateTime datetime null,
UpdateTime datetime null,
Is_MSG varchar(1) null,
SendMSGTime datetime null,
Mobile varchar(50) null,
Callname nvarchar(64) null
)
go
-------------------------------------------------------
15 8c7ec15f 周小伟 NULL 哈哈 个电饭锅体育用途 2012-08-16 17:00:17.200 NULL 1 2012-01-01 22:38:00.000 13555678899 电饭锅
16 8c7ec15f 周小伟 NULL 测试2 测试你懂的 2012-08-16 17:22:43.310 NULL 1 2012-08-21 08:00:00.000
15800596430 张伦铭
表BCREATE TABLE B
(
[ID] [int] IDENTITY(1,1) NOT NULL,
[Mobile] [varchar](50) NULL,
[SendTime] [datetime] NULL,
[AgentID] [varchar](36) NULL,
[Msg] [varchar](500) NULL,
[ReceiveName] [varchar](64) NULL,
[SedMsgID] [decimal](18, 0) NULL
)
-------------------------------------
1 13877688909 2012-08-20 09:20:00.000 8c7ec15f aassddff zlm NULL
我想插入表A的同时,插表B,因为他们的几个字段含义相同
1 A.agentid=B.agentid
2 A.worklog=B.msg
3 A.sendmsgtime=B.sendtime
4 A.mobile=B.mobile
5 A.callname=B.receivename插A表语句
insert into swcl(agentid,agentname,summary,worklog,createtime,is_msg,sendmsgtime,mobile,callname) values(@agentid,@agentname,@summary,@worklog,getdate(),@is_msg,@sendmsgtime,@mobile,@callname)
插B表语句
insert into TBL_TimingSendMsg(mobile,sendtime,agentid,msg,receivename) values(@mobile,@sendtime,@agentid,@msg,@receivename)
问题来了,A表是我前台操作的表,有点击事件,我可以知道ID,插得时候没问题,如果更新,删除的话,我A表知道操作哪一条,B表就完蛋了,我不知道ID的,因为B表是后台表,WEBSERVICE自动扫描的表。我查了下解决方案,有的说用触发器,但是不行啊,我B表一旦WEBSERVICE扫描到了数据,程序会把B的数据删掉的。
也就是说A表有数据,B表也要有数据,A表更新B表更新,A表删除B表删除,但B表删除A表依旧在
WorkID int identity(1,1) primary key not null,
AgentID varchar(36) null,
AgentName nvarchar(64) null,
WorkStateID varchar(4) null,
Summary varchar(100) null,
Worklog varchar(500) null,
CreateTime datetime null,
UpdateTime datetime null,
Is_MSG varchar(1) null,
SendMSGTime datetime null,
Mobile varchar(50) null,
Callname nvarchar(64) null
)
go
-------------------------------------------------------
15 8c7ec15f 周小伟 NULL 哈哈 个电饭锅体育用途 2012-08-16 17:00:17.200 NULL 1 2012-01-01 22:38:00.000 13555678899 电饭锅
16 8c7ec15f 周小伟 NULL 测试2 测试你懂的 2012-08-16 17:22:43.310 NULL 1 2012-08-21 08:00:00.000
15800596430 张伦铭
表BCREATE TABLE B
(
[ID] [int] IDENTITY(1,1) NOT NULL,
[Mobile] [varchar](50) NULL,
[SendTime] [datetime] NULL,
[AgentID] [varchar](36) NULL,
[Msg] [varchar](500) NULL,
[ReceiveName] [varchar](64) NULL,
[SedMsgID] [decimal](18, 0) NULL
)
-------------------------------------
1 13877688909 2012-08-20 09:20:00.000 8c7ec15f aassddff zlm NULL
我想插入表A的同时,插表B,因为他们的几个字段含义相同
1 A.agentid=B.agentid
2 A.worklog=B.msg
3 A.sendmsgtime=B.sendtime
4 A.mobile=B.mobile
5 A.callname=B.receivename插A表语句
insert into swcl(agentid,agentname,summary,worklog,createtime,is_msg,sendmsgtime,mobile,callname) values(@agentid,@agentname,@summary,@worklog,getdate(),@is_msg,@sendmsgtime,@mobile,@callname)
插B表语句
insert into TBL_TimingSendMsg(mobile,sendtime,agentid,msg,receivename) values(@mobile,@sendtime,@agentid,@msg,@receivename)
问题来了,A表是我前台操作的表,有点击事件,我可以知道ID,插得时候没问题,如果更新,删除的话,我A表知道操作哪一条,B表就完蛋了,我不知道ID的,因为B表是后台表,WEBSERVICE自动扫描的表。我查了下解决方案,有的说用触发器,但是不行啊,我B表一旦WEBSERVICE扫描到了数据,程序会把B的数据删掉的。
也就是说A表有数据,B表也要有数据,A表更新B表更新,A表删除B表删除,但B表删除A表依旧在
你这个要求就用触发器就可以了,有什么问题么?
那么A表有数据,B表也要有数据,A表更新B表更新,A表删除B表删除。
这个时候你操作b表,A表不会有变化的。如果你操作b表,a表也要有变化,那么在b表后面同样也要写一个存储过程修改a表数据。
触发器还没自己写过,大师给个格式,我依样坏葫芦
插入时候对的,但是更新时候表B不对了
帮我看下,哪里写错了,谢谢
create trigger TwoTableDo on A
for insert,update,delete
AS
delete B where id in (select id from deleted)
insert into B (mobile,sendtime,agentid,msg,receivename) select mobile,sendmsgtime,agentid,worklog,callname from inserted
if @@error<>0
begin
rollback tran
return
end
for insert,update,delete
AS
if exists(select 1 from inserted)
begin
delete B where id in (select id from inserted)
insert into B (mobile,sendtime,agentid,msg,receivename) select mobile,sendmsgtime,agentid,worklog,callname from inserted
--or
-- update B set mobile=i.mobile,sendtime=i.sendmsgtime,agentid=i.agentid,msg=i.worklog,receivename=i.callname
-- from B
-- inner join inserted i
-- on B.id=i.id
end
if exists(select 1 from deleted)
begin
delete B where id in (select id from deleted)
end
if @@error<>0
begin
rollback tran
return
end
这个要求加上那张B.id和A.workid,不是对应关系的,因为对B表做插入动作的还有其他程序,很有可能我insert into A表时候A.workid=10 而与此同时触发了insert into B表的B.id=15