I am getting 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 '' at line 4
when i run followed code in phpmyadmin also appearing this error beside of second declare expression:
unrecognized statement type (near declare)
What can be cause of these error? (Phpmyadmin, version, etc)
create function fnc_generate_url(title_in varchar(250))
returns varchar(250)
begin
declare v_count int;
declare v_return varchar(250);
declare cr_count cursor
for
select count(1) from tbl_page where page_title like concat('%',title_in,'%');
open cr_count;
fetch cr_count into v_count;
close cr_count;
if v_count = 0 then
set v_return = replace(trim(title_in), ' ', '-');
else
set v_return = concat(replace(trim(title_in), ' ', '-'),'-',v_count);
end if;
return v_return;
end;
Works fine if we use delimiters.
Try this:
DELIMITER $$
create function fnc_generate_url(title_in varchar(250))
returns varchar(250)
begin
declare v_count int;
declare v_return varchar(250);
declare cr_count cursor
for
select count(1) from tbl_page where page_title like concat('%',title_in,'%');
open cr_count;
fetch cr_count into v_count;
close cr_count;
if v_count = 0 then
set v_return = replace(trim(title_in), ' ', '-');
else
set v_return = concat(replace(trim(title_in), ' ', '-'),'-',v_count);
end if;
return v_return;
end;
$$
DELIMITER ;
Related
friends.
I am trying to create function in MySQL using this script:
CREATE FUNCTION CurrentMemoDepartment(MID INT)
RETURNS INT
BEGIN
DECLARE DeptID INT;
SET DeptID = 0;
SELECT TOP 1 TDeptID INTO DeptID FROM Transactions WHERE MemoID = MID ORDER BY ID DESC;
RETURN DeptID;
END
but it says:
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 4
Single-statement function does not need in BEGIN-END, intermediate variables, DELIMITER reassing.
And MySQL uses LIMIT, not TOP n.
CREATE FUNCTION CurrentMemoDepartment(MID INT)
RETURNS INT
RETURN SELECT TDeptID
FROM Transactions
WHERE MemoID = MID
ORDER BY ID DESC
LIMIT 1;
USE DELIMITER for declaring
like
DELIMITER $$
CREATE FUNCTION CurrentMemoDepartment(MID INT)
RETURNS INT
BEGIN
DECLARE DeptID INT;
SET DeptID = 0;
SELECT TDeptID INTO DeptID FROM Transactions WHERE MemoID = MID ORDER BY ID DESC LIMIT 1;
RETURN DeptID;
END$$
DELIMITER ;
I have table name "tmp1" which has fields name id and comma. comma contains 1,3,2,4,5 values with text datatype.
I want separate value in loop in my stored procedure. Like 1 then 3 then 2 etc. So, I was create below given procedure.
BEGIN
DECLARE my_delimiter CHAR(1);
DECLARE split_comma text;
DECLARE done INT;
DECLARE occurance INT;
DECLARE i INT;
DECLARE id INT;
DECLARE sel_query VARCHAR(500);
DECLARE splitter_cur CURSOR FOR SELECT id,comma from tmp1;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;
OPEN splitter_cur;
splitter_loop:LOOP
FETCH splitter_cur INTO id,split_comma;
SET occurance = LENGTH(split_comma) - LENGTH(REPLACE(split_comma,',',''))+1;
SET my_delimiter=',';
IF done = 1 THEN
LEAVE splitter_loop;
END IF;
IF occurance > 0 THEN
SET i = 1;
WHILE i <= occurance DO
SET sel_query = "SELECT SUBSTRING_INDEX(comma,',',i) as abc from tmp1";
SET #sel_query = sel_query;
PREPARE sel_query FROM #sel_query;
EXECUTE sel_query;
SET i = i + 1;
END WHILE;
END IF;
SET occurance = 0;
END LOOP;
CLOSE splitter_cur;
END;
But when I execute this procedure error occurs "MySQL said: #1054 - Unknown column 'i' in 'field list'"
But here i is a variable used to rotate loop.
Is there any solution? Please help me ... Thank you in advance ...
You have to embed value of variable i in the query, which you did not do.
Change:
SET sel_query = "SELECT SUBSTRING_INDEX(comma,',',i) as abc from tmp1";
SET #sel_query = sel_query;
To:
SET #sel_query = CONCAT( "SELECT SUBSTRING_INDEX(comma,',',", i, ") as abc from tmp1 )";
I have a stored procedure to fetch an id. It concat's the last name with first name as "Stone, Cold" and compares with the _fullname passed which when i var_dump gives
String(13) Stone, Cold
It should give an id
IF NOT ISNULL(_fullname) THEN
SET _fullname = TRIM(_fullname);
SET clause = CONCAT( clause , ' AND CONCAT(c.lname, ', ', c.fname) LIKE CONCAT('%',_fullname,'%')');
END IF;
When i try the same query in MySQL it works perfectly fine but doesn't work in procedure. I'm sure that the problem is syntax in stored procedure.
Try:
DELIMITER //
DROP PROCEDURE IF EXISTS `sp_test`//
CREATE PROCEDURE `sp_test`(IN `_fullname` VARCHAR(20))
BEGIN
DECLARE `clause` VARCHAR(500) DEFAULT '';
IF NOT ISNULL(`_fullname`) THEN
SET `_fullname` := TRIM(`_fullname`);
SET `clause` := CONCAT(`clause`, ' AND CONCAT(`c`.`lname`, '', '', `c`.`fname`) LIKE ''', '%', `_fullname`, '%''');
END IF;
SELECT `clause`;
END//
DELIMITER ;
SQL Fiddle demo
trying to create a stored procedure through mysqli in php. Keep throwing back at 1064 error when I do it through php, but is fine, when I copy the same sql command in php. Anyone able to shed some light on what could be the problem, or anyone found a solution to something similar?
Create Procedure code:
DELIMITER //
CREATE PROCEDURE 6de443aacf727f6009e857480f33153cmakeLive (IN taskID INT, IN avaliableTime INT)
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE a INT;
DECLARE cur1 CURSOR FOR SELECT t.`task_id`
FROM `task_dependency` d
LEFT OUTER JOIN (
SELECT `task_id`, `dependent_on_task_id`, ti.`id`, min(ti.`completed`) as make_live
FROM `task_dependency`
LEFT OUTER JOIN (
SELECT `task_id`, `completed` FROM `6de443aacf727f6009e857480f33153c_tasks`
) ti ON ti.`task_id` = `dependent_on_task_id`
GROUP BY `task_id`
) t ON d.`task_id` = t.`task_id`
WHERE d.`dependent_on_task_id` = taskID
AND t.make_live > 0;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur1;
read_loop: LOOP
FETCH cur1 INTO a;
IF done THEN
LEAVE read_loop;
END IF;
UPDATE `6de443aacf727f6009e857480f33153c_tasks` SET `live` = 1 WHERE `task_id` = a;
END LOOP;
CLOSE cur1;
UPDATE `6de443aacf727f6009e857480f33153c_tasks` SET `earliest_start` = avaliableTime WHERE `task_id` IN (
SELECT `task_id`
FROM `task_dependency`
WHERE `dependent_on_task_id` = taskID
) AND `earliest_start` < avaliableTime;
END //
DELIMITER ;
PHP Code:
if (!$this->dbc->query("DROP PROCEDURE IF EXISTS " . $this->table_prefix . "makeLive") ||
!$this->dbc->query($query)) {
echo "Stored procedure creation failed: (" . $this->dbc->errno . ") " . $this->dbc->error . "<br /><br />" . $query;
exit;
}
echo "success";
exit;
Script execution is throwing out the following error...
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 'DELIMITER //
CREATE PROCEDURE 6de443aacf727f6009e857480f33153cmakeLive (IN tas' at line 1
Although it says it's a syntax error, the fact a straight copy and paste of the command to phpmyadmin seems to work, looks like it's gotta be some sort of mysqli bug perhaps?
The query function only allows you to execute a single query. Try the multi_query function (documentation: http://www.php.net/manual/en/mysqli.multi-query.php ) instead. It allows for multiple commands, and may fix your issue.
Turns out mysqli->query already parses semi-colons and runs the whole command. The delimiter commands at the start and end of the query where unnecessary in successfully creating the procedure.
Just remove DELIMITER and it will work. So in your case it will be like this:
CREATE PROCEDURE 6de443aacf727f6009e857480f33153cmakeLive (IN taskID INT, IN avaliableTime INT)
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE a INT;
DECLARE cur1 CURSOR FOR SELECT t.`task_id`
FROM `task_dependency` d
LEFT OUTER JOIN (
SELECT `task_id`, `dependent_on_task_id`, ti.`id`, min(ti.`completed`) as make_live
FROM `task_dependency`
LEFT OUTER JOIN (
SELECT `task_id`, `completed` FROM `6de443aacf727f6009e857480f33153c_tasks`
) ti ON ti.`task_id` = `dependent_on_task_id`
GROUP BY `task_id`
) t ON d.`task_id` = t.`task_id`
WHERE d.`dependent_on_task_id` = taskID
AND t.make_live > 0;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur1;
read_loop: LOOP
FETCH cur1 INTO a;
IF done THEN
LEAVE read_loop;
END IF;
UPDATE `6de443aacf727f6009e857480f33153c_tasks` SET `live` = 1 WHERE `task_id` = a;
END LOOP;
CLOSE cur1;
UPDATE `6de443aacf727f6009e857480f33153c_tasks` SET `earliest_start` = avaliableTime WHERE `task_id` IN (
SELECT `task_id`
FROM `task_dependency`
WHERE `dependent_on_task_id` = taskID
) AND `earliest_start` < avaliableTime;
END;
I'm trying to create a trigger and its giving me an error that I can't understand the reason of it. In my local server it works perfectly and in the remote doesn't. Both MySQL version are the same, MySQL 5.1.48.
Here is the trigger code:
CREATE TRIGGER actualizar_cliente
BEFORE UPDATE ON cliente FOR EACH ROW BEGIN
IF NEW.password = "" THEN
SET NEW.password = OLD.password;
ELSEIF NEW.password IS NULL THEN
SET NEW.password = (MD5(NEW.password));
END IF;
END
Here is the error message:
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 4
DELIMITER $$
CREATE TRIGGER actualizar_cliente
BEFORE UPDATE ON cliente FOR EACH ROW BEGIN
IF NEW.password = "" THEN
SET NEW.password = OLD.password;
ELSEIF NEW.password IS NULL THEN
SET NEW.password = (MD5(NEW.password));
END IF;
END
$$
DELIMITER ;
and you probably need IS NOT NULL:
ELSEIF NEW.password IS NOT NULL THEN