Error in trigger - php

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 ;

Related

It occurs #1054 Error when I call procedure in mysql

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

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 ;

MySQL: Syntax error when creating trigger

Why my sql syntax is throwing an error?
CREATE TRIGGER info
AFTER INSERT ON inbox
FOR EACH ROW BEGIN if SUBSTRING(new.TextDecoded,1,6)='telkom' then
INSERT INTO outbox ( DestinationNumber, Coding, TextDecoded, CreatorID )VALUES ( new.SenderNumber, 'Default_No_Compression', (SELECT nama from data_dosen WHERE kode = SUBSTRING(new.TextDecoded,8,10)), '1');
else
INSERT INTO outbox (DestinationNumber, Coding, TextDecoded, CreatorID) VALUES (new.SenderNumber, 'Default_No_Compression', 'Maaf format sms Anda salah. Ketik telkom<spasi>kode dosen', '1');
end if;
END$$
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
You don't have delimiter definition before your trigger. Delimiter informs MySQL which characters will end up your trigger. Without it, MySQL assumes semicolon as default delimiter and fails upon first semicolon in your code. Check out this:
DELIMITER $$
CREATE TRIGGER info
AFTER INSERT ON inbox
FOR EACH ROW BEGIN if SUBSTRING(new.TextDecoded,1,6)='telkom' then
INSERT INTO outbox ( DestinationNumber, Coding, TextDecoded, CreatorID )VALUES ( new.SenderNumber, 'Default_No_Compression', (SELECT nama from data_dosen WHERE kode = SUBSTRING(new.TextDecoded,8,10)), '1');
else
INSERT INTO outbox (DestinationNumber, Coding, TextDecoded, CreatorID) VALUES (new.SenderNumber, 'Default_No_Compression', 'Maaf format sms Anda salah. Ketik telkom<spasi>kode dosen', '1');
end if;
END$$
DELIMITER ;

MySQL Verify all colums and add if not exist

I try to check if all the column exist in a MySql ddb. I use this answer MySQL add column if not exist but i have a 1064 error. Anyboby can helps me ?
Thanks
foreach($source_bdd as $each_column)
{
$source_column = explode('***',$each_column);
$nullable = $source_column[4] == 'YES' ? 'DEFAULT NULL' : 'NOT NULL';
$verify_and_update_column .= '
DELIMITER $$
DROP PROCEDURE IF EXISTS Alter_Table_'.$source_column[0].''.$source_column[1].' $$
CREATE PROCEDURE Alter_Table_'.$source_column[0].''.$source_column[1].'()
BEGIN
DECLARE '.$source_column[0].''.$source_column[1].'_count INT;
SET '.$source_column[0].''.$source_column[1].'_count = ( SELECT COUNT(*)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = "'.$source_column[0].'" AND
COLUMN_NAME = "'.$source_column[1].'");
IF '.$source_column[0].''.$source_column[1].'_count = 0 THEN
ALTER TABLE '.$source_column[0].'
ADD COLUMN '.$source_column[1].' '.$source_column[9].' '.$nullable.';
END IF;
END $$
CALL Alter_Table_'.$source_column[0].''.$source_column[1].'() $$
DELIMITER ;
';
}
Here is an extract of the query and the error at the end :
DELIMITER $$
DROP PROCEDURE IF EXISTS Alter_Table_table_1_column_1 $$
CREATE PROCEDURE Alter_Table_table_1_column_1()
BEGIN
DECLARE table_1_column_1_count INT;
SET table_1_column_1_count = ( SELECT COUNT(*)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = "table_1" AND
COLUMN_NAME = "table_1");
IF table_1_column_1_count = 0 THEN
ALTER TABLE table_1
ADD COLUMN table_1 varchar(512) DEFAULT NULL;
END IF;
END $$
CALL Alter_Table_table_1_column_1() $$
DELIMITER ;
DELIMITER $$
DROP PROCEDURE IF EXISTS Alter_Table_table_1_column_2 $$
CREATE PROCEDURE Alter_Table_table_1_column_2()
BEGIN
DECLARE table_1_column_2_count INT;
SET table_1_column_2_count = ( SELECT COUNT(*)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = "table_1" AND
COLUMN_NAME = "table_1");
IF table_1_column_2_count = 0 THEN
ALTER TABLE table_1
ADD COLUMN table_1 varchar(512) DEFAULT NULL;
END IF;
END $$
CALL Alter_Table_table_1_column_2() $$
DELIMITER ;
….
DELIMITER $$
DROP PROCEDURE IF EXISTS Alter_Table_table_25_column_12 $$
CREATE PROCEDURE Alter_Table_table_25_column_12()
BEGIN
DECLARE table_25_column_12_count INT;
SET table_25_column_12_count = ( SELECT COUNT(*)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = "table_1" AND
COLUMN_NAME = "table_1");
IF table_25_column_12_count = 0 THEN
ALTER TABLE table_1
ADD COLUMN table_1 varchar(512) DEFAULT NULL;
END IF;
END $$
CALL Alter_Table_table_25_column_12() $$
DELIMITER ;
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 $$
DROP PROCEDURE IF EXISTS Alter_Table_table_1_column_1 $$
CREATE PROC' at line 11064

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