0

im using this sql query :

IF NOT EXISTS(SELECT 1 FROM Payments WHERE CustomerID = '145300')
    INSERT INTO Payments(CustomerID,Amount)
    VALUES('145300',12.33)

but i get this error:

Error
Static analysis:

1 errors were found during analysis.

Unrecognized statement type. (near "IF NOT EXISTS" at position 0)
SQL query: Documentation

IF NOT EXISTS(SELECT 1 FROM Payments WHERE CustomerID = '145300') INSERT INTO Payments(CustomerID,Amount) VALUES('145300',12.33)

MySQL said: Documentation

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INSERT INTO Payments(CustomerID,Amount)
    VALUES('145300',12.33)' at line 2

this SQL query which I am using is from the answer of the question in this link : sql query

based an another answer, I even used END IF at the end of this statement but it didn't work yet.

how can solve this problem?

Dharman
  • 30,962
  • 25
  • 85
  • 135
Program_Lover
  • 91
  • 1
  • 10

1 Answers1

0

You can use Flow Control Statements only in stored programs. Use INSERT IGNORE when inserting a row with an explicit PK value. Alternatively if CustomerID is not a PK by chance

 insert Payments(CustomerID,Amount)
 select * 
 from (
        values row ('145300',12.33) 
      ) t(CustomerID,Amount)
 where not exists (select 1 
                   from Payments p 
                   where p.CustomerID = t.CustomerID);
Serg
  • 22,285
  • 5
  • 21
  • 48