MySql trigger not working, syntax error - php

I have tried to create the following trigger but it is not working:
delimiter //
CREATE TRIGGER DeleteFeature
AFTER DELETE ON Features
FOR EACH ROW BEGIN
IF (OLD.FeatureID IN (SELECT FeatureID FROM GroupFeatures)) THEN
INSERT INTO DeletedFeatures VALUES (OLD.FeatureID, OLD.FeatureName, OLD.FeatureDescription);
END IF;
END;
//
It is giving the following error:
"#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 "
Any ideas what the problem could be?

Have you checked by removing the semi-colon and add $$ after the END
DELIMITER $$
CREATE TRIGGER DeleteFeature
AFTER DELETE ON Features
FOR EACH ROW
BEGIN
IF (OLD.FeatureID IN (SELECT FeatureID FROM GroupFeatures)) THEN
INSERT INTO DeletedFeatures VALUES (OLD.FeatureID, OLD.FeatureName OLD.FeatureDescription);
END IF;
END $$
DELIMITER ;

Related

SQL error when running command from php file

I need to check if a column exists before trying to add it if not present.
Following information found in this post: mysql ALTER TABLE if column not exists
I added the following to my zen cart php file
$db->Execute("DROP PROCEDURE IF EXISTS `gdpr_accept`;
DELIMITER //
CREATE PROCEDURE `gdpr_accept`()
BEGIN
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION BEGIN END;
ALTER TABLE " . TABLE_CUSTOMERS . " ADD `gdpr_accept` TINYINT(1) NOT NULL DEFAULT '0' AFTER `COWOA_account`;
END //
DELIMITER ;
CALL `gdpr_accept`();
DROP PROCEDURE `gdpr_accept`;");
However, I get the following error logged
[05-May-2018 19:37:02 Europe/Paris] PHP Fatal error: 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 'DELIMITER //
CREATE PROCEDURE gdpr_accept()
BEGIN
' at line 2 :: DROP PROCEDURE IF EXISTS gdpr_accept;
DELIMITER //
CREATE PROCEDURE gdpr_accept()
BEGIN
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION BEGIN END;
ALTER TABLE customers ADD gdpr_accept TINYINT(1) NOT NULL DEFAULT '0' AFTER COWOA_account;
END //
DELIMITER ;
CALL gdpr_accept();
DROP PROCEDURE gdpr_accept; ==> (as called by) /Applications/MAMP/htdocs/gdpr/stOrm-PTO-fluSh/includes/init_includes/init_reconsent_popup_setup.php on line 72
However, when I run the same command in phpMyAdmin, after confirming that i want to "DROP PROCEDURE IF EXISTS gdpr_accept" it runs perfectly.
Note: If i attempt to split up the query, it will fail at
$db->Execute("DELIMITER //");
with this error: 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 'DELIMITER //' at line 1
Is there a reason why this SQL command can't be done via php, and is there a way round it?
DELIMITER is a builtin command in the mysql client, it is not a statement that the MySQL server recognizes. You can't run a DELIMITER statement using PHP.
But you don't need to use DELIMITER. That's only to help the mysql client tell where your CREATE PROCEDURE statement ends, because a procedure usually contains semicolon characters, and otherwise it would be ambiguous which semicolon was part of the procedure body versus the end of the procedure definition.
You should run one statement at a time from PHP. Then it's not ambiguous.
$db->Execute("DROP PROCEDURE IF EXISTS `gdpr_accept`");
$db->Execute("CREATE PROCEDURE `gdpr_accept`()
BEGIN
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION BEGIN END;
ALTER TABLE " . TABLE_CUSTOMERS . " ADD `gdpr_accept` TINYINT(1) NOT NULL DEFAULT '0' AFTER `COWOA_account`;
END");
$db->Execute("CALL `gdpr_accept`()");
$db->Execute("DROP PROCEDURE `gdpr_accept`;");
By the way, there's no reason you need a procedure for this task, since you just drop the procedure when you're done anyway. It would be much simpler to just run the ALTER TABLE directly:
$db->Execute("ALTER TABLE " . TABLE_CUSTOMERS .
" ADD `gdpr_accept` TINYINT(1) NOT NULL DEFAULT '0' AFTER `COWOA_account`");
I see a lot of questions on Stack Overflow from people who seem to think it's a good idea to use stored procedures in MySQL. While stored procedures are common in Oracle and Microsoft SQL Server, they're more trouble than they're worth in MySQL. I avoid using stored procedures in MySQL.

mysqlprocedure weird mistake on localhost

I'm trying to change charset of my mysql database using a procedure.
I wrote it few years ago and did not go back to it, till today.
By this I have a weird mistake :
below is my procedure :
Create procedure changecharset()
Begin
Declare tname varchar(255);
Declare done int default 0;
Declare tc Cursor For
Select TABLE_NAME From information_schema.TABLES Where TABLE_SCHEMA = schema();
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1;
Open tc;
myloop: loop
Fetch tc into tname;
If done = 1 Then
Leave myloop;
End if ;
Set #query = Concat('Alter Table `', tname, '` convert to character set utf8 collate utf8_swedish_ci;');
PREPARE alterstmt FROM #query;
execute alterstmt ;
Deallocate prepare alterstmt;
End loop;
close tc;
End //
Delimiter ;
[/sql]
and this is the answer of mysql server
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 3
Anykind of help will be much appreciated
It looks like MySQL is seeing the semicolon as the end of the statement.
Make sure you change the delimiter before running the CREATE PROCEDURE statement.
DELIMITER $$
CREATE PROCEDURE foo()
BEGIN
DECLARE ... ;
SET ... ;
END $$
DELIMITER ;

Mysql Stored Procedure Error

Please need help:
DELIMITER $$
CREATE PROCEDURE `getTodayCheckOuts`
BEGIN
SELECT `tec_check_out`.`date`,`tec_check_out_items`.`check_out_id`,`tec_check_out_items`.`item_id`,CONCAT(`tec_items`.`name`,' - ',`tec_items`.`code`) AS 'name',SUM(`tec_check_out_items`.`quantity`) AS 'totla_qty',SUM(`tec_check_out_items`.`quantity`*`tec_check_out_items`.`price`) AS 'total_price'
FROM `tec_check_out_items`, `tec_items`, `tec_check_out`
WHERE `tec_check_out_items`.`item_id`=`tec_items`.`id`
AND `tec_check_out_items`.`check_out_id`=`tec_check_out`.`id`
AND DATE(`tec_check_out`.`date`) = DATE(NOW())
GROUP BY `tec_check_out_items`.`item_id`
END $$
DELIMITER ;
keep getting this error
Error
Static analysis:
2 errors were found during analysis.
Unrecognized data type. (near "." at position 163)
Unrecognized data type. (near "," at position 176)
#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 'BEGIN
SELECT `tec_check_out`.`date`,`tec_check_out_items`.`check_out_id`,`tec_' at line 2
Change:
/*
CREATE PROCEDURE `gettodaycheckouts`
*/
by:
CREATE PROCEDURE `gettodaycheckouts`()
and
/*
GROUP BY `tec_check_out_items`.`item_id`
*/
by:
GROUP BY `tec_check_out_items`.`item_id`;

Showing me error while executing this procedure in MySQL Workbench

DELIMITER //
CREATE PROCEDURE Sample
BEGIN
DECLARE v_SQLSTR VARCHAR(800);
SET v_SQLSTR='Hi';
END;
//
DELIMITER ;
Error Details: 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 'BEGIN DECLARE v_SQLSTR VARCHAR(800); SET
v_SQLSTR='Hi'; END' at line 2
I am new in MySQL. Please help.
Missed the brackets. Try this:
...
CREATE PROCEDURE Sample()
...
That is because you have a syntax error:
Add () to your procedure's name
Remove ; after the END keyword
Change your code to:
DELIMITER //
CREATE PROCEDURE Sample()
BEGIN
DECLARE v_SQLSTR VARCHAR(800);
SET v_SQLSTR='Hi';
END //
DELIMITER ;
To avoid further errors, you can check the official documentation.

Trigger syntax error in phpmyadmin version 5.1

SQL Query -
CREATE TRIGGER `trigger_insert` AFTER INSERT ON `user`
FOR EACH ROW BEGIN
INSERT INTO `credentials` (`UserId`,`Password`,`UserType`,`Status`)
VALUES (NEW.UserId,NEW.Password,'2',NEW.Status);
END;
DELIMITER ;
Error -
#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 3
Need help... thanks in advance :)..
For your case you can use this statement -
CREATE TRIGGER `trigger_insert` AFTER INSERT ON `user`
FOR EACH ROW
INSERT INTO `credentials` (`UserId`,`Password`,`UserType`,`Status`) VALUES (NEW.UserId,NEW.Password,'2',NEW.Status);
What is the DELIMITER in MySQL and what it’s used for.
Delimiter problems I reckon:
DELIMITER $$
CREATE TRIGGER `trigger_insert` AFTER INSERT ON `user`
FOR EACH ROW BEGIN
INSERT INTO `credentials` (`UserId`,`Password`,`UserType`,`Status`) VALUES (NEW.UserId,NEW.Password,'2',NEW.Status);
END$$
DELIMITER ;

Categories