tvae
asked on
Ignore Error in a store procedure and continue
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.
If the record already exist then just continue with the rest of the procedure.
ASKER
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?
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?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
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
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
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