用于数据库抛出具体异常信息给程序,示例: BEGIN TRY /* RAISERROR ('Error raised in TRY block.', -- Message text. 16, -- Severity. 1 -- State. ); */ DECLARE @x INT=9; DECLARE @y INT =0; SELECT @x/@y; END TRY BEGIN CATCH DECLARE @ErrorMessage NVARCHAR(4000); DECLARE @ErrorSeverity INT; DECLARE @ErrorState INT;
SELECT @ErrorMessage = ERROR_MESSAGE(), @ErrorSeverity = ERROR_SEVERITY(), @ErrorState = ERROR_STATE();
RAISERROR (@ErrorMessage, -- Message text. @ErrorSeverity, -- Severity. @ErrorState -- State. ); END CATCH; 执行后返回信息:
来一篇参考文章:http://www.cnblogs.com/xugang/archive/2011/04/09/2010216.html ----------------------------------------------华丽分割线-------------------------------------------------------------- 再上一个实际工作中的SQL: ALTER PROCEDURE [dbo].[UP_ERP_Customer_AddRelations] ( @CusId BIGINT, @UserId VARCHAR(100), @OpenKey VARCHAR(100), @DeviceId VARCHAR(100), @IMSI VARCHAR(100), @AppFrom VARCHAR(100), @AppVer VARCHAR(100), @AppType TINYINT, @SourceId INT, @LoginType VARCHAR(100),
@UID VARCHAR(100), @email VARCHAR(100) ) AS BEGIN SET NOCOUNT ON; DECLARE @flag INT; BEGIN TRY BEGIN TRAN; IF(OBJECT_ID('tempDB..#temp_AddRelations') IS NOT NULL) BEGIN DROP TABLE #temp_AddRelations; END CREATE TABLE #temp_AddRelations ( CusId BIGINT, UserId VARCHAR(100), OpenKey VARCHAR(100), DeviceId VARCHAR(100), IMSI VARCHAR(100), AppFrom VARCHAR(100), AppVer VARCHAR(100), AppType VARCHAR(50), SourceId VARCHAR(10), LoginType VARCHAR(100), PartnerEmail VARCHAR(100), [UID] VARCHAR(100) ) INSERT INTO #temp_AddRelations SELECT CusId=@CusId, UserId =@UserId, OpenKey =@OpenKey, DeviceId =@DeviceId, IMSI =@IMSI, AppFrom =@AppFrom, AppVer =@AppVer, AppType =@AppType, SourceId =@SourceId, LoginType =@LoginType, PartnerEmail=@email, [UID]=@UID
--操作表ThirdPartyUser DECLARE @source INT SELECT @source= CASE @LoginType WHEN 'qq' THEN 9 WHEN 'aly' THEN 1 ELSE 0 END MERGE INTO YinTaiCustomer.dbo.ThirdPartyUser th USING( SELECT CusId,UserId,OpenKey,DeviceId,IMSI,AppFrom,AppVer,AppType,SourceId,LoginType,PartnerEmail,[UID] FROM #temp_AddRelations ) tt ON tt.OpenKey=th.PartnerUserID AND tt.CusId=th.CustomerID WHEN NOT MATCHED THEN INSERT VALUES( tt.CusId, tt.OpenKey, tt.PartnerEmail, @source, '','','',0,GETDATE(),0,GETDATE(),'','' ); ----操作表customerDeviceValidate IF(@UID IS NOT NULL AND @UID<>'') BEGIN MERGE INTO YinTaiCustomer.dbo.CustomerDeviceValidate d USING( SELECT CusId,UserId,OpenKey,DeviceId,IMSI,AppFrom,AppVer,AppType,SourceId,LoginType,PartnerEmail,[UID] FROM #temp_AddRelations ) td ON td.[UID]=d.[UID] AND td.CusId=d.CustomerID WHEN NOT MATCHED THEN INSERT VALUES( td.[UID], td.CusId, td.IMSI, td.DeviceId, td.AppFrom, td.SourceId, GETDATE(), td.AppType, td.AppVer ); END --操作表UnionRegister MERGE INTO YinTaiCustomer..UnionRegister u USING( SELECT CusId,UserId,OpenKey,DeviceId,IMSI,AppFrom,AppVer,AppType,SourceId,LoginType,PartnerEmail,[UID] FROM #temp_AddRelations ) ut ON ut.PartnerEmail=u.NewAccount WHEN NOT MATCHED THEN INSERT VALUES ( NULL, ut.PartnerEmail, NULL, ut.SourceId, NULL, GETDATE(), NULL, NULL, NULL, NULL ); SET @flag=1; COMMIT TRAN; END TRY BEGIN CATCH SET @flag=0; ROLLBACK TRAN; /*定义要返回的异常信息变量*/ DECLARE @ErrorMessage NVARCHAR(4000); DECLARE @ErrorSeverity INT; DECLARE @ErrorState INT; /*收集可能出现的异常信息*/ SELECT @ErrorMessage = ERROR_MESSAGE(), @ErrorSeverity = ERROR_SEVERITY(), @ErrorState = ERROR_STATE(); /*抛出异常*/ RAISERROR (@ErrorMessage, -- Message text. @ErrorSeverity, -- Severity. @ErrorState -- State. ); END CATCH RETURN @flag; END 如果调用此存储过程中出现异常,就能再程序中CATCH到信息:
|