日期:2014-05-19  浏览次数:20501 次

取@@ERROR永远都是0????

CREATE   PROCEDURE   dbo.ChkbuyersTime

AS

declare   @i   int,@RA_User   int,@RA_HighPrice   int,@AL_Rate   float,@str   nvarchar(1000),@price   int,@RA_Domain   int,@RA_DomainUser   int,@maxid   int
BEGIN   TRANSACTION   GetDataSet

CREATE   TABLE   #TemporaryTable   (
Row   int   IDENTITY(1,1)   PRIMARY   KEY,
RA_User   int,RA_HighPrice   int,RA_domain   int,RA_DomainUser   int
)
IF   @@ERROR   <>   0
GOTO   ErrorHandler

SET   @str   =   '   INSERT   INTO   #TemporaryTable1   '

SET   @str   =   @str   +     '   SELECT   [RA_User],[RA_HighPrice],[RA_Domain],[RA_DomainUser]   FROM   RivalAct   join   domains   on   (d_id   =   RA_Domain   and   D_user   =   RA_DomainUser)   join   domainsell   on   d_id   =   ds_domain   where   D_SellState   =1   and   DS_TradeType   =   3   and     RA_State   =   1   and   DATEDIFF   (s,DATEADD(d,5,RA_Time),getdate())> =0   '  

EXEC(@str)
IF   @@ERROR   <>   0
GOTO   ErrorHandler

DROP   TABLE   #TemporaryTable
COMMIT   TRANSACTION   GetDataSet
INSERT   INTO   Operate_Info   (Operate_Name,Operate_State,Operate_Time)   VALUES   ( 'ChkbuyersTime ', '成功 ',getdate())
RETURN   0

ErrorHandler:
ROLLBACK   TRANSACTION   GetDataSet
INSERT   INTO   Operate_Info   (Operate_Name,Operate_State,Operate_Time,Operate_Error)   VALUES   ( 'ChkbuyersTime ', '失败 ',getdate(),@@ERROR)
RETURN   @@ERROR
GO


为什么在失败时@@ERROR取出来还是0呢???

------解决方案--------------------
INSERT INTO Operate_Info (Operate_Name,Operate_State,Operate_Time,Operate_Error) VALUES ( 'ChkbuyersTime ', '失败 ',getdate(),@@ERROR)

RETURN @@ERROR

-------------
这个是因为你刚刚执行了插入语句,而且已经成功了,所以@@ERROR为 0 ...
------解决方案--------------------
ErrorHandler:
declare @error int
set @error= @@ERROR
ROLLBACK TRANSACTION GetDataSet
INSERT INTO Operate_Info (Operate_Name,Operate_State,Operate_Time,Operate_Error) VALUES ( 'ChkbuyersTime ', '失败 ',getdate(),@@ERROR)
RETURN @error
------解决方案--------------------
@@ERROR 只返回上一个语句出错后的代码.
------解决方案--------------------
可以將錯誤信息放到一張表pubMessage中保存
然後自定義一下函數GetMsg_fn來調用
CREATE FUNCTION [dbo].[GetMsg_fn](@msgid varchar(5),@msglang varchar(10))
RETURNS nvarchar(200)
AS
BEGIN
DECLARE @msgText nvarchar(200)
declare @ErrStr varchar(200)
set @msgText= ' '
select @msgText=msgText from pubMessage where msgid=@msgid and msglang=@msglang
return @msgText
END

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER Procedure [dbo].[Att_DayoffTypeNewEdt_sp]
@pLang char(10)= ' ',
@pTranType varchar(30)=null,
@DayoffTypeID smallint =0,
@DayoffTypeNo varchar(20) = ' ',
@DayoffTypeName varchar(30) = ' ',