I'm trying to implement a SQL query to "UPDATE if exists else INSERT"
My table(Allowance) is as below:
EmployeeID int(8) PK
Year year(4) PK
Month int(2) PK
OverTime decimal(10,2)
Medical decimal(10,2)
Lunch decimal(10,2)
Bonus decimal(10,2)
Allowance decimal(10,2)
Below is the SQL query I tried:
IF EXISTS (SELECT * FROM Allowance WHERE EmployeeID =10000001 and Year = 2014 and Month = 4)
    UPDATE Allowance
    SET OverTime = 10.00, Medical = 10.00, Lunch = 10.45, Bonus =10.10, Allowance = 40.55
    WHERE EmployeeID =10000001 and Year = 2014 and Month = 4
ELSE
    INSERT into Allowance values (10000001,2014,4,10.00,10.00,10.45,10.10,40.55)
I keep getting this error message:
"#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IF EXISTS (SELECT * FROM Allowance WHERE EmployeeID =10000001 and Year = 2014 an' at line 1 "
Can somebody please help??
The SQL EXISTS condition is used in combination with a subquery and is considered to be met, if the subquery returns at least one row. It can be used in a SELECT, INSERT, UPDATE, or DELETE statement.
No. Insert will only create a new row.
If that record already exists then, the stored procedure will update that record. And if the record does not exist then, it will insert the record in the table. So, in this tutorial, we will understand how to create a stored procedure for if exists else update operation in SQL Server 2019. And we will also try to demonstrate multiple examples.
The easiest way to do MySQL INSERT or UPDATE if exists is to use INSERT ON DUPLICATE command. Here is an example to insert values (1,2) into couple of columns (key, col1) in our table, where key is our primary key and col1 is for data.
4 SQL syntax for "if exists" 1 Insert or update if column with specific value exists 0 Codeigniter Bootstrap add and edit record simultaneously 0 MySQL query breaks in linux shell script, but query runs in mysql command line
In this article, we will learn how to insert record if it doesn’t exist, or update it if it exists in MySQL. The easiest way to do MySQL INSERT or UPDATE if exists is to use INSERT ON DUPLICATE command. Here is an example to insert values (1,2) into couple of columns (key, col1) in our table, where key is our primary key and col1 is for data.
The below query will fulfill your requirement.
INSERT INTO `ALLOWANCE` (`EmployeeID`, `Year`, `Month`, `OverTime`,`Medical`,
`Lunch`, `Bonus`, `Allowance`) values (10000001, 2014, 4, 10.00, 10.00,
10.45, 10.10, 40.55) ON DUPLICATE KEY UPDATE `EmployeeID` = 10000001
                        With this procedure you can check if exist or not and then update/insert as you want
 DELIMITER $$;   
 CREATE PROCEDURE example()
BEGIN
DECLARE vexist int;
  SELECT count(*) into vexist FROM Allowance --count because i will
  WHERE EmployeeID =10000001 and Year = 2014 and Month = 4;  --this will check if exist or not
    IF (vexist >= 1) then  --if exist then update
    UPDATE Allowance
        SET OverTime = 10.00, Medical = 10.00, Lunch = 10.45, Bonus =10.10, Allowance = 40.55
        WHERE EmployeeID =10000001 and Year = 2014 and Month = 4;
    ELSE
      INSERT into Allowance values (10000001,2014,4,10.00,10.00,10.45,10.10,40.55);
END IF;
END $$
DELIMITER ;
You have to call the procedure now:
call example();
Note: This will solve you solution as for now, but it's not the best idea since procedures are intended to get used in the future aswell, so i'll give you an improved version where in the future you will be able to update/insert by just invoking the procedure and wrinting your inserts values.
DELIMITER $$;   
     CREATE PROCEDURE example(
IN
vempid int(8),
vyear year(4), 
vmonth int(2),
vovertime float(10,2),
vmedical float(10,2),
vlunch float(10,2),
vbonus float(10,2),
vallowance float(10,2))
    BEGIN
    DECLARE vexist int;
      SELECT count(*) into vexist FROM Allowance --count because i will
      WHERE EmployeeID =vemp and Year = vyear and Month = vmonth;  --this will check if exist or not
        IF (vexist >= 1) then  --if exist then update
        UPDATE Allowance
            SET OverTime = vovertime, Medical = vmedical, Lunch = vlunch, Bonus = vbonus, Allowance = vallowabce
            WHERE EmployeeID =10000001 and Year = vyear and Month = vmonth;
        ELSE
          INSERT INTO `ALLOWANCE` (`EmployeeID`, `Year`, `Month`, `OverTime`,`Medical`,`Lunch`, `Bonus`, `Allowance`) values (vempid,vyear,vmonth,vovertime,vmedical,vlunch,vbonus,vallowance);
    END IF;
    END $$
    DELIMITER ;
And invoking (with the correct parameters order):
call example2(10000001,2014,4,10.00,10.00,10.45,10.10,40.5);
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With