Insert into ProjectCode(ProjectCode_Item_Code)
SELECT ProjectCode_Item_Code
FROM ProjectCode
WHERE NOT EXISTS(SELECT ProjectCode_Item_Code
FROM ProjectCode where ProjectCode_Item_Code = xx) ;
I want to check if the row already exists in the table, if so then don't do anything but if it does not exists then insert the row. Above is just an example of what I am trying to do.
The problem is when this table does not have any records, its empty then
NOT EXISTS(SELECT ProjectCode_Item_Code
FROM ProjectCode where ProjectCode_Item_Code = xx)
will not return anything , then will my query work ?
Originally posted by sonaliak The problem is when this table does not have any records, its empty then
NOT EXISTS(SELECT ProjectCode_Item_Code
FROM ProjectCode where ProjectCode_Item_Code = xx)
will not return anything
Wrong, in this case the above expression will return TRUE, so the inserts will take place.
Jurij Modic ASCII a stupid question, get a stupid ANSI
24 hours in a day .... 24 beer in a case .... coincidence?
Am I being stupid here, or is it possible that the reason that this does not work is because you are trying to insert into "Table A" all the records in "Table A" that do not exist in "Table A"? If they do not exist in "Table A" then you can't select them from "Table A" in order to insert them. Or have I missed something?
Originally posted by slimdave Am I being stupid here, or is it possible that the reason that this does not work is because you are trying to insert into "Table A" all the records in "Table A" that do not exist in "Table A"? If they do not exist in "Table A" then you can't select them from "Table A" in order to insert them. Or have I missed something?
I guess I did not explain the question very well...
I will try again..
At 1st the table is empty..
I want to insert a row coming in..obviously at this point as there are no records in the table.. I want to insert the 1st row.
When someone tries to insert the next record... i want to check if it is already there in that table or not.. if its there then do not insert it, if it isn't then insert it.. and so on.
In sql server you can do it by saying
If not exists( SELECT ProjectCode_Item_Code
FROM ProjectCode where ProjectCode_Item_Code = XYZ)
Insert into ProjectCode(ProjectCode_Item_Code)
VALUES(xyz)
can I do the same thing in Oracle ?
The statement I wrote will work and works only when I have atleast 1 record in the table... but not at start when there will be no records.
Insert into ProjectCode(ProjectCode_Item_Code)
SELECT xyz
FROM ProjectCode
WHERE NOT EXISTS(SELECT ProjectCode_Item_Code
FROM ProjectCode where ProjectCode_Item_Code = XYZ) ;
Bookmarks