tags:

views:

256

answers:

2

sql - query to insert a column value if it does not exist in that column

+3  A: 

Hm. Do you want a new row? In that case,

 IF NOT EXISTS(SELECT 1 FROM emp WHERE fruits = 'mango')
    INSERT INTO emp (fruits) VALUES ('mango')
Jonas Lincoln
can u just tell me clearly abt not Exists.i want to insert a value for example insert into emp("fruits") values("mango"); this statement has to insert if and only if mango is not present alreadycan u tell me the query for that.
sangeetha
Is this clearer?
Jonas Lincoln
emp is the table name and fruits is the column name and mango is the value.Now can u tell me how
sangeetha
Ok, updated the example. It's the same as valli's first example.
Jonas Lincoln
+1  A: 

Two ways to do

1.IF NOT EXISTS (SELECT fruit FROM emp WHERE fruit='mango') 
BEGIN 
INSERT INTO emp(fruit) Values('mango'); 
END 

2.INSERT INTO emp ('mango') SELECT distinct fruit FROM emp WHERE not exists (select fruit from emp as e Where emp.fruit = e.fruit);
valli