SQL Server 捕获异常并继续 [英] SQL Server Catch exception and continue
本文介绍了SQL Server 捕获异常并继续的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我遵循程序:
alter procedure sp_insert_cities
(
@txt_nome_cidade varchar(300),
@txt_nome_estado varchar(150) = null,
@txt_pais varchar(150) = null,
@int_id_cidade int output
)
as
begin
//Here an exception may occur
insert into tb_cidades values(
@txt_nome_cidade,
@txt_nome_estado,
@txt_pais)
set @int_id_cidade = @@identity
//Here i want to catch exception and continue executing the proc
if(@@error <> 0)
begin
select @int_id_cidade = int_id_cidade
from tb_cidades
where
txt_nome_cidade = @txt_nome_cidade
end
在 if(@@error <> 0)
行之后,即使有任何错误,我也想继续执行代码,但是 SQL 向我的应用程序和 IF 中的代码抛出异常条件不会执行.
After if(@@error <> 0)
line,i want to continue executing code even if there are any errors,but SQL throws an exception to my application and the code inside IF condition will not executed.
有什么想法吗?
推荐答案
BEGIN TRY
insert into tb_cidades values(
@txt_nome_cidade,
@txt_nome_estado,
@txt_pais)
set @int_id_cidade = @@identity
END TRY
BEGIN CATCH
select @int_id_cidade = int_id_cidade
from tb_cidades
where
txt_nome_cidade = @txt_nome_cidade
END CATCH
这篇关于SQL Server 捕获异常并继续的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文