Insert statement is inserting duplicate records

Hi Experts,

In my project, I have a finger print scanner. When the students scan their finger, I insert the record in SQL Server. It is inserting duplicate records exactly at same time.


This is my query in the stored procedure.

if not exists(
                   select StdLogID,StdTimeIn from LOGINDETAILS  where userID = @UserID
                   and TodaysDate =FORMAT(GetDate(),'MMddyyyy')
                   and DATEDIFF(SECOND, StdTimeIn,GetDate()) < 60
             and StdTimeIn IS NOT NULL and StdTimeout IS NULL)
         begin
                   INSERT INTO dbo.LOGINDETAILS
                           (UserID,              
                              StdTimeIn,
                            TodaysDate    
                            )
                   VALUES
                              (@UserID,                                    
                              CURRENT_TIMESTAMP,
                              FORMAT(GetDate(),'MMddyyyy')                  
                            )
            end

1139      325      2016-09-09 06:52:28.057      NULL      09092016
1140      325      2016-09-09 06:52:28.057      NULL      09092016
1141      325      2016-09-09 06:52:28.057      NULL      09092016
1154      329      2016-09-09 06:53:38.033      NULL      09092016
1155      329      2016-09-09 06:53:38.033      NULL      09092016
1167      481      2016-09-09 06:55:34.737      NULL      09092016
1166      481      2016-09-09 06:55:34.737      NULL      09092016
1073      604      2016-09-09 06:41:40.243      NULL      09092016
1072      604      2016-09-09 06:41:40.243      NULL      09092016
RadhaKrishnaKiJayaAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Vitor MontalvãoConnect With a Mentor MSSQL Senior EngineerCommented:
Try this:
IF NOT EXISTS (SELECT 1 
        FROM LOGINDETAILS  
        WHERE userID = @UserID 
              AND DATEDIFF(SECOND, StdTimeIn,GetDate()) < 60
              AND StdTimeout IS NULL)
    INSERT INTO dbo.LOGINDETAILS (UserID,  StdTimeIn, TodaysDate) 
    VALUES (@UserID,CURRENT_TIMESTAMP,FORMAT(GetDate(),'MMddyyyy')

Open in new window

             
But if possible change the PK to a composite of the columns UserID and StdTimeIn. This will avoid the insert of any duplicate record.
0
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
So what is your question:  How to avoid inserting duplicates, how to remove duplicates in the table already, or both?
Looks like whatever feeds @UserID is doing it twice, hence the duplicates when it hits your query.
0
 
RadhaKrishnaKiJayaAuthor Commented:
Thanks for your reply. My question is " How to avoid inserting duplicates"?
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
Vitor MontalvãoMSSQL Senior EngineerCommented:
One of the cases that will be easily solved with a proper PK. I guess you're using an identity column as PK, right?
0
 
RadhaKrishnaKiJayaAuthor Commented:
Yes
0
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
It would help if you give the column names for your sample data.
0
 
RadhaKrishnaKiJayaAuthor Commented:
StdLogID UserID     StdTimeIn                       StdTimeout    TodaysDate
-----------------------------------------------------------------------------------------------
1139      325      2016-09-09 06:52:28.057      NULL      09092016
 1140      325      2016-09-09 06:52:28.057      NULL      09092016
 1141      325      2016-09-09 06:52:28.057      NULL      09092016
 1154      329      2016-09-09 06:53:38.033      NULL      09092016
 1155      329      2016-09-09 06:53:38.033      NULL      09092016
 1167      481      2016-09-09 06:55:34.737      NULL      09092016
 1166      481      2016-09-09 06:55:34.737      NULL      09092016
 1073      604      2016-09-09 06:41:40.243      NULL      09092016
 1072      604      2016-09-09 06:41:40.243      NULL      09092016
0
 
ZberteocCommented:
Use this:
if not exists
	(select * from LOGINDETAILS  
		where 
			userID = @UserID 
		and TodaysDate =FORMAT(GetDate(),'MMddyyyy')
		and DATEDIFF(SECOND, StdTimeIn,GetDate()) < 60
		and StdTimeIn IS NOT NULL and StdTimeout IS NULL)
begin
	INSERT INTO dbo.LOGINDETAILS 
	(	UserID,               
		StdTimeIn,
		TodaysDate     
	) 
	SELECT 
		@UserID,                                     
		CURRENT_TIMESTAMP,
		FORMAT(GetDate(),'MMddyyyy')
	WHERE
		NOT EXISTS (SELECT * FROM dbo.LOGINDETAILS WHERE UserID=@UserID and StdTimeIn=CURRENT_TIMESTAMP and TodaysDate=FORMAT(GetDate(),'MMddyyyy'))
end

Open in new window

The problem is that your IF statement checks different values from what you insert so you will have to make sure that what you actually insert does not already exist.
0
 
RadhaKrishnaKiJayaAuthor Commented:
Thank you for trying to help me. But it didn't work.
0
 
ZberteocCommented:
What exactly didn't work?
0
 
RadhaKrishnaKiJayaAuthor Commented:
Still it is adding the duplicates.
0
 
ZberteocCommented:
How can this insert duplicates?
INSERT INTO dbo.LOGINDETAILS 
	(	UserID,               
		StdTimeIn,
		TodaysDate     
	) 
	SELECT 
		@UserID,                                     
		CURRENT_TIMESTAMP,
		FORMAT(GetDate(),'MMddyyyy')
	WHERE
		NOT EXISTS (SELECT * FROM dbo.LOGINDETAILS WHERE UserID=@UserID and StdTimeIn=CURRENT_TIMESTAMP and TodaysDate=FORMAT(GetDate(),'MMddyyyy'))

Open in new window

It would be possible only if the SELECT part returns 2 duplicate rows that don't yet exist in the target table but in this case that is out of the question because you don't SELECT from a table or sub query but you simply return a rowset with one row formed by the variable content and current date values.

Are you sure you used the code I posted? Or is there a trigger that in the background duplicates the insert somehow?
0
 
RadhaKrishnaKiJayaAuthor Commented:
Actually there is no trigger in the background and I just copied the query you sent.

Thanks for trying to help me.
0
 
ZberteocCommented:
Still, if you really want to be helped then you need to backup your statements with some data. I would like to see an example of data before and after you used my code in order to find what causes the duplicates, if that really is the case. The only reason I would see is that the values in the table somewhat differ from the ones are inserted at the moment when they are compared in the WHERE clause. This starts to sounds like twilight zone.
0
 
RadhaKrishnaKiJayaAuthor Commented:
Hi,

I like the idea of the composite key. I think that would be the best solution to avoid the duplicates.

Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.