Issue in create trigger phpmyadmin - mysql

I have a problem creating a trigger in phpmyadmin. Here is my syntax:
DECLARE n_ubicacion VARCHAR(45) DEFAULT '';
IF (OLD.id_ubicacion <> NEW.id_ubicacion) THEN
SELECT ubicacion INTO n_ubicacion FROM ubicacions WHERE id = NEW.id_ubicacion LIMIT 1;
END IF;
INSERT INTO historico_equipos(usuario, id_equipo, estado, ubicacion, empleado, f_asignacion, created_at) VALUES (CURRENT_USER(), NEW.id, NEW.id_estado, n_ubicacion, NEW.id_empleado, NEW.f_asignacion, CURRENT_DATE())
I don''t know where is the problem. In phpmyadmin show the next error:
Something is wrong in your syntax near 'DECLARE n_ubicacion VARCHAR(45) DEFAULT ''; IF (OLD.id_ubicacion <> NEW.id_ubicacion) THEN'
Thanks.

Check if any other statements are present inside the Trigger above the DECLARE statement. If so, then it may throw the error.
Check your delimiter. By default, MySQL's delimiter is ';'. So, on seeing the first ';', your query will break. Hence use some other delimiter before creating the trigger.
Example:
DELIMITER //
CREATE TRIGGER ...
BEGIN
DECLARE n_ubicacion VARCHAR(45) DEFAULT '';
IF (OLD.id_ubicacion <> NEW.id_ubicacion) THEN
SELECT ubicacion INTO n_ubicacion FROM ubicacions WHERE id =
NEW.id_ubicacion LIMIT 1;
END IF;
INSERT INTO historico_equipos(usuario, id_equipo, estado, ubicacion, empleado, f_asignacion, created_at) VALUES (CURRENT_USER(), NEW.id, NEW.id_estado, n_ubicacion, NEW.id_empleado, NEW.f_asignacion, CURRENT_DATE())
END//
Then again change the DELIMITER to default.
DELIMITER ;

Related

how to declare variable phpmyadmin mysql triggers properly?

