• Home
  • About
SQiLler

Tag Archives: try and catch

TRY and CATCH on MSSQL

Posted on July 13, 2014 by Hiram Romero

TRY and CATCH is a very helpful way to identify errors on basically any major programming language, and MSSQL is not the exception.

Using TRY and CATCH will let us check if the query we want to execute will fail, and if so do something about it, here is a basic example:

For this we will use AdventureWorks Microsoft database sample.

BEGIN TRY
INSERT INTO PERSON.Person (BusinessEntityID,PersonType,FirstName,LastName)
VALUES ('X','EM','Hiram','Romero')
END TRY

BEGIN CATCH
SELECT ERROR_MESSAGE() as Error
END CATCH

The above example returns:
“Conversion failed when converting the varchar value ‘X’ to data type int.”

Because the column BusinessEntityID is an int value, and we tried to insert a varchar (letter X).

NOTE! This example doesn’t interrupts the QUERY execution.

Continue reading “TRY and CATCH on MSSQL” »

Posted in SQL | Tags: MSSQL, sql, sqlserver, try and catch, try catch, TSQL | Leave a comment |

Stack Overflow

profile for sqiller on Stack Exchange, a network of free, community-driven Q&A sites

Recent Posts

  • English dictionary with synonyms
  • Create your own SQL log
  • Drop table if exists
  • Replace a value in all tables
  • Share temp tables to other sessions
Follow on Bloglovin

Pages

  • About

Archives

  • June 2015
  • August 2014
  • July 2014

Categories

  • SQL (23)

WordPress

  • Register
  • Log in
  • WordPress
CyberChimps

CyberChimps

© SQiLler