SQL Server Catch exception and continue

前端 未结 2 1748
我在风中等你
我在风中等你 2021-01-05 16:58

Ive follow procedure:

alter procedure sp_insert_cities
(
    @txt_nome_cidade varchar(300),
    @txt_nome_estado varchar(150) = null,
    @txt_pais varchar(1         


        
2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-05 17:55

    The following will try to run your command. You can put anything you want to run in the CATCH block, which will execute only when an error occurs. The remaining code after the CATCH will run with error or without error.

    BEGIN TRY
       insert into tb_cidades values(
       @txt_nome_cidade,
       @txt_nome_estado,
       @txt_pais)
    
           set @int_id_cidade = @@identity
    END TRY
    
    BEGIN CATCH
      PRINT 'Error occurred'
    
    END CATCH
    
    if(@@error <> 0)
    begin
    select @int_id_cidade = int_id_cidade 
    from tb_cidades 
    where 
    txt_nome_cidade = @txt_nome_cidade
    end
    

提交回复
热议问题