Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Check Duplicate Expand / Collapse
Author
Message
Posted Tuesday, March 09, 2010 10:02 PM


SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Today @ 1:19 AM
Points: 80, Visits: 349
Hello,

I had a table name: candidate_details
Columns under this table are: ID(primary key), name, dob, mobile, telephone & email

Now I want to check the duplicate entries, but my duplicate checking should be with name, dob & email.

If either of the two columns found similar with the existing data in table, TRIGGER should RAISERROR.

PLEASE HELP ME.


----------------------------------------
Daipayan
A Beginner to the World of DBMS!
----------------------------------------
Post #879924
Posted Tuesday, March 09, 2010 11:58 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 4:37 PM
Points: 21,627, Visits: 27,480
Please provide the DDL (CREATE TABLE statement) for the table, sample data (as a series of INSERT INTO statments) to populate the table and is representative of the problem, expected results based on the sample data, and what you have written so far to solve this problem.

Without all this, not much help we can provide except for shots in the dark that may or may not prove helpful. Plus, if you provide all the requested information, you will get back TESTED CODE.



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #879963
Posted Wednesday, March 10, 2010 12:11 AM


SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Today @ 1:19 AM
Points: 80, Visits: 349
TABLE:
CREATE TABLE [candidate_details] (
[candidate_ID] [numeric](18, 0) IDENTITY (1, 1) NOT NULL ,
[name] [varchar] (500) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[dob] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[mobile] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[email] [varchar] (200) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
CONSTRAINT [PK_candidate_details] PRIMARY KEY CLUSTERED
(
[candidate_ID]
) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO

TEST DATA:
INSERT INTO [dbo].[candidate_details]([name], [dob], [mobile], [email])
VALUES('Alex', '03/26/1970', '9876567898', 'alex@example.com')
VALUES('John', '12/24/1987', '9876567896', 'john@gmail.com')
VALUES('Sinha', '06/06/1980', '9877297783', 'sinha@yahoo.com')
VALUES('Alex', '09/21/1980', '9836829829', 'alex@example.com')

Here you will see, there are 2 candidate having same name n same E-Mail ID but different DOB. This can be accepted. But if the same candidate having same name, e-mail & DOB, that should not be accepted. A duplication should be checked with either of the two fields i.e. Name & E-Mail, E-Mail & DOB, Name & DOB or individual Name, E-mail, DOB, BUT IF ALL THE THREE CRITERIA ARE MATCHING, DATA SHOULD NOT BE INSERTED.

Hope this may help you to resolve my problem. Thank You in advance.


----------------------------------------
Daipayan
A Beginner to the World of DBMS!
----------------------------------------
Post #879968
Posted Wednesday, March 10, 2010 12:14 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Monday, June 04, 2012 5:00 PM
Points: 891, Visits: 225
Hi,


You can check these duplicates using queries

select name,dob,count(*) from candidate_details group by name,dob

select name,email,count(*) from candidate_details group by name,email

select dob,email,count(*) from candidate_details group by dob,email

if count is more than 1 means duplicate values..

Similarly you can create insert and update trigger on table to check if any of this pair value already exists in the table.




Post #879970
Posted Wednesday, March 10, 2010 12:17 AM


SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Today @ 1:19 AM
Points: 80, Visits: 349
I want to know, what can be the combined insert/update trigger for this!!

----------------------------------------
Daipayan
A Beginner to the World of DBMS!
----------------------------------------
Post #879972
Posted Wednesday, March 10, 2010 5:49 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 4:51 PM
Points: 32,923, Visits: 26,811
mail2payan (3/10/2010)
I want to know, what can be the combined insert/update trigger for this!!


Ummmm... no... a trigger isn't the place for such code. You should already know if the data is going to cause an error before you try an insert. If you just want an error to pop up like you say, put a unique index on the column combination... it'll raise an error if anyone try's to insert duplicate info into the columns you care about.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #880629
Posted Wednesday, March 10, 2010 9:59 PM


SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Today @ 1:19 AM
Points: 80, Visits: 349
I did this & it works....
, CONSTRAINT [UNQ_name_email] UNIQUE (
[mobile]
, [email]
)

, CONSTRAINT [UNQ_dob_email] UNIQUE (
[dob]
, [email]
)

, CONSTRAINT [UNQ_dob_name] UNIQUE (
[dob]
, [name]
)

, CONSTRAINT [UNQ_dob_name_email] UNIQUE (
[dob]
, [name]
, [email]
)



----------------------------------------
Daipayan
A Beginner to the World of DBMS!
----------------------------------------
Post #880687
Posted Wednesday, March 10, 2010 10:00 PM


SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Today @ 1:19 AM
Points: 80, Visits: 349
Thank You for your Guidance!!!

----------------------------------------
Daipayan
A Beginner to the World of DBMS!
----------------------------------------
Post #880688
Posted Wednesday, March 10, 2010 11:18 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 4:51 PM
Points: 32,923, Visits: 26,811
Very cool. Thank you for the feedback.

--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #880715
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse