您的位置:首页 > 其它

机房重构(三)--存储过程的调试

2015-09-01 23:26 453 查看
          在机房的重构过程中,我们为了操作两个以上的表一般都用到了事务,写过存储过程但是我们一般都是写好了连上数据库,通过程序来调试,看存储过程是否正确.这样未免太麻烦了.作为一款成熟的软件SQL Server 怎么可能没有调试的机制呢?所以下面就是我的分享.

      让我们找一个存储过程,右击,选择执行存储过程如图

 


      然后会弹出这样的一个窗口,让你来传入参数,参数要与自己定义的相同

 


这里要填写的是我们存储过程中定义的参数,参数的类型要与存储过程中定义的一样,这样才不会出现异常,下面是我的存储过程

CREATE PROCEDURE [dbo].[PROC_SettleAccount]
-- 这里定义参数
@UserID varchar(20)
AS
--这里定义变量
declare @Initialcash money,@consumecash money ,@currentcash money,@addcash money,@cancelcash money,@remaincash money,
@date date,@time time,@oldcurrentmoney money
BEGIN
tran
select @Initialcash=SUM(initialcash),@consumecash=SUM(consumecash),@currentcash=SUM(currentcash) from Card_info where IsCheck='未结账'
select @addcash=SUM(addcash) from Recharge_info where IsCheck='未结账'
select @cancelcash=SUM(cancelcash) from CancelCard_info where IsCheck='未结账'
select top 1 @oldcurrentmoney=currentcash from DailyBill_info order by BillNo desc
select @remaincash=remaincash from DailyBill_info
if(@remaincash is null)
set @remaincash=0
else
set @remaincash=@oldcurrentmoney
insert into DailyBill_info (RemainCash,RegisterCash,RechargeCash,ConsumeCash,CancelCardCash,CurrentCash,Date,time,Head)
values(@remaincash,@Initialcash,@addcash,@consumecash,@cancelcash,@currentcash,GETDATE(),GETDATE(),@UserID)
update CancelCard_info set IsCheck='结账' where IsCheck='未结账'
update Recharge_info set IsCheck='结账' where IsCheck='未结账'
update Card_info set IsCheck='结账' where IsCheck='未结账'
if @@ERROR<>0
begin
rollback tran
return 0
end
else
begin
commit tran
return 1
END


 

      返回值为1,则证明我的存储过程中事务已经执行完,进行了相应的修改,如果我们的存储过程有错误,那么就会提示出错误信息和位置,方便我们的调试.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: