MySQL Syntax Error When Creating a Trigger - mysql

I am trying to create a trigger but I am getting a syntax error and I am not really sure why I am.
CREATE TRIGGER Section_Insert AFTER INSERT ON Section
-> FOR EACH ROW BEGIN
-> INSERT INTO Audit(changeTime, tableName, Action) VALUES (NOW(), 'Section', 'INSERT');
ERROR 1064 (42000): 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 '' at line 3

The following may be useful:
DELIMITER //
DROP TRIGGER IF EXISTS `Section_Insert`//
DROP TABLE IF EXISTS `Section`//
CREATE TABLE `Section` (
`changeTime` DATETIME NOT NULL,
`tableName` VARCHAR(64) NOT NULL,
`Action` VARCHAR(64) NOT NULL
)//
CREATE TRIGGER `Section_Insert` AFTER INSERT ON `Section`
FOR EACH ROW
BEGIN
INSERT INTO `Audit`(`changeTime`, `tableName`, `Action`) VALUES (NOW(), 'Section', 'INSERT');
END//
DELIMITER ;

Related

Create Procedure to insert values into a table sql

I am attempting to create a procedure that will allow me to insert values into a table
the code I have is
Create procedure add_instructor
#Name char(40),
#IAccount char(20)
AS
BEGIN
set nocount on
insert into final_instructors (Name, IAccount)
values (#name, #IAccount)
end
go;
I am getting this error
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 '#Name char(40), #IAccount char(20)

'WHERE' condition in an UPDATE TRIGGER giving syntax error :mysql version 5.7

I am using mysql database(VERSION 5.7).
I created a table with the following CREATE command:
CREATE TABLE `attendance` (
`id` varchar(11) NOT NULL,
`hash` char(8) NOT NULL,
`time_in` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`time_out` datetime DEFAULT NULL,
`time_spent` time DEFAULT NULL,
`purpose` char(10) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
I have a trigger with the following query:
DELIMITER $$
CREATE TRIGGER test_trigger
BEFORE UPDATE ON attendance
FOR EACH ROW
BEGIN
SET NEW.time_spent=TIMEDIFF(NEW.time_out,OLD.time_in) WHERE OLD.time_spent IS NULL;
END$$
DELIMITER ;
But mysql gives the following error:
ERROR 1064 (42000): 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 'WHERE OLD.time_spent IS NULL; END' at line 1
(The where condition makes sure that only current days records get updated)
How am I supposed to write the query?
You can't use a WHERE clause with SET inside a trigger. Use IF instead:
DELIMITER $$
CREATE TRIGGER test_trigger
BEFORE UPDATE ON attendance
FOR EACH ROW
BEGIN
IF OLD.time_spent IS NULL THEN
SET NEW.time_spent = TIMEDIFF(NEW.time_out, OLD.time_in);
END IF;
END$$
DELIMITER ;

SQL Stored Procedure - Run a SQL Query

Here is the way I am creating my stored procedure:
SET #SQL =
(SELECT ddl_script FROM TABLENAME.versions where ID = 5)
;
USE TABLENAME;
DROP procedure IF EXISTS `ddl_stored_procedure`;
DELIMITER $$
CREATE PROCEDURE `ddl_stored_procedure` ()
BEGIN
PREPARE part1 FROM #SQL;
EXECUTE part1;
DEALLOCATE PREPARE part1;
END$$
DELIMITER ;
This part works fine and my stored procedure is created. Now I try to run my stored procedure with this line of code to test it:
CALL `DBNAME`.`ddl_stored_procedure`();
And I get the error message:
Error Code: 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 'USE `xxx_landing`; CREATE TABLE `TABLE1` ( `ROW1` varchar(1), `T' at line 1
The code that is running, is an SQL statement inside one of my databases as longtext and looks like this:
CREATE DATABASE `xxx_landing` /*!40100 DEFAULT CHARACTER SET utf8 */;
USE `xxx_landing`;
CREATE TABLE `TABLE1` (
`ROW1` varchar(1),
`ROW2` varchar(20),
`ROW3` varchar(3)
)
ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `TABLE2` (
`ROW4` varchar(2),
`ROW5` varchar(10),
`ROW6` varchar(10),
`ROW7` varchar(10),
`ROW8` varchar(10),
`ROW9` varchar(50)
)
ENGINE=InnoDB DEFAULT CHARSET=utf8;
I do not know why the stored procedure fails to create the database, use it and create all tables. If I execute the SQL-Query manually there is no syntax error.
Can someone explain me what I am doing wrong?
Thanks in advance!

creating trigger inserting UNIX_TIMESTAMP() to a log table

Hi im stuck on the syntax of making this trigger to insert values to my log table. Using the sql tab inside phpmyadmin an error appears before executing the sql statement
`#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 '' at line 8 `
here is my sql statement in creating a trigger
CREATE TRIGGER after_insert_list
AFTER INSERT ON list FOR EACH ROW
BEGIN
INSERT INTO log (user_id, action, date_log)
VALUES (
NEW.user_id,
NEW.action,
UNIX_TIMESTAMP()
);
END
Create your trigger like follows:
delimiter |
CREATE TRIGGER testref
BEFORE INSERT ON test1FOR EACH ROW BEGIN
INSERT INTO test2 SET a2 = NEW.a1;
DELETE FROM test3 WHERE a3 = NEW.a1;
UPDATE test4 SET b4 = b4 + 1 WHERE a4 = NEW.a1;
END;
|
delimiter ;
Here is a 100% working example:
Table demo
CREATE TABLE `demo` (
`id` int NOT NULL AUTO_INCREMENT,
`text` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
`updated_at` int NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
);
Trigger on INSERT
DELIMITER ;;
CREATE TRIGGER `updated_at__on_insert`
BEFORE INSERT
ON `demo`
FOR EACH ROW
BEGIN
SET new.updated_at = UNIX_TIMESTAMP();
END;;
DELIMITER ;
Trigger on UPDATE
DELIMITER ;;
CREATE TRIGGER `updated_at__on_update`
BEFORE UPDATE
ON `demo`
FOR EACH ROW
BEGIN
SET new.updated_at = UNIX_TIMESTAMP();
END;;
DELIMITER ;
p.s. I was looking for such a solution for my CMS EFFCORE.

syntax error in mysql during tringger definition

I'm trying to use a trigger defined as follows
-- the table
CREATE TABLE codici_ddt(
id_ordine VARCHAR(15) NOT NULL,
id_invoice VARCHAR(15) NOT NULL,
ddt_numero INT(8) NOT NULL,
fatturazione DATE NOT NULL,
ddt VARCHAR(20) NOT NULL,
FOREIGN KEY(id_ordine) REFERENCES ordini_dal_web(id_ordine),
PRIMARY KEY(id_ordine)
);
--the_trigger
DELIMITER $$
CREATE TRIGGER genera_numero_ddt BEFORE INSERT ON codici_ddt FOR EACH ROW
BEGIN
DECLARE ultimo_ddt INT(8);
SELECT COALESCE(max(ddt_numero),1) INTO ultimo_ddt
FROM codici_ddt
WHERE data_fatturazione >= MAKEDATE(YEAR(NEW.data_fatturazione) ,1)
AND data_fatturazione < MAKEDATE(YEAR(NEW.data_fatturazione)+1,1);
SET NEW.ddt_numero = (ultimo_ddt+1)
SET NEW.ddt = CONCAT(NEW.ddt_numero,'/',(SUBSTRING(SUBSTRING_INDEX(NEW.data_fatturazione,'-',1),-2)),'c');
END $$
DELIMITER ;
the message returned from mysql is
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 'SET NEW.ddt =
CONCAT(NEW.ddt_numero,'/',(SUBSTRING(SUBSTRING_INDEX(NEW.data_fatt' at
line 11
the CONCAT should be right, where is my error?
many thanks!
You miss ';' at the end of line.
SET NEW.ddt_numero = (ultimo_ddt+1);
Missing semi-colon. Also you are referencing data_fatturazione instead of just fatturazione.
Try this:
DELIMITER $$
CREATE TRIGGER genera_numero_ddt BEFORE INSERT ON codici_ddt FOR EACH ROW
BEGIN
DECLARE ultimo_ddt INT(8);
SELECT COALESCE(max(ddt_numero),1) INTO ultimo_ddt FROM codici_ddt
WHERE fatturazione >= MAKEDATE(YEAR(NEW.fatturazione) ,1)
AND fatturazione < MAKEDATE(YEAR(NEW.fatturazione)+1,1);
SET NEW.ddt_numero = (ultimo_ddt+1);
SET NEW.ddt = CONCAT(NEW.ddt_numero,'/',(SUBSTRING(SUBSTRING_INDEX(NEW.fatturazione,'-',1),-2)),'c');
END $$
DELIMITER ;