I have two tables - tblRequests and tblData. tblRequests has a primary key called recID, and tblData has a foreign key to that primary key, called requestRecID.
On tblRequests, I have a unique index on two columns, which prevents users from entering duplicate rows.
Problem: when I attempt the duplicate insert on tblRequests, it errors out as expected, but my tblData is still updating its foreign key.
So, how do I say "Don’t update tblData if tblRequests insert didn't happen"?
In doing some research, it seems a try/catch would be in order, but I am not at all familiar with this level of SQL.
My code below:
CREATE Procedure [dbo].[spInsert]
(
@vOpID varchar(3),
@vSNumb varchar(12)
)
AS
Declare @vRecID int
BEGIN
BEGIN TRANSACTION
Insert tblRequests
(
opID,
SNumb
)
Values
(
@vOpID,
@SNumb
)
Set @vRecID = IDENT_CURRENT ('tblRequests')
COMMIT TRANSACTION;
BEGIN TRANSACTION
Update tblData
Set requestRecID = @vRecID
Where SNumb = @SNumb And opID = @vOpID
COMMIT TRANSACTION;
END