--检查@@ERROR,如果是0则执行成功,否则执行出错 --如: -- 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
要update到符合条件的行中, 但是数据库中又不存在这样的一行, 不会报错,rowcount = 0 而已。
那就简单了, 执行更新之后判断一下@@rowcount是不是等0 , 等于0的话就说明没更新到资料, 报错出来就可以啦
if exists ...先update 再 @@rowcount 从编程思想来说,跟用try catch 一样, 算是后设防, 把本来可预知的错误当作不可预知的异常来处理, 不建议这么做。
--如:
-- 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