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
2009-12-10 12:07:32
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
2009-12-10 12:23:01
Is this clearer?
Jonas Lincoln
2009-12-10 12:28:27
emp is the table name and fruits is the column name and mango is the value.Now can u tell me how
sangeetha
2009-12-10 12:48:30
Ok, updated the example. It's the same as valli's first example.
Jonas Lincoln
2009-12-10 12:50:23
+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
2009-12-10 12:25:08