Your issue is not clear enough.
First of all, i would suggest you to read below articles:
Error Handling in T-SQL: From Casual to Religious[
^]
Using TRY...CATCH in Transact-SQL[
^]
SQL Server Error Handling Workbench[
^]
Handling Errors in Stored Procedures[
^]
SQL Server 2005 Try and Catch Exception Handling[
^]
Secondly, you can check for existing records in a 3 ways:
1) using
EXISTS[
^] statement.
2) using
SELECT
with
IN[
^] or
NOT IN
option
3) looping through the set of records (using
WHILE[
^] statement or
CTE[
^] /Common Table Expressions/)
Example:
INSERT INTO Table2 (Title)
SELECT Title
FROM (SELECT 'Title1' UNION ALL
SELECT 'Title2' UNION ALL) AS T
WHERE T.Title NOT IN (SELECT Title FROM Table2)
NOTE: Above query is not optimal and time of execute is very long in case of large data amount.