create proc p_process
@id_购物车 int
as
begin tran --用事务配合下面的排它锁,保证数据一致性
if exists(select * from 购物车表 with(TABLOCKX) --用排它锁,防止多次提交
where id_购物车=@id_购物车)
begin
insert 订单表(id_顾客,交易时间,付款标志)
select distinct 用户名,getdate(),0
from 购物车表 with(TABLOCKX)
where id_购物车=@id_购物车
insert 订单明细表(id_订单,数量,价格)
select @@identity, 价格,数量
from 购物车表 with(TABLOCKX)
where id_购物车=@id_购物车
end
else
begin
raiserror('"%d" 号购物车不存在!',1,16,@id_购物车)
rollback tran
end
create proc p_process
@id_购物车 int
as
begin tran --用事务配合下面的排它锁,保证数据一致性
if exists(select * from 购物车表 with(TABLOCKX) --用排它锁,防止多次提交
where id_购物车=@id_购物车)
begin
insert 订单表(id_顾客,交易时间,付款标志)
select distinct 用户名,getdate(),0
from 购物车表 with(TABLOCKX)
where id_购物车=@id_购物车
insert 订单明细表(id_订单,数量,价格)
select @@identity, 价格,数量
from 购物车表 with(TABLOCKX)
where id_购物车=@id_购物车
delete from 购物车表
where id_购物车=@id_购物车
end
else
begin
raiserror('"%d" 号购物车不存在!',1,16,@id_购物车)
rollback tran
end