Solved

Ignore Error in a store procedure and continue

Posted on 2010-08-20
5
298 Views
Last Modified: 2012-05-10
I have a stored procedure that insert to a table and I have creadted a primary key. I want to ignore the error Violation of Primary Key - cannot isert duplicate key.

If the record already exist then just continue with the rest of the procedure.
0
Comment
Question by:tvae
  • 2
  • 2
5 Comments
 
LVL 2

Expert Comment

by:aamsoh
ID: 33488471
you can handel the errors like this,


DECLARE @Error INT

-- DO Stuff
UPDATE client SET cliTypCd = (SELECT cliTypCd FROM client T WHERE T.link = @throwLink) WHERE link = @keepLink
SET @Error = @@ERROR

IF @Error <> 0 AND @Error <> 30002
BEGIN
      -- Handle Error
END
-- Else Ignore
0
 

Author Comment

by:tvae
ID: 33488642
aamsoh,

What is this step doing?
UPDATE client SET cliTypCd = (SELECT cliTypCd FROM client T WHERE T.link = @throwLink) WHERE link = @keepLink

Do i need to put replace anything in there?
0
 
LVL 23

Accepted Solution

by:
Rajkumar Gs earned 250 total points
ID: 33488656
Please check the attached script

Raj
-- Case #1

-- Copying bulk data from Table1 to Table2

INSERT INTO Table2

	SELECT * FROM Table1

	WHERE ID NOT IN (SELECT ID FROM Table2)

	-- AND (if any where conditions add here)



-- Case #2

-- Copying single row at a time

IF (SELECT COUNT(*) FROM Table2 WHERE ID = @ID) = 0

BEGIN

	INSERT INTO Table2 SELECT * FROM Table1 

						-- WHERE ... (any where condition add here)

END

Open in new window

0
 
LVL 2

Expert Comment

by:aamsoh
ID: 33488658
actully this was only a sample. you should consider on this code:
you can use this error handel with INSERT OR UPDATE quety.


SET @Error = @@ERROR

IF @Error <> 0 AND @Error <> 30002
BEGIN
      -- Handle Error
END
-- Else Ignore
0
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 33488672
If you are inserting bulk records, first script can help. It will insert only new records based on primary key.

If you are inserting single record at a time, then you can check whether any row with the primary key value you are going to insert, already exists in the table. If not insert.

Raj
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now