如何加锁来控制查询结果的准确性?
在一个存储过程里面语句
begin tran
detele from temp_a
insert into temp_a select * from A
delete from temp_b
insert into temp_b select * from B
select from temp_a,temp_b
if(@@error> 0)
rollback
else
commit
这样是否能够保证每次查询结果的正确,因为之前的情况是,如果两个应用程序同时执行的话,一个查的时候另一个刚刚做完删除操作,这样就会找不到结果。
应该如何避免呢?
------解决方案--------------------begin tran
detele from temp_a
insert into temp_a select * from A
if(@@error> 0)
rollback
else
commit
begin tran
delete from temp_b
insert into temp_b select * from B
if(@@error> 0)
rollback
else
commit
select from temp_a,temp_b
------解决方案--------------------关注,学习
------解决方案--------------------DECLARE @intError int
SET @intError=0
BEGIN TRAN
DELETE FROM temp_a
SET @intError = COALESCE(NULLIF(@intError, 0), @@ERROR)
INSERT INTO temp_a SELECT * FROM A
SET @intError = COALESCE(NULLIF(@intError, 0), @@ERROR)
DELETE FROM temp_b
SET @intError = COALESCE(NULLIF(@intError, 0), @@ERROR)
INSERT INTO temp_b SELECT * FROM B
SET @intError = COALESCE(NULLIF(@intError, 0), @@ERROR)
IF(@intError <> 0) ROLLBACK TRAN
ELSE COMMIT TRAN
SELECT * FROM temp_a,temp_b
------解决方案--------------------up
------解决方案--------------------学习
------解决方案--------------------学习
------解决方案--------------------你在事物中调用 p1,p2就可以了
DELETE 和INSERT sqlserver会自动使用排他锁的