返回最后執(zhí)行的 Transact-SQL 語(yǔ)句的錯(cuò)誤代碼。
@@ERROR
integer
當(dāng) Microsoft® SQL Server™ 完成 Transact-SQL 語(yǔ)句的執(zhí)行時(shí),如果語(yǔ)句執(zhí)行成功,則 @@ERROR 設(shè)置為 0。若出現(xiàn)一個(gè)錯(cuò)誤,則返回一條錯(cuò)誤信息。@@ERROR 返回此錯(cuò)誤信息代碼,直到另一條 Transact-SQL 語(yǔ)句被執(zhí)行。您可以在 sysmessages 系統(tǒng)表中查看與 @@ERROR 錯(cuò)誤代碼相關(guān)的文本信息。
由于 @@ERROR 在每一條語(yǔ)句執(zhí)行后被清除并且重置,應(yīng)在語(yǔ)句驗(yàn)證后立即檢查它,或?qū)⑵浔4娴揭粋€(gè)局部變量中以備事后查看。
下面的示例用 @@ERROR 在一個(gè) UPDATE 語(yǔ)句中檢測(cè)限制檢查沖突(錯(cuò)誤 #547)。
USE pubs
GO
UPDATE authors SET au_id = '172 32 1176'
WHERE au_id = "172-32-1176"
IF @@ERROR = 547
print "A check constraint violation occurred"
在此示例中,IF...ELSE 語(yǔ)句在存儲(chǔ)過(guò)程中的 INSERT 語(yǔ)句后檢測(cè) @@ERROR。@@ERROR 變量的值將決定傳給調(diào)用程序的返回值,以指示此過(guò)程的成功與失敗。
USE pubs
GO
-- Create the procedure.
CREATE PROCEDURE add_author
@au_id varchar(11),@au_lname varchar(40),
@au_fname varchar(20),@phone char(12),
@address varchar(40) = NULL,@city varchar(20) = NULL,
@state char(2) = NULL,@zip char(5) = NULL,
@contract bit = NULL
AS
-- Execute the INSERT statement.
INSERT INTO authors
(au_id, au_lname, au_fname, phone, address,
city, state, zip, contract) values
(@au_id,@au_lname,@au_fname,@phone,@address,
@city,@state,@zip,@contract)
-- Test the error value.
IF @@ERROR <> 0
BEGIN
-- Return 99 to the calling program to indicate failure.
PRINT "An error occurred loading the new author information"
RETURN(99)
END
ELSE
BEGIN
-- Return 0 to the calling program to indicate success.
PRINT "The new author information has been loaded"
RETURN(0)
END
GO
下面的示例取決于 INSERT 和 DELETE 語(yǔ)句的成功操作。局部變量在兩條語(yǔ)句后均被設(shè)置為 @@ERROR 的值,并且用于此操作的共享錯(cuò)誤處理例程中。
USE pubs
GO
DECLARE @del_error int, @ins_error int
-- Start a transaction.
BEGIN TRAN
-- Execute the DELETE statement.
DELETE authors
WHERE au_id = '409-56-7088'
-- Set a variable to the error value for
-- the DELETE statement.
SELECT @del_error = @@ERROR
-- Execute the INSERT statement.
INSERT authors
VALUES('409-56-7008', 'Bennet', 'Abraham', '415 658-9932',
'6223 Bateman St.', 'Berkeley', 'CA', '94705', 1)
-- Set a variable to the error value for
-- the INSERT statement.
SELECT @ins_error = @@ERROR
-- Test the error values.
IF @del_error = 0 AND @ins_error = 0
BEGIN
-- Success. Commit the transaction.
PRINT "The author information has been replaced"
COMMIT TRAN
END
ELSE
BEGIN
-- An error occurred. Indicate which operation(s) failed
-- and roll back the transaction.
IF @del_error <> 0
PRINT "An error occurred during execution of the DELETE
statement."
IF @ins_error <> 0
PRINT "An error occurred during execution of the INSERT
statement."
ROLLBACK TRAN
END
GO
下面的示例用 @@ERROR 和 @@ROWCOUNT 驗(yàn)證一條 UPDATE 語(yǔ)句的操作。為任何可能出現(xiàn)的錯(cuò)誤而檢驗(yàn) @@ERROR 的值,而用 @@ROWCOUNT 保證更新已成功應(yīng)用于表中的某行。
USE pubs
GO
CREATE PROCEDURE change_publisher
@title_id tid,
@new_pub_id char(4)
AS
-- Declare variables used in error checking.
DECLARE @error_var int, @rowcount_var int
-- Execute the UPDATE statement.
UPDATE titles SET pub_id = @new_pub_id
WHERE title_id = @title_id
-- Save the @@ERROR and @@ROWCOUNT values in local
-- variables before they are cleared.
SELECT @error_var = @@ERROR, @rowcount_var = @@ROWCOUNT
-- Check for errors. If an invalid @new_pub_id was specified
-- the UPDATE statement returns a foreign-key violation error #547.
IF @error_var <> 0
BEGIN
IF @error_var = 547
BEGIN
PRINT "ERROR: Invalid ID specified for new publisher"
RETURN(1)
END
ELSE
BEGIN
PRINT "ERROR: Unhandled error occurred"
RETURN(2)
END
END
-- Check the rowcount. @rowcount_var is set to 0
-- if an invalid @title_id was specified.
IF @rowcount_var = 0
BEGIN
PRINT "Warning: The title_id specified is not valid"
RETURN(1)
END
ELSE
BEGIN
PRINT "The book has been updated with the new publisher"
RETURN(0)
END
GO
相關(guān)文章