Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to write query for duplicate records on pan card basis.
my problem is that when i m inserting records for the first time its working but when i m editing record at that time its giving problem.
i am not able to edit any value against that pancard.

What I have tried:

SQL
select * from tblemployee where pancard=@pancard
Posted
Updated 28-Sep-16 21:48pm
v2
Comments
Suvendu Shekhar Giri 29-Sep-16 3:18am    
The query you have shared is a SELECT statement. Can you share what is your UPDATE query here?
manishss 29-Sep-16 3:56am    
i m updating all the records which comes under tblemployee table.
before that i m telling you the problem, what happen when i m inserting records with new pan card its working fine but now i want to change the record say name at that time not able to edit records because of my above query.

If I understand you correctly, the responsibility to prevent duplicates should be on the database server. This should be enforced using a primary key constraint or a unique constraint. For more information see Create Unique Constraints[^]

Now when trying to add the second row that contains the exact same values for key columns as already exists in the database you receive an exception. This exception should be correctly handled. Depending on the situation it may be a programmatic error or you could inform the user that the data already exists if the input is originated from the user.
 
Share this answer
 
manishss[^] wrote:
my problem is that when i m inserting records for the first time its working but when i m editing record at that time its giving problem.
i am not able to edit any value against that pancard.


You forget to mention about method you're using to edit (update) records. Seems, you are using only INSERT statement. To work with databases, you have to use several operations, which have common name: CRUD[^].

To insert (create) data, use INSERT[^] statement
To read data, use SELECT[^] statement
To update (edit) data, use UPDATE[^] statement
To delete data, use DELETE[^] statement.

To avoid duplicates, you have to check if record exists! See:
EXISTS (Transact-SQL)[^]
Subqueries with EXISTS[^]
Subqueries with NOT EXISTS[^]
IF...ELSE (Transact-SQL)[^]

For further details, please see: SQL: If Exists Update Else Insert – Jeremiah Clark's Blog[^]
 
Share this answer
 
Comments
manishss 29-Sep-16 3:59am    
select * from tblemployee where pancard=@pancard
see i m updating records when i m getting zero record in above query in updatation case.
Maciej Los 29-Sep-16 4:03am    
No, you're not! SELECT statement, as i mentioned, is used to read data, not to update. Please, read my answer carfully.
manishss 29-Sep-16 4:16am    
i m telling you the scenario after saving the record in database for the first time i have gridview on edit button all the fields came and suppose i want to change the name of from abc to xyz in that case i m not able to edit cos i getting record from my select query. this query is common for both insert and update.
Maciej Los 29-Sep-16 5:17am    
i m updating records when i m getting zero record in above query in updatation case.
You can't update record when it doesn't exist! In that case you can only add new record.
vani suresh 30-Sep-16 3:08am    
Can you put your whole query for update..So that we can verify where is the problem.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900