0
你們看到我處理以下存儲過程中的錯誤的方式有任何問題嗎?如果我做錯了任何事情,我仍然對SQL不夠了解。謝謝:)SQL事務錯誤處理
CREATE PROCEDURE [dbo].[UserAccounts_Create]
@username varchar(255),
@email varchar(255),
@password nvarchar(127),
@id bigint OUTPUT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
SET XACT_ABORT ON;
DECLARE @now smalldatetime
SET @now = GETDATE()
DECLARE @userId bigint
DECLARE @transactionSuccess bit
SET @transactionSuccess = 0
BEGIN TRANSACTION
INSERT INTO UserAccounts([username], [email], [password], [createDate], [lastLoginDate])
VALUES (@username, @email, @password, @now, @now)
SET @userId = SCOPE_IDENTITY()
INSERT INTO Users([id], [firstName], [lastName])
VALUES (@userId, '', '')
SET @transactionSuccess = 1;
COMMIT TRANSACTION
IF (@transactionSuccess = 1)
RETURN 0; -- Success.
ELSE
RETURN 1; -- Some unknown error occured.
END
使用try-catch塊 – Adeel 2010-02-18 05:31:29