Hello All,
I'm trying to avoid getting a VB6/ODBC/SQL error message (this is a composed error log message of my own with the standard ODBC script)....
1) 01:16:32 pm TANYAJ Error in clsReference.InsertLock -2147217900 [Microsoft][ODBC SQL Server Driver][SQL Server]Violation of PRIMARY KEY constraint 'PKEELocedRecords'. Cannot insert duplicate key in object 'EELockedRecords'.
whenever this stored proc is called.
CREATE PROCEDURE InsertQALock
(
@Picrefnum VARCHAR(10),
@UserID VARCHAR(12)
)
AS
INSERT INTO EELockedRecords(Picrefnum, UserID)
VALUES(@Picrefnum, @UserID)
================================================================
I'm thinking that redesigning the proc like this.....
CREATE PROCEDURE InsertQALock
(
@Picrefnum VARCHAR(10),
@UserID VARCHAR(12)
)
AS
IF NOT EXISTS (SELECT * FROM EELockedRecords (nolock) WHERE PicRefNum = @PicRefNum)
BEGIN
INSERT INTO EELockedRecords(Picrefnum, UserID)
VALUES(@Picrefnum, @UserID)
END
would eliminate the error from happening again. Is the "IF NOT EXISTS (SELECT....) construct correct? Will it correctly catch and prevent a duplicate PICREFNUM key from being inserted into the table if it already exists? Or is there a better way to "look ahead" into the table to prevent accidentally inserting a duplicate key?
Thanks,
gregoriw
I'm trying to avoid getting a VB6/ODBC/SQL error message (this is a composed error log message of my own with the standard ODBC script)....
1) 01:16:32 pm TANYAJ Error in clsReference.InsertLock -2147217900 [Microsoft][ODBC SQL Server Driver][SQL Server]Violation of PRIMARY KEY constraint 'PKEELocedRecords'. Cannot insert duplicate key in object 'EELockedRecords'.
whenever this stored proc is called.
CREATE PROCEDURE InsertQALock
(
@Picrefnum VARCHAR(10),
@UserID VARCHAR(12)
)
AS
INSERT INTO EELockedRecords(Picrefnum, UserID)
VALUES(@Picrefnum, @UserID)
================================================================
I'm thinking that redesigning the proc like this.....
CREATE PROCEDURE InsertQALock
(
@Picrefnum VARCHAR(10),
@UserID VARCHAR(12)
)
AS
IF NOT EXISTS (SELECT * FROM EELockedRecords (nolock) WHERE PicRefNum = @PicRefNum)
BEGIN
INSERT INTO EELockedRecords(Picrefnum, UserID)
VALUES(@Picrefnum, @UserID)
END
would eliminate the error from happening again. Is the "IF NOT EXISTS (SELECT....) construct correct? Will it correctly catch and prevent a duplicate PICREFNUM key from being inserted into the table if it already exists? Or is there a better way to "look ahead" into the table to prevent accidentally inserting a duplicate key?
Thanks,
gregoriw