Black Dagger
Black Dagger

Reputation: 399

On error display error message FIRST and then return (MS Sql Server)

Begin Try
exec @sql
End Try

Begin Catch
Display error message (How to?)
return
End Catch

Now, on error with the exec @sql statement, I want it to display the error message first and then end the code.

Upvotes: 0

Views: 879

Answers (2)

Gemini
Gemini

Reputation: 89

To get the error message generated by system

  declare @sql varchar(100) = 'Select 1/0'  
  Begin Try  
        exec(@sql) 
  End Try  
  Begin Catch
        print ERROR_MESSAGE()
  return

Upvotes: 1

Elmer
Elmer

Reputation: 272

declare @sql varchar(100) = 'Select 1/0'  
Begin Try  
exec(@sql) 
End Try  

Begin Catch
print 'error='+ERROR_MESSAGE()
return
End Catch

Upvotes: 1

Related Questions