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存储过程--资料全 SQL存储过程--资料全 SQL存储过程--资料全 SQL存储过程--资料全
VC 操作 SQL Server 主从表
主要介绍了SQLServer存储过程中事务的使用方法,简短的代码带大家更好的学习使用SQLServer存储过程中事务,感兴趣的小伙伴们可以参考一下
原创sql存储过程函数范例,一是为了自己方便查找,今天到公司因为没有我保存的一些范例,一个简单的例子写了半个小时,如果有范例直接套几分钟肯定搞定,所以索性上传到CSDN上,何时何地都能找到我的范例了。...
MySQL SQL高级特性-存储过程-触发器-事务,非常不错,感谢
利用delphi中的内存表来制作主从表,可以替代access或sqlserver
SQL存储过程与事务
一、使用视图meeting将多表关联整合在一起 二、通过Sql语句筛选数据 三、建立和主从表的关联 四、注意在CrystalReport1表中插入子报表的时候,一定要将主从表的关联字段设置好,否则出乱子
使用silverlight显示主从表的方式网上还真不多,找了一个星期都没找到多少资源,今天终于调试成功,拿出来和大家分享。
T-sql编程存储过程T-sql编程存储过程T-sql编程存储过程T-sql编程存储过程T-sql编程存储过程
主要介绍了sql存储过程实例--动态根据表数据复制一个表的数据到另一个表的相关资料,需要的朋友可以参考下
从创建域控,搭建集群到高可用自动转移设置全程图文齐全, 原创!
SQLServer2008R2主从数据库同步
事务处理及回滚.sql 使用全文索引.txt 函数.sql 分布式查询视图.sql 创建和使用游标示例.sql 各种联接类型示范.sql 命名事务.sql 回滚存储点.sql 子查询.sql 存储过程.sql 完全下线.sql 寻找雇员上级....
sql存储过程学习资料--总结的很全的 暴经典的资料啊
SQL数据库----存储过程、触发器
vs2012.net 主从表微软标准示例 初学c#,在网上搜了半天主从表的知识,结果看到微软正宗的主从表示例,仅需一个类就完成了,感受到了.net的强大。窗体、控件全部动态生成。适合初学者看看。 内附Northwind 数据库...
SqlServer数据库字典--表.视图.函数.存储过程.触发器.主键.外键.约束.规则
SQL_Server_存储过程_-_hoojo_-_博客园
什么是数据库存储过程和触发器?它们的作用和使用场景是什么? 什么是数据库视图?它的作用和使用场景是什么? 什么是数据库分表和分库?为什么需要进行分表和分库操作? 什么是数据库锁?列举一些常见的数据库锁...