Solved

Insert statement is inserting duplicate records

Posted on 2016-09-09
15
51 Views
Last Modified: 2016-09-13
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
0
Comment
Question by:RadhaKrishnaKiJaya
  • 7
  • 4
  • 3
  • +1
15 Comments
 
LVL 65

Expert Comment

by:Jim Horn
ID: 41791444
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
 

Author Comment

by:RadhaKrishnaKiJaya
ID: 41791447
Thanks for your reply. My question is " How to avoid inserting duplicates"?
0
 
LVL 45

Expert Comment

by:Vitor Montalvão
ID: 41791453
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
 

Author Comment

by:RadhaKrishnaKiJaya
ID: 41791454
Yes
0
 
LVL 45

Expert Comment

by:Vitor Montalvão
ID: 41791456
It would help if you give the column names for your sample data.
0
 

Author Comment

by:RadhaKrishnaKiJaya
ID: 41791459
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
 
LVL 26

Expert Comment

by:Zberteoc
ID: 41791639
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
Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

 

Author Comment

by:RadhaKrishnaKiJaya
ID: 41791760
Thank you for trying to help me. But it didn't work.
0
 
LVL 26

Expert Comment

by:Zberteoc
ID: 41791825
What exactly didn't work?
0
 

Author Comment

by:RadhaKrishnaKiJaya
ID: 41794321
Still it is adding the duplicates.
0
 
LVL 26

Expert Comment

by:Zberteoc
ID: 41794348
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
 

Author Comment

by:RadhaKrishnaKiJaya
ID: 41796188
Actually there is no trigger in the background and I just copied the query you sent.

Thanks for trying to help me.
0
 
LVL 26

Expert Comment

by:Zberteoc
ID: 41796210
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
 
LVL 45

Accepted Solution

by:
Vitor Montalvão earned 500 total points
ID: 41796248
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
 

Author Closing Comment

by:RadhaKrishnaKiJaya
ID: 41796937
Hi,

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

Thanks
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now