我从a表里查个信息,然后根据这个判断往b表里插入数据,在更改a表的内容,表明已经往b里插过数据了
怎么保证在我查询a表之后,b表插入数据之前,没有别的用户往b里插入数据
我担心我查完a表,有其他用户正好往b里插入数据了,结果我也插入,引起错误
------解决方案--------------------
你在A表设置一个列,是否导出,1为导出,2为没有
在导出的同时更新这一列就OK了,然后导出的条件在加一个 判断列 is not nul
或者还有一个方式就是
用 left join 的方法
insert into select 2个表A 表 LEFT JOIN B 表 where B表 id列 is null 就好了
这样就不会出现重复的
------解决方案--------------------
insert into A
select col1,col2,..from B
where not exisits(select 1 from A)
------解决方案--------------------
not exisits(select 1 from A where col1=b.col1 ....)
------解决方案--------------------
1.设事务隔离等级为serializable.
2.加事务.
3.事务开始时查询b表,加hint: (xlock) --> 此时其他进程就无法插入数据到b表了.
------解决方案--------------------
insert into a
select 插入内容 from B left join A
on A.id=B.id where a.id is null
就好了
给你一个列子
insert into 货运费用表(货运日期,货运酒店,id_酒店,id_负责人车辆号,快件否)
select a.日期-1,a.酒店名称,a.id_酒店,a.id_负责人车辆号,'0' from (select t.日期,t.酒店名称,t.id_负责人车辆号,t.id_酒店 from (select 订单编号表头.日期,订单表.酒店名称,货运责任表.id_负责人车辆号,订单表.id_酒店 from 订单编号表头,订单表,货运责任表,厨房表,酒店表"
where 订单编号表头.id_编号=订单表.id_编号 and 订单表.id_厨房=厨房表.id_厨房 and 厨房表.id_负责人车辆号=货运责任表.id_负责人车辆号 and 酒店表.id_酒店=厨房表.id_酒店 and 酒店表.地域='外地' and 货运责任表.运费否=1 and 日期 between '" & 服务器时间 - 7 & "' and '" & 服务器时间 & "'
group by 日期,订单表.酒店名称,货运责任表.id_负责人车辆号,订单表.id_酒店) as t left join 货运费用表 on 货运费用表.id_酒店=t.id_酒店 and 货运费用表.货运日期+1=t.日期 where id_货运费用 is null) as a