0
votes

I'd like to combine an insert query with a "where not exists"

Here my SQL :

INSERT INTO MYTABLE (ACT_ORDER, MNU_ACTION, ACT_STATUT, ACT_BEFORE, ACT_AFTER)
SELECT (1,'acOuvPOS', 'T', 'T', NULL)
WHERE NOT EXISTS (SELECT * FROM MYTABLE WHERE  (MNU_ACTION ='acOuvPOS' AND ACT_STATUT = 'T' AND ACT_BEFORE= 'T');

But when I execute the SQL I have this problem

Invalid token. Dynamic SQL Error. SQL error code = -104. Token unknown - line 2, char 8. ,.

How can I resolve this please Thanks in advance

2
What version of SQL are you using (e.g. MySQL)? - Tim Biegeleisen
Your missing a closing ) before the terminating ; - Alex K.
Which DBMS are you using? - a_horse_with_no_name
Sorry I miss the closing ) when coping the requet, I use Interbase 6.0.2 - mimou_2009
I think that SELECT needs a FROM clause. - Tim Biegeleisen

2 Answers

0
votes

INSERT INTO MYTABLE (ACT_ORDER, MNU_ACTION, ACT_STATUT, ACT_BEFORE, ACT_AFTER) SELECT 1,'acOuvPOS', 'T', 'T', NULL WHERE NOT EXISTS (SELECT * FROM MYTABLE WHERE MNU_ACTION ='acOuvPOS' AND ACT_STATUT = 'T' AND ACT_BEFORE= 'T')

0
votes

you can simply say :

IF ( NOT EXISTS (SELECT * FROM MYTABLE WHERE (MNU_ACTION ='acOuvPOS' AND ACT_STATUT = 'T' AND ACT_BEFORE= 'T') BEGIN INSERT INTO MYTABLE (ACT_ORDER, MNU_ACTION, ACT_STATUT, ACT_BEFORE, ACT_AFTER) (1,'acOuvPOS', 'T', 'T', NULL) END