1 use hrmis 2 set ANSI_NULLS ON 3 set QUOTED_IDENTIFIER ON 4 GO 5 Alter Trigger [ dbo ] . [ Tg_k20_month ] on [ dbo ] . [ k20 ] 6 FOR insert , update 7 AS 8 if ( update (k2006)) or ( update (k2007)) 9 BEGIN 10 declare @msg nvarchar ( 80 ), @begTIme datetime , @endTime datetime ; 11 declare @Year int , @Month int ; 12 -- 获取请假的年份及月份 13 select @Year = Year (k2006), 14 @Month = Case 15 When Day (k2006) > 25 Then Month (k2006) + 1 16 else Month (k2006) 17 End 18 From Inserted; 19 -- 计算请假单据的开始时间所在的月份 20 Select @endTime = Case 21 When @Month < 10 Then Convert ( varchar ( 4 ), @Year ) + ' 0 ' + Convert ( varchar ( 1 ), @Month ) + ' 25 ' 22 else Convert ( datetime , Convert ( varchar ( 4 ), @Year ) + Convert ( varchar ( 1 ), @Month ) + ' 25 ' ) 23 End 24 -- ,@begTime = Case 25 -- When @Month < 11 Then Convert(varchar(4),@Year)+'0'+Convert(varchar(1),@Month-1)+'26' 26 -- else Convert(varchar(4),@Year)+Convert(varchar(1),@Month-1)+'26' 27 -- End 28 if exists ( Select A0188 From Inserted Where k2007 > @endTime ) 29 Begin 30 Select @msg = Convert ( varchar ( 5 ),k2006, 110 ) + ' 到 ' + Convert ( varchar ( 5 ),k2007, 110 ) + 31 ' 此段时间内有跨月现象,请按月份重新录入! ' from Inserted 32 raiserror ( @msg , 16 , 1 ) 33 rollback transaction 34 return 35 End 36 End 37 return
有一个问题,是后来发现的,上面SQL语句中有一段:
1 if exists ( Select A0188 From Inserted Where k2007 > @endTime ) 2 Begin 3 Select @msg = Convert ( varchar ( 5 ),k2006, 110 ) + ' 到 ' + Convert ( varchar ( 5 ),k2007, 110 ) + 4 ' 此段时间内有跨月现象,请按月份重新录入! ' from Inserted 5 raiserror ( @msg , 16 , 1 ) 6 rollback transaction 7 return 8 End
其中raiserror(@msg,16,1)
是抛出一个错误级别为16的异常信息,这个信息能否被C#里的catch语句捕足?具体怎么捕捉?由于今天是在转我QQ空间的资料,所以暂时写一点,日后我看到的话,回来再补回。