ALTER Proc AddPurcaseInStoreRoom
@PurcaseInStoreRoomNo Char(30),
@InDate SmallDatetime,
@ProviderID Int,
@ArrivePurchaseInspectID Int,
@ArrivePurchaseInspectNo Char(30),
@Remark Varchar(100),
@TotalAmount Money,
@InputEmployeeID Int,
@AddDetailSql Varchar(500)
As
Declare @newId int, @State int, @tmpPid int, @tmpSid int, @tmpPrice money, @Pno Varchar(30), @Count int, @tmpstr Varchar(500)
Set @State = 0
Begin Tran
--插入主表
Insert Into tPurcaseInStoreRoom (PurcaseInStoreRoomNo, InDate, ProviderID, ArrivePurchaseInspectID, ArrivePurchaseInspectNo, Remark, IsSettled, TotalAmount, SettledAmount, RealPayAmount, InputEmployeeID, InputDateTime)
Values (@PurcaseInStoreRoomNo, @InDate, @ProviderID, @ArrivePurchaseInspectID, @ArrivePurchaseInspectNo, @Remark, 0, @TotalAmount, 0, 0, @InputEmployeeID, GetDate())
If @@Error <> 0
Begin
Rollback Tran
Return -1
End
Set @newId = @@Identity
--插入从表
Set @tmpstr = Convert(Varchar, @newId)
Set @tmpstr = Replace(@AddDetailSql, '-Id', @tmpstr)
Exec(@tmpstr)
If @@Error <> 0
Begin
Rollback Tran
Return -1
End
--更新商品最新进价以及库存状态
Declare Temp_Cursor Cursor
For
Select ProductID, StoreRoomID, Price, BatchNo, Amount From tPurcaseInStoreRoomDetail Where PurcaseInStoreRoomID = @newId
Open Temp_Cursor
--如果没有任何行则直接退出
If @@Cursor_Rows = 0
Begin
Close Temp_Cursor
Deallocate Temp_Cursor
End
While @State = 0
Begin
Fetch Temp_Cursor Into @tmpPid, @tmpSid, @tmpPrice, @Pno, @Count
Select @State = @@Fetch_Status
--修改最近进价
Update tProduct Set RecCostPrice = @tmpPrice Where ProductID = @tmpPid And RecCostPrice != @tmpPrice
--修改库存信息
--如果该批号商品存在于数据库中则更新
if (Exists(Select StoreRoomID From tStock Where ProductID = @tmpPid And BatchNo = @Pno))
Update tStock Set Amount = Amount + @Count Where ProductID = @tmpPid And BatchNo = @Pno
else
Insert Into tStock (StoreRoomID, ProductID, BatchNo, ProviderID, [ExpireDate], Amount, CostPrice, IsStopSale)
Values (@tmpSid, @tmpPid, @Pno, @ProviderID, GetDate(), @Count, @tmpPrice, 0)
If @@Error <> 0
Begin
Rollback Tran
Close Temp_Cursor
Deallocate Temp_Cursor
Return -1
End
End
Close Temp_Cursor
Deallocate Temp_Cursor
--修改导入状态
Update tArrivePurchaseInspect Set IsCite = 1 Where ArrivePurchaseInspectID = @ArrivePurchaseInspectID
If @@Error <> 0
Begin
Rollback Tran
Return -1
End
Commit Tran
Return @newId
详细解决方案
SQL存储过程事宜-主从表同时插入以及一般性事务处理
热度:71 发布时间:2016-05-05 13:57:32.0
相关解决方案
- Zend Framework 事务处理 有关问题
- java web程序,事务处理,该怎么解决
- java 事务处理?来者不拒,该如何解决
- java 事务处理,该怎么解决
- java如何给数据库加行锁(事务处理)
- oracle 事务处理 注意事项(札记)
- oralce 事务处理,调用这个方法,程序就死了。该怎么解决
- 如果存储过程中进行【事务处理】,是不是每一步操作,都要判断@@error=0 呀?该怎么解决
- Zend Framework 事务处理 有关问题
- SQLite学习笔记(7)&&事务处理
- Android SQLite 创造多表及多表查询 事务处理
- 基于Eclipse Maven的Spring4/Spring-MVC/Hibernate4整合之十:Spring mvc hibernate 事务处理(回滚)
- SQL学习笔记 - 12.事务处理
- JDBC--事务处理
- T-Sql学习(14) - 事务处理
- laravel 事务处理
- Spring Boot数据访问:事务处理