It occurs #1054 Error when I call procedure in mysql - php

CREATE PROCEDURE test_proc (in name varchar(100),out return_msg varchar(3000))
BEGIN
BEGIN
DECLARE v_return_msg VARCHAR(3000); -- return message
DECLARE v_error_flag INT DEFAULT 0;
DECLARE EXIT HANDLER FOR SQLEXCEPTION SET v_error_flag = -1;
START TRANSACTION;
INSERT INTO `tablename` (name, value) VALUES (name, 'Test2');
END;
IF v_error_flag < 0 THEN
ROLLBACK;
CALL DBMS_OUTPUT.PUT_LINE('Error : ' || mysql_error );
SELECT * FROM INSERT_INFO; -- UPDATE_TABLE_USED
SHOW ERRORS;
ELSE
COMMIT;
CALL DBMS_OUTPUT.PUT_LINE('Sucess : ' || sqlerrm );
SELECT 'Process succeed!!!';
END IF;
END
This is my mysql query(procedure).
However, When I CALL my procedure, It occurs error.
MySQL Message: #1054 - Unknown column 'v_error_flag' in 'field list'
I think IF statement occur error.
IF v_error_flag < 0 THEN
However, I don't know how can I fix it.

The v_error_flag is declared with in the BEGIN ... END.
The IF v_error_flag < 0 THEN condition is out of the block so v_error_flag is not recognized and throwing the error.
You need to move the IF v_error_flag < 0 THEN condition inside the BEGIN ... END block.
or
Simply remove or comment the Second BEGIN ... END block as:
CREATE PROCEDURE test_proc (in name varchar(100),out return_msg varchar(3000))
BEGIN
-- BEGIN <-- remove/comment this
DECLARE v_return_msg VARCHAR(3000); -- return message
DECLARE v_error_flag INT DEFAULT 0;
DECLARE EXIT HANDLER FOR SQLEXCEPTION SET v_error_flag = -1;
START TRANSACTION;
INSERT INTO `tablename` (name, value) VALUES (name, 'Test2');
-- END; <-- remove/comment this
IF v_error_flag < 0 THEN
ROLLBACK;
CALL DBMS_OUTPUT.PUT_LINE('Error : ' || mysql_error );
SELECT * FROM INSERT_INFO; -- UPDATE_TABLE_USED
SHOW ERRORS;
ELSE
COMMIT;
CALL DBMS_OUTPUT.PUT_LINE('Sucess : ' || sqlerrm );
SELECT 'Process succeed!!!';
END IF;
END

Related

#1054 Unknown Column in Field List in mysql Procedure

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 )";

Syntax error in MySQL function while running code

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 ;

Error in trigger

DELIMITER $$
USE `airdb`$$
DROP TRIGGER /*!50032 IF EXISTS */ `updateprodaja`$$
CREATE
/*!50017 DEFINER = 'root'#'localhost' */
TRIGGER `updateprodaja` BEFORE UPDATE ON `prodaja`
FOR EACH ROW BEGIN
IF ( SELECT podmireno FROM prodaja WHERE id = NEW.id ) > 0 THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Error: you got an error'
END IF;
END;
$$
DELIMITER ;
Got given 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 'END IF;
END' at line 7
try with this you forget to terminate sql query before end if
DELIMITER $$
USE `airdb`$$
DROP TRIGGER /*!50032 IF EXISTS */ `updateprodaja`$$
CREATE
/*!50017 DEFINER = 'root'#'localhost' */
TRIGGER `updateprodaja` BEFORE UPDATE ON `prodaja`
FOR EACH ROW BEGIN
IF ( SELECT podmireno FROM prodaja WHERE id = NEW.id ) > 0 THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Error: you got an error';
END IF;
END;
$$
DELIMITER ;

Can't create procedure with mysqli, but can with phpmyadmin?

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;

Trigger Mysql Error Near the "

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

Categories