1. exec up_testExceptionThrown
    2. SELECT * FROM TestExceptionThrownTB
    1. USE [master]
    2. IF EXISTS (SELECT * FROM SYS.DATABASES WHERE NAME ='TestExceptionThrownDB')
    3. BEGIN
    4. DROP DATABASE TestExceptionThrownDB
    5. END
    6. GO
    7. CREATE DATABASE TestExceptionThrownDB
    8. GO
    9. USE TestExceptionThrownDB
    10. GO
    11. IF EXISTS (SELECT * FROM SYS.OBJECTS WHERE NAME ='TestExceptionThrownTB')
    12. BEGIN
    13. DROP TABLE TestExceptionThrownTB
    14. END
    15. CREATE TABLE TestExceptionThrownTB(
    16. [Id] INT PRIMARY KEY IDENTITY(1,1),
    17. [Name] VARCHAR(25)
    18. )
    19. GO
    20. IF OBJECT_ID('up_testExceptionThrown1') IS NOT NULL
    21. BEGIN
    22. DROP PROCEDURE up_testExceptionThrown1
    23. END
    24. go
    25. CREATE PROC [dbo].up_testExceptionThrown1
    26. AS
    27. /***********************************************************************************
    28. 作者:向以胜
    29. 日期:2020-04-05
    30. 功能:测试异常抛出
    31. exec up_testExceptionThrown1
    32. ***********************************************************************************/
    33. BEGIN
    34. PRINT 'exec up_testExceptionThrown 1 start';
    35. IF EXISTS (SELECT * FROM TestExceptionThrownTB WHERE [Name]='test')
    36. BEGIN
    37. RAISERROR ('存在',16,1);RETURN;
    38. END
    39. ELSE
    40. BEGIN
    41. RAISERROR ('不存在',16,1);RETURN;
    42. END
    43. PRINT 'exec up_testExceptionThrown 1 end';
    44. END
    45. GO
    46. IF OBJECT_ID('up_testExceptionThrown') IS NOT NULL
    47. BEGIN
    48. DROP PROCEDURE up_testExceptionThrown
    49. END
    50. go
    51. CREATE PROC [dbo].up_testExceptionThrown
    52. AS
    53. /***********************************************************************************
    54. 作者:向以胜
    55. 日期:2020-04-05
    56. 功能:测试异常抛出
    57. exec up_testExceptionThrown
    58. SELECT * FROM TestExceptionThrownTB
    59. ***********************************************************************************/
    60. BEGIN
    61. DECLARE @Errmsg NVARCHAR(300)
    62. BEGIN TRY
    63. BEGIN TRANSACTION
    64. PRINT 'exec up_testExceptionThrown start';
    65. INSERT INTO TestExceptionThrownTB VALUES('test');
    66. EXEC up_testExceptionThrown1
    67. PRINT 'exec up_testExceptionThrown end';
    68. COMMIT TRANSACTION
    69. END TRY
    70. BEGIN CATCH
    71. SELECT @Errmsg=ERROR_MESSAGE();
    72. GOTO Error
    73. END CATCH
    74. ERROR:
    75. ROLLBACK TRANSACTION
    76. PRINT 'ROLLBACK TRANSACTION Errmsg:'+ @Errmsg
    77. END