使用TRY CATCH

时间:2017-09-21 19:21:23

标签: sql sql-server tsql

我编写了一个存储过程,它使用加密算法将一行插入表中。该过程正在成功执行,但未添加任何行。

我的代码:

CREATE TABLE dbo.[User]
(
    UserID INT IDENTITY(1,1) NOT NULL,
    Email NVARCHAR(64) NOT NULL,
    PasswordHash BINARY(64) NOT NULL,
    FirstName NVARCHAR(40) NULL,
    LastName NVARCHAR(40) NULL,

    CONSTRAINT [PK_User_UserID] PRIMARY KEY CLUSTERED (UserID ASC)
)

CREATE PROCEDURE dbo.addUser
    @pEmail NVARCHAR(64), 
    @pPassword NVARCHAR(64), 
    @pFirstName NVARCHAR(40) = NULL, 
    @pLastName NVARCHAR(40) = NULL,
    @responseMessage NVARCHAR(250) OUTPUT
AS
BEGIN
    BEGIN TRY
        INSERT INTO dbo.[User] (Email, PasswordHash, FirstName, LastName)
        VALUES(@pEmail, HASHBYTES('SHA2_512', @pPassword), @pFirstName, @pLastName)

        SET @responseMessage = 'Success'
    END TRY
    BEGIN CATCH
        SET @responseMessage = ERROR_MESSAGE() 
    END CATCH
END

DECLARE @responseMessage NVARCHAR(250)
EXEC dbo.addUser
          @pEmail = 'Admin',
          @pPassword = '123',
          @pFirstName = 'Admin',
          @pLastName = 'Administrator',
          @responseMessage=@responseMessage OUTPUT

执行该程序后,表中没有添加任何行。

0 个答案:

没有答案