tags:

views:

5716

answers:

2

I am trying to port this line from MS SQL Server to SQLite

IF NOT EXISTS(SELECT 1 FROM EVENTTYPE WHERE EventTypeName = 'ANI Received') 
    INSERT INTO EVENTTYPE (EventTypeName) VALUES ('ANI Received');

It seems that SQLite does not support IF NOT EXISTS or at least I can't make it work. Am I missing something simple? Is there a workaround?

+15  A: 

How about this?

INSERT OR IGNORE INTO EVENTTYPE (EventTypeName) VALUES 'ANI Received'

(Untested as I don't have SQLite... however this link is quite descriptive.)

Additionally, this should also work:

INSERT INTO EVENTTYPE (EventTypeName)
SELECT 'ANI Received'
WHERE NOT EXISTS (SELECT 1 FROM EVENTTYPE WHERE EventTypeName = 'ANI Received');
beach
Thanks. However, it should be noted that INSERT OR IGNORE piece only works if EventTypeName is set to be unique.
AngryHacker
True. I assumed that it was unique given how it was used in the sample SQL. If not, the second method should be used.
beach
+1  A: 

The very common SQL "IF EXISTS()" and "IF NOT EXISTS()" is another 1 of countless, common things that just don't exist in sqlite at all.

(Very unfortunately)

Another "work around", another "band aid".

Patricia