I have a database where whenever residential address update in user table I want it to store in history table of user. For that I'm trying to write triggers but failing miserably in phpmyadmin. Also it's not giving me proper reason why so I can correct it. This is what I have done so far.
DROP TRIGGER IF EXISTS `record_history`;
CREATE TRIGGER `record_history` AFTER UPDATE ON `s_user`
FOR EACH ROW
BEGIN
DECLARE date_current datetime;
DECLARE residential_address varchar(1000);
SET #date_current = NOW();
SET #residential_address = NEW.residential_address;
IF (#residential_address <> OLD.residential_address AND #residential_address != "" AND #residential_address IS NOT NULL) THEN
INSERT INTO history_residential_address (`s_u_id`, `residential_address`, `status`, `date_added`, `date_updated`) VALUES
(OLD.s_u_id, #residential_address, 1, #date_current, #date_current);
END IF;
END;
delimiter ;
A cleaner version of your code
DROP TRIGGER IF EXISTS `record_history`;
delimiter $$
CREATE TRIGGER `record_history` AFTER UPDATE ON `s_user`
FOR EACH ROW
BEGIN
IF (new.residential_address <> OLD.residential_address AND new.residential_address <> "" AND new.residential_address IS NOT NULL) THEN
INSERT INTO history_residential_address (`s_u_id`, `residential_address`, `status`, `date_added`, `date_updated`) VALUES
(OLD.s_u_id, new.residential_address, 1, now(), now());
END IF;
END $$
delimiter ;
If you are still having problems please add sample data from s_user as text to the question.

undefined error with MySql insert trigger

I'm trying to write a MySQL insert trigger:
CREATE TRIGGER `trigger1` BEFORE INSERT ON `COMPANY`
FOR EACH ROW
IF NEW.DOKUMENTTYP LIKE 'test'
AND NEW.STATUS LIKE 'neu'
THEN
INSERT INTO my_tools.testimport( processname, version, step, insstring, initiator )
VALUES ( 'COMPANY_ER', 0, 1, CONCAT( 'DWDOCID=', NEW.DWDOCID ) , 'robot' );
END IF;
But there's an error in my SQL syntax. I cannot find the solution. Could anybody please help me?
Try this
IF (NEW.DOKUMENTTYP LIKE 'test' AND NEW.STATUS LIKE 'neu') ...
Triggers body contains more then one statement, it means that BEGIN..END keywords must be used. And add DELIMITERs, if needed:
DELIMITER $$
CREATE TRIGGER `trigger1`
BEFORE INSERT
ON `Company`
FOR EACH ROW
BEGIN
IF NEW.DOKUMENTTYP LIKE 'test' AND NEW.Status LIKE 'neu' THEN
INSERT INTO my_tools.testimport (processname, VERSION, step, insstring, initiator)
VALUES ('COMPANY_ER', 0, 1, CONCAT('DWDOCID=', NEW.DWDOCID), 'robot');
END IF;
END$$
DELIMITER ;

Trigger (AFTER/BEFORE) In Mysql

I have a trigger which is working fine.
CREATE TRIGGER crm_listings__au
AFTER UPDATE
ON crm_listings FOR EACH ROW
INSERT INTO crm_listings_versions
SELECT
'update', NULL, NOW(), NULL, d.*
FROM
crm_listings AS d
WHERE
d.id = NEW.id;
Now I want to keep track of the field column name also. I am thinking I could not do in above query so I changed to below trigger
CREATE TRIGGER crm_listings__au
BEFORE UPDATE
ON crm_listings
FOR EACH ROW
BEGIN
IF OLD.type != NEW.type
THEN
INSERT INTO crm_listings_versions
SELECT
'update', NULL, NOW(), 'type', d.*
FROM
crm_listings AS d
WHERE
d.id = NEW.id;
END IF;
IF OLD.price != NEW.price
THEN
INSERT INTO crm_listings_versions
SELECT
'update', NULL, NOW(), 'price', d.*
FROM
crm_listings AS d
WHERE
d.id = NEW.id;
END IF;
END;
$$
When I run this code, I get 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 '' at line 10
UPDATE:
I followed this post on stackoverflow
#kordirko: Can you please explain a little bit?
Please study the documentation: http://dev.mysql.com/doc/refman/5.7/en/stored-programs-defining.html
If you use the mysql client program to define a stored program
containing semicolon characters, a problem arises. By default, mysql
itself recognizes the semicolon as a statement delimiter, so you must
redefine the delimiter temporarily to cause mysql to pass the entire
stored program definition to the server.
To redefine the mysql delimiter, use the delimiter command. The
following example shows how to do this for the dorepeat() procedure
just shown. The delimiter is changed to // to enable the entire
definition to be passed to the server as a single statement, and then
restored to ; before invoking the procedure. This enables the ;
delimiter used in the procedure body to be passed through to the
server rather than being interpreted by mysql itself.
Simple example - I am using MySql Workbench and have copied and pasted your trigger. First some dummy tables:
create table crm_listings(
id int,
type int,
price int
);
create table crm_listings_versions(
ttype varchar(100),
something varchar(100),
d date,
something1 varchar(100),
id int,
type int,
price int
);
And now I run your code without DELIMITER
CREATE TRIGGER crm_listings__au BEFORE UPDATE ON crm_listings
FOR EACH ROW
BEGIN
IF OLD.type != NEW.type
THEN
INSERT INTO crm_listings_versions SELECT 'update', NULL, NOW(), 'type', d.* FROM crm_listings AS d WHERE d.id = NEW.id;
END IF;
IF OLD.price != NEW.price
THEN
INSERT INTO crm_listings_versions SELECT 'update', NULL, NOW(), 'price', d.* FROM crm_listings AS d WHERE d.id = NEW.id;
END IF;
END;
$$
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 '' at line 10 0.000 sec
Outcome = Error
Then the same, but with the DELIMITER command:
DELIMITER $$
CREATE TRIGGER crm_listings__au BEFORE UPDATE ON crm_listings
FOR EACH ROW
BEGIN
IF OLD.type != NEW.type
THEN
INSERT INTO crm_listings_versions SELECT 'update', NULL, NOW(), 'type', d.* FROM crm_listings AS d WHERE d.id = NEW.id;
END IF;
IF OLD.price != NEW.price
THEN
INSERT INTO crm_listings_versions SELECT 'update', NULL, NOW(), 'price', d.* FROM crm_listings AS d WHERE d.id = NEW.id;
END IF;
END;
$$
DELIMITER ;
0 row(s) affected
Outcome = Success

MySQL stored Proc syntax confusion

Using syntax form docs, read many examples and docs, I have reduced code down to isolate the syntax that is causing the failure, I cannot see it... I'm still getting an error.
Running thru MySQL command line
PARAMS: A_score smallint, B_score smallint
delimiter $$
create procedure my_procedure(A_score smallint, B_score smallint)
begin
DECLARE winner BIGINT DEFAULT 0;
DECLARE winningScore, losingScore SMALLINT DEFAULT;
if A_score > B_score then
SET winningScore = 1;
elseif A_score < B_score then
SET winningScore = 2;
end if;
start transaction;
UPDATE
winners
SET
winner = winningScore
WHERE
id = 1
commit;
end $$
delimiter ;
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 ';
if A_score > B_score then
SET winningScore = 1;
elseif A_score' at line 4
I see you forgot to write SET (in the previous version of your question) to assign values to your variables.
(I changed the type for winningScore and losingScore to be characters, because smallints can't be strings):
-- Be sure to change the default delimiter before writing your procedure
delimiter $$
create procedure my_procedure(A_score smallint, B_score smallint)
begin
DECLARE winner BIGINT DEFAULT 0;
DECLARE winningScore, losingScore VARCHAR(2) DEFAULT;
if A_score > B_score then
SET winningScore = 'A';
-- ^^^--you forgot this
elseif A_score < B_score then
SET winningScore = 'B';
-- ^^^--and this
else
SET winningScore = 'AB';
-- ^^^--and this
end if;
start transaction;
-- Do whatever your transaction is meant to be
commit;
end $$
-- ^^--- And I think you're forgetting to put this
-- Be sure to reset the delimiter to ; after you end your procedure
delimiter ;
Quoting from the reference manual:
Variables can be set directly with the SET statement. See Section 13.7.4, “SET Syntax”.
Hope this helps

Cant get Mysql trigger statement right

Can someone please tell me whats wrong with this trigger statement?
DELIMITER //
CREATE TRIGGER something AFTER INSERT ON sometable
FOR EACH ROW
BEGIN
DECLARE var INT DEFAULT 0;
SET var = SELECT COUNT(*) FROM anothertable;
IF var=0 THEN
INSERT INTO anothertable values(`x`,`y`,`z`);
END IF;
END//
I keep getting error saying i have syntax errors...
Try putting the SELECT statement within brackets:
DELIMITER //
CREATE TRIGGER something AFTER INSERT ON sometable
FOR EACH ROW
BEGIN
DECLARE var INT DEFAULT 0;
SET var = (SELECT COUNT(*) FROM anothertable);
IF var=0 THEN
INSERT INTO anothertable values('x','y','z');
END IF;
END//