拍卖功能涉及到多表操作的问题 [全部放在存储过程中处理]
情景描述:
在线拍卖,客户浏览拍卖的商品,对商品进行出价时的一系列的操作
涉及的表:商品表goods 、用户表users 、订单表orders
--1 用户表 create table Users ( uId int identity(1,1) primary key, loginId nvarchar(50) not null, loginPwd nvarchar(50) not null, name nvarchar(50), address nvarchar(200), phone nvarchar(100), mail nvarchar(100), uRoleId int, uStateId int, uMoney money,--可用资金 freezeMoney money, --冻结资金 uAccess int, uPic nvarchar(50) ) --2.goods 商品信息 create table Goods ( gId int identity(1,1) primary key, gName nvarchar(50), gDescription nvarchar(max), gPic nvarchar(100), gStartPrice money, gCurrentPrice money, gCount int, --出价次数 gStartTime datetime, gEndTime datetime, gUserId int, categoryId int, clicks int ) --3.orders 订单表 create table Orders ( orderId nvarchar(50), orderDate datetime, userId int, --竞拍客户ID totalPrice money, --冻结金额 postAddress nvarchar(255), oState int, gId int --商品编号 )
数据库:sql server2008
代码的需要实现的操作:
1:根据商品的编号,在订单表orders中查找是否有关于该商品已拍下的订单;
2:根据1得到的结果:没有记录,说明为“第一次投标“,只需要进行:①订单的生成、②商品竞价次数的加一、③该客服可用资金的减少,冻结资金的添加;
有记录,只有可能为一条[一旦有一个客户投标,上条记录就会改变],这里开启事务Begin Transaction :①获得上一条 竞价记录 的 userID 和 被冻结的金额、
②将上一条 记录 状态变成 1 ③ 将该userid 的可用金额 + 冻结的金额 然后 将 冻结金额 - 购买此商品而冻结的金额 ;
这几项语句的执行过程中@error 来记录@@error 的错误信息 ,无误 commit transaction 后续的操作和【没有记录时一样】;
-- ================================================ -- Template generated from Template Explorer using: -- Create Procedure (New Menu).SQL -- -- Use the Specify Values for Template Parameters -- command (Ctrl-Shift-M) to fill in the parameter -- values below. -- -- This block of comments will not be included in -- the definition of the procedure. -- ================================================ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ============================================= -- Author: ancient_sir -- Create date: 2012-2-3 -- Description: 商品竞拍 -- ============================================= if exists(select * from sysobjects where [name]='usp_auction') drop proc usp_auction go create PROCEDURE usp_auction --参数 商品编号 最高价格 影响行数 --orderId, orderDate, userId, totalPrice, postAddress, oState, gid @orderId nvarchar(50), @orderDate datetime, @userId int, @totalPrice decimal, @postAddress nvarchar(255), @oState int, @gId int, @msg nvarchar(50) output AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; -- declare @id int,@uId int,@uMoney decimal,@error int set @error = 0 select @id=userId from orders where gId = @gId and totalPrice < @totalPrice and oState = 0 if(@id is null) begin insert into orders ( orderId, orderDate, userId, totalPrice, postAddress, oState, gid ) values (@orderId, @orderDate, @userId, @totalPrice, @postAddress, @oState, @gid) --本账户数据操作 update Goods set gCount = gCount + 1 where gid = @gId ----出价次数 +1 update users set users.umoney = users.umoney - @totalPrice,freezeMoney = freezeMoney+ @totalPrice where uId = @userId set @msg ='第一投标' return end else begin begin transaction --开启事务 --1.获得上一条 竞价记录 的 userID 和 被冻结的金额 select @uId = userId ,@uMoney = orders.totalPrice from orders where userid = @id --2.将上一条 记录 状态变成1 update orders set oState = 1 where userId = @uId set @error = @error + @@error --3.将该userid 的可用金额 + 冻结的金额 然后 将 冻结金额 - 购买此商品而冻结的金额 update users set users.umoney = users.umoney+ @uMoney , freezeMoney = freezeMoney - @uMoney where uId = @uId set @error = @error + @@error if(@error=0) begin commit transaction --本账户数据操作 insert into orders ( orderId, orderDate, userId, totalPrice, postAddress, oState, gid ) values (@orderId, @orderDate, @userId, @totalPrice, @postAddress, @oState, @gid) --出价次数 +1 update Goods set gCount = gCount + 1 where gid = @gId -- update users set users.umoney = users.umoney - @totalPrice,freezeMoney = freezeMoney+ @totalPrice where uId = @userId set @msg='已经有人投标了' end else begin rollback transaction raiserror('竞拍出错',18,1) set @msg='error' end end set NOCOUNT OFF; END GO
测试数据:
declare @msg nvarchar(50) EXEC usp_auction '4','2012-2-3',1,100,'22',0,4,@msg output select @msg
一个简单的事例,希望给需要的朋友,同时希望高手指导。。。.net的新手