0
Assuming the following script:
DECLARE @var1 VARCHAR(100) = 'Stackoverflow';
DECLARE @var2 VARCHAR(100) = '10';
SELECT 1 * @var1;
SELECT 1 * @var2;
If you execute the first select
, we will receive an error of:
Conversion failed when Converting the varchar value 'Stackoverflow' to data type int.
If we execute the second select
, we will succeed.
My question is if there is any kind of treatment - similar to Excel’s SEERRO - to use in the first select
, with that we would have something of the following result:
SELECT IFERROR(1 * @var1, 'Erro', 'Sucesso')
Is there something similar to this in SQL Server? I searched for some possibilities, but I’ve always seen about rollback
and transaction
, my doubt is for some specific function.
https://docs.microsoft.com/en-us/sql/t-sql/functions/error-transact-sql?view=sql-server-2017
– jean
Although it is not good practice to use global variables, pq vc does not use https://docs.microsoft.com/en-us/sql/t-sql/functions/isnumeric-transact-sql?view=sql-server-2017
– jean
or simply use TRY CATCH https://docs.microsoft.com/en-us/sql/t-sql/language-elements/try-catch-transact-sql?view=sql-server-2017
– jean