0

I have a table that must have only one row. if this row exist don't do nothing if there is no row insert data that does NOT contain the indexs field

I tried to use the below code but I get an error:

select count(id) into @myvar from customer;
IF(@myvar>0)
BEGIN
    INSERT INTO customer 
    (dateformat, alarmDays, backup, pastPass, loginFails, passLength, timetochangepass)
    values('d,m,y',30, 30, 3, 5, 8, 90)
END
Ramprasath Selvam
  • 2,706
  • 1
  • 19
  • 31
Avi Levi
  • 11
  • 3
  • 1
    Control statements like `if` are only allowed in procedures, triggers, events and so on. – juergen d Aug 28 '19 at 06:24
  • Every if needs a THEN and an END IF, every statement needs a terminator, an IF..END IF block in mysql does not require a BEGIN..END block, if this code is in a stored program(procedure,function,trigger,event) then an if exists test would be better (for readability as much as anything else) – P.Salmon Aug 28 '19 at 06:38
  • You only anticipate 1 customer? – P.Salmon Aug 28 '19 at 06:42
  • thank you all i will try using stored program – Avi Levi Aug 28 '19 at 07:28
  • For a different method (and if you have control over your table), you could try [`INSERT ... ON DUPLICATE KEY UPDATE`](https://stackoverflow.com/q/4205181) with a dummy/default primary/unique key. – Solarflare Aug 28 '19 at 07:52

0 Answers0