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.
Related
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.
I'm working on a php project and i'm still a novice in error debugging.
whenever i try to execute the following code
CREATE PROCEDURE insertData(Name varchar(255),Description text)
BEGIN
INSERT INTO categories(name,description) VALUES (Name,Description);
END
i get an error stating (in phpmyadmin)
MySQL said: Documentation
#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 '' at line 3
Try using a DELIMITER statement:
DELIMITER $$
CREATE PROCEDURE insertData (
in_Name varchar(255),
in_Description text
)
BEGIN
INSERT INTO categories(name, description)
VALUES (in_Name, in_Description);
END;$$
DELIMITER ;
Notice that I also renamed in the input parameters so they are less likely to be confused with column names. This is a good practice when writing stored procedures and functions.
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`;
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 ;
I successfully use the below run_sql_script() function to execute SQL in a file using PHP.
static public function run_sql_script($script)
{
// Load and explode the sql file
$f = fopen($script,"r+");
$sqlFile = fread($f,filesize($script));
$sqlFile=preg_replace("/\\\;/", ';', $sqlFile); //replace semicolons with ascii
$sqlArray = explode(';',$sqlFile);
//Process the sql file by statements
foreach ($sqlArray as $stmt)
{
if (strlen($stmt)>8)
{
//Used to prevent blank lines at end of sql script from executing
$stmt=preg_replace("/;/", ';', $stmt); //replace ascii with semicolons
try {db::db()->exec($stmt);}
catch(PDOException $e){library::sql_error($e,$stmt);}
}
}
return;
}
I generate the SQL using MySQL Workbench, and recently, I added a trigger also through MySQL Workbench. It added the following SQL to the file.
DELIMITER $$
CREATE TRIGGER tg_contacts_upd AFTER UPDATE ON contacts
FOR EACH ROW
....
BEGIN
END$$
DELIMITER ;
Upon running the new file through my run_sql_script() function, I now get the following error.
Error in query: DELIMITER $$ CREATE TRIGGER tg_contacts_upd AFTER
UPDATE ON contacts FOR EACH ROW BEGIN END$$ DELIMITER SQLSTATE[42000]:
Syntax error or access violation: 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 TRIGGER
tg_contacts_upd AFTER UPDATE ON contacts FOR EACH ' at line 1
Any suggestion how to fix
Seems too easy, but the following appears to work.
static public function run_sql_script($script)
{
$sql = file_get_contents($script);
db::db()->exec($sql);
}