How to launch custom T-SQL exception?

Asked

Viewed 199 times

3

I have a Tigger for CPF validation, and I want to make an exception if the reported CPF is invalid.

About the release of exceptions I’ve been reading this publishing, but in the author’s examples he simply makes an exception that already exists in the system (Division by zero) and simply relaunches this exception.

But what I want is to cast a new exception, an exception that doesn’t exist in the system, like, an exception CpfInvalid.

1 answer

2

Matheus, the SQL SERVER does not have a function for validation of CPF or CNPJ, these types of numbers are Brazilian standards and are not applied to other countries, so a function of this type would have no use for many users of this bank.

You have the option to cast the exception through the BEGIN TRY END TRY or use the RAISERROR to returns an error message.

Ideal in this case is that you treat your client-side validation (EX; Javascript) and in the application (EX; C#, JAVA), do not use the database for this type of validation, this has a cost of processing back and forth to the database, when your application could easily validate.

  • I understand the questions of making the validations in the application. But, take up the theme of about doing this in db. I see no reason to use BEGIN TRY END TRY because you yourself cited an exception of CpfInvalido does not exist in SQL Server, I already knew that. I do not see pq use BEGIN TRY END TRY for the same reason I find the examples of the publication I quoted unfortunate. That is, in the example of the publication the author does not make any treatment, only relaunches the exception with an extra text. As in Sqlserver there is no exception for CpfInvalid TRY CATCH is useless. cont.....

  • I don’t want to treat an exception that doesn’t exist, I just want to launch one, and let it explode in the app.

  • 1

    @Matheussaraiva , RAISERROR may return an error message.

  • @Matheussaraiva, Returns beautiful messages in the application are not a good, it would only hide the real error that is occurring, the way that Altor does, described in the question is not wrong, it returns the exception correctly, there is no error in it.

  • In my understanding what the author does is just capture and relaunch. Isn’t that the same as not doing any treatment? RAISERROR I have seen that he is depreciated and being replaced by THROW, where the doc claims to have three parameters error_number message and state.&#My question now is whether the values of error_number and state are free choice programmer.

  • @Matheussaraiva, are not well of free choice, the ERROR_NUMBER is captured in the treatment block as well as the ERROR_MESSAGE and the ERROR_STATE, but you can show message the way you want, but as I said earlier it would only hide the real error.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.