I know may be it will seem a newbie question, but i facing this problem.
I have created a table users where i have 3 columns
CREATE TABLE USERS
(ID INT IDENITITY(1,1),
USERNAME VARCHAR(10),
USERID INT)
Now every time there is new requirement to create a user i call a procedure to check if requested USERID is available in system or not, if not then i create that user
CREATE PROCEDURE CreateUser
@USERID INT,
@USERNAME VARCHAR(10),
AS
IF EXISTS (SELECT * FROM USERS WHERE USERID=@USERID)
SELECT 'USER ALREADY EXISTS.'
ELSE
BEGIN
INSERT INTO USERS (USERNAME,USERID) VALUES (@USERNAME,@USERID)
END
It is not fail proof to avoid duplicate value.
I check my database and found same USERID was created twice.
I know may be some of you guys will think why don't i make this USERID unique, but unfortunately this table is shared by 2 different kind of users one with USERID and one with no USERID (where USERID will be null).
How i am going to intercept 2 request which have already pass first check (EXISTS) and doing INSERT for same USERID.
Any advise will be appreciated.