I must be missing something simple because I can't figure out what is causing my script to fail.
Below is the stored procedure I've written:
CREATE PROCEDURE `Search_contacts`(IN `in_owner_id` INT,
IN `in_first_name` VARCHAR(255))
IF in_first_name IS NOT NULL THEN
SELECT * FROM `contacts`
WHERE `owner_id` = in_owner_id AND `first_name` LIKE in_first_name;
END IF;
When I try and execute this on my MySQL server I get the following 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 5
I'd like to know what is causing this error and why so I can avoid it again.
Any help is appreciated!
Try adding "BEGIN", "END" and "DELIIMITER", like this:
DELIMITER $$
CREATE PROCEDURE `Search_contacts`(IN `in_owner_id` INT,
IN `in_first_name` VARCHAR(255))
BEGIN
IF in_first_name IS NOT NULL THEN
SELECT * FROM `contacts`
WHERE `owner_id` = in_owner_id AND `first_name` LIKE in_first_name;
END IF;
END $$
DELIMITER ;
Related
I'm getting the below error when running the below. Looking at the code it looks correct to me. I'm not fully sure though.
Stored procedure creation failed: (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 ALTER TABLE sitesettings ADD backgroundColor varchar(255) DEFAULT '
CREATE PROCEDURE p()
BEGIN
IF COL_LENGTH('sitesettings', 'backgroundColor') IS NULL
BEGIN
ALTER TABLE sitesettings ADD backgroundColor varchar(255) DEFAULT '#202225';
END
IF COL_LENGTH('sitesettings', 'logintype') IS NULL BEGIN
ALTER TABLE sitesettings ADD logintype INT DEFAULT 1
END
IF NOT EXISTS (SELECT 'X'
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = 'classicusers')
BEGIN
CREATE TABLE classicusers(
id TEXT NOT NULL,
username TEXT NOT NULL,
password TEXT NOT NULL);
END
END;
I removed all syntax errors, si taht at least Workbench don't mind
The Syntax for IF is
IF condition THEN
-- do something
END IF
Everything else results in errors
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `p`()
BEGIN
IF COL_LENGTH('sitesettings', 'backgroundColor') IS NULL THEN
BEGIN
ALTER TABLE sitesettings ADD backgroundColor varchar(255) DEFAULT '#202225';
END;
END IF;
IF COL_LENGTH('sitesettings', 'logintype') IS NULL THEN
BEGIN
ALTER TABLE sitesettings ADD logintype INT DEFAULT 1 ;
END;
END IF;
IF NOT EXISTS (SELECT 'X'
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = 'classicusers') THEN
BEGIN
CREATE TABLE classicusers(
id TEXT NOT NULL,
username TEXT NOT NULL,
password TEXT NOT NULL);
END;
END IF;
END$$
DELIMITER ;
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!
I am using MySQL Workbench 8.0 CE and i'm trying to create a stored procedure to show 2 fields from my table. I am receiving the following error:
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 3
This is my table:
CREATE TABLE student (
id INT PRIMARY KEY,
name VARCHAR(200),
age INT,
final_grade DOUBLE,
sex VARCHAR(1)
)
And this is the procedure:
CREATE PROCEDURE show_name_grade ()
BEGIN
SELECT name,final_grade FROM student;
END
You will need to redefine Delimiter to something else other than ;. At the end, define it back to ;
DELIMITER $$
CREATE PROCEDURE show_name_grade ()
BEGIN
SELECT name,final_grade FROM student;
END $$
DELIMITER ;
I have the following procedure that when I manually import this is breaking for some unknown reason.
CREATE PROCEDURE `register_house`(
IN UID CHAR(17),
IN new_username VARCHAR(16),
IN new_signature CHAR(64),
IN email VARCHAR(128),
IN postcode VARCHAR(16),
IN customer_name VARCHAR(45),
IN phone_number VARCHAR(16)
)
BEGIN
UPDATE bb.checkin SET username = new_username, signature = new_signature WHERE _site = UID;
END
I'm getting the following error in Mysql Workbench -
Error Code: 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 11
Can someone suggest what the problem is?
UPDATE
As suggested i've amended to include the DELIMITER and now get the following error:
Error Code: 1728. Cannot load from mysql.proc. The table is probably corrupted
The tables all appear to look correct is the way to 'de-corrupt' them if they have corrupted somehow?
Based on https://stackoverflow.com/a/639356/2381157, try this
delimiter //
CREATE PROCEDURE `register_house2`(
IN UID CHAR(17),
IN new_username VARCHAR(16),
IN new_signature CHAR(64),
IN email VARCHAR(128),
IN postcode VARCHAR(16),
IN customer_name VARCHAR(45),
IN phone_number VARCHAR(16)
)
BEGIN
UPDATE bb.checkin SET username = new_username, signature = new_signature WHERE _site = UID;
END
//
delimiter ;
It worked for me in MySQL 5.5
This worked for me...
Added this to the start of the statement on line 1;
DELIMITER $$
then on the ver
END $$
Then finally to fix the 'corrupted' message I simply ran the following file:
C:\xampp\mysql\bin\mysql_upgrade.exe
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 ;