tags:

views:

45

answers:

4
+1  A: 

To just check if a Lion is already in the table:

select count(*) from animals where name = 'Lion'

You can do the check and the insert in one query with a where clause:

insert into animals (name) 
select  'Lion'
where not exists 
        (
        select * from animals where name = 'Lion'
        )

In reply to your comment, to select a sub-list of animals:

select name from animals where name in ('Lion', 'Tiger', 'Jaguar')

This would return up to 3 rows for each animal that already exists.

Andomar
can I have a single query where I can pass multiple parameters at once and just get the list of the animals that are already in table.I do not want query to add the animals .
Ram
@Ram: Perhaps an IN clause would do the trick, answer edited
Andomar
@Andomar - Thanks.
Ram
A: 

SELECT COUNT(your_animal_column) FROM tblAnimals WHERE your_animal_column = ?;

The question marks get filled by your csv values. If this statement returns more than 0 there the value already exists

MUG4N
A: 

If your incoming file is already in a normalized column format, instead of comma separated like you have displayed, it should be easy. Create a temp table for the insert, then something like..

insert into YourLiveTable ( animalname ) 
   select Tmp.animalname
      from YourTempInsertTable Tmp
      where Tmp.animalname not in 
               ( select Live.animalname  
                    from YourLiveTable Live )

To match your revised request... just use the select portion and change "NOT IN" to "IN"

   select Tmp.animalname
      from YourTempInsertTable Tmp
      where Tmp.animalname IN 
               ( select Live.animalname  
                    from YourLiveTable Live )
DRapp
A: 

How about

SELECT ANIMAL_NAME, COUNT(*)
FROM ANIMALS
GROUP BY ANIMAL_NAME
HAVING COUNT(*) > 1

This should give you all the duplicate entries

pm_2
That would find animals that are already duplicated in the table. If I understand the question correctly, the OP would like to prevent duplicates while importing a CSV file
Andomar
The revised question says "I just want duplicate animals"
pm_2