MySql Trigger won't compute - mysql

I need to create a trigger that computes one column value based on other column values. A database is a little bit denormalized to get higher performance. (Normalization is not an issue of this question).
The problem is that I want to set value of computed_address value and it is ok if i put a constant in it. But it seems that these If clauses aren't working and I just can't see the problem.
Below is the trigger code.
Thank you very much!
DELIMITER $$
USE `nth_poi_new_3`$$
DROP TRIGGER /*!50032 IF EXISTS */ `poi_address_creator`$$
CREATE
/*!50017 DEFINER = 'root'#'localhost' */
TRIGGER `poi_address_creator` BEFORE INSERT ON `poi`
FOR EACH ROW BEGIN
DECLARE full_address VARCHAR(255);
DECLARE country_string VARCHAR(100);
DECLARE region_string VARCHAR(100);
DECLARE town_string VARCHAR(100);
DECLARE address_string VARCHAR(100);
IF NEW.address <> '' THEN
SET full_address = CONCAT(NEW.address, ",");
END IF;
IF NEW.town_name IS NOT NULL THEN
SET full_address = CONCAT(full_address, NEW.town_name, ",");
ELSEIF NEW.town_id IS NOT NULL THEN
SELECT NAME INTO town_string FROM town WHERE town.town_id = NEW.town_id LIMIT 1;
SET full_address = CONCAT(full_address, town_string, ",");
END IF;
IF NEW.region_name IS NOT NULL THEN
SET full_address = CONCAT(full_address, NEW.region_name, ",");
ELSEIF NEW.region_id IS NOT NULL THEN
SELECT NAME INTO region_string FROM region WHERE region.region_id = NEW.region_id LIMIT 1;
SET full_address = CONCAT(full_address, region_string, ",");
END IF;
IF NEW.country_name IS NOT NULL THEN
SET full_address = CONCAT(full_address, NEW.country_name, ",");
ELSEIF NEW.country_id IS NOT NULL THEN
SELECT NAME INTO country_string FROM country WHERE country.country_id = NEW.country_id LIMIT 1;
SET full_address = CONCAT(full_address, country_string, ",");
END IF;
SET NEW.computed_address = full_address;
END;
$$
DELIMITER ;

at the bottom of your trigger code
END;
$$
DELIMITER ;
should be
END$$
DELIMITER ;

Hey,
just to answer. I had to initialize full_address variable first. I just made this:
DECLARE full_address VARCHAR(255);
But after declaration it needed to be initialization also:
SET full_address="";

Related

MySQL Query to delete entries containing words longer then 10 characters

I want to search in Wp post(title) column and delete entries which contains words longer then 10 characters but i am unable to find a query to do that. can anyone help?
Edit: I made following SP.
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `p`(IN `instring` VARCHAR(255))
begin
declare tempstring varchar(10000);
declare idin int;
declare outstring varchar(100);
declare c1 varchar(100);
declare c2 varchar(100);
declare totallength int DEFAULT 0;
declare checkit int;
declare done int;
DECLARE CUR1 CURSOR for SELECT id,post_title FROM wp_posts;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
drop table if exists occursresults;
create table occursresults (col1 varchar(20), col2 varchar(20),totallength int );
open CUR1;
read_loop: LOOP
FETCH CUR1 INTO idin,tempstring;
if done then leave read_loop; end if;
set checkit = 0;
looper: while tempstring is not null and instr(tempstring,' ') > 0 do
set checkit = checkit + 1;
if checkit > 100 then #In case of infinite loop
leave looper;
end if;
set outstring = substr(tempstring,1,instr(tempstring, ' ') - 1);
set tempstring = ltrim(rtrim(replace(tempstring,concat(outstring,' '),'')));
set c1 = substr(outstring,1,instr(outstring, ' ') - 1);
set c2 = replace(outstring,concat(c1,' '),'');
if Length(c2) > 10 then
delete from wp_posts where id=idin;
end if;
if Length(c2) < 10 then
INSERT INTO occursresults (COL1,COL2,totallength) VALUES (c1,c2,Length(c2));
end if;
end while;
select tempstring;
set outstring = tempstring;
set c1 = substr(outstring,1,instr(outstring, ' ') - 1);
set c2 = replace(outstring,concat(c1,' '),'');
if Length(c2) > 10 then
delete from wp_posts where id=idin;
end if;
if Length(c2) < 10 then
INSERT INTO occursresults (COL1,COL2,totallength) VALUES (c1,c2,Length(c2));
end if;
end loop;
close cur1;
end$$
DELIMITER ;
but when i call it. i get this error.
Error
Static analysis:
1 errors were found during analysis.
Missing expression. (near "ON" at position 25)
SQL query: Edit Edit
SET FOREIGN_KEY_CHECKS = ON;
MySQL said: Documentation
2014 - Commands out of sync; you can't run this command now
You can use a regex to identify the rows to delete:
DELETE FROM post
WHERE title RLIKE '[a-zA-Z]{10}'
This defines a word as a sequence of alphabetic characters. You might want to adapt the regex to add more characters if needed (digits, underscore, dash, ...).
you can use CHAR_LENGTH as a function to get the length.
Select:
SELECT
*
FROM
post
WHERE
CHAR_LENGTH(title) > 10;
Delete query:
DELETE FROM post
WHERE
CHAR_LENGTH(title) > 10;
Note: LENGTH function too works but it will calculate in bytes rather than chars more info on this refer this MySQL - length() vs char_length()

MySQL Function getting Syntax Error

I am trying to create the below function in MySQL but getting syntax error.
I am not able to find the solution, would be grateful for some help
CREATE FUNCTION `test`.`pro`(depart_id int) RETURNS varchar
BEGIN
DECLARE title varchar;
if depart_id = 1 then
set title='IT Department';
else if depart_id = 2 then
set title='HR Department';
else
set title='Admin';
end if;
return title;
END$$
DELIMITER ;
You have several syntax errors in your script:
varchar must have a length
You should define DELIMITER $$ first
It's not else if, but elseif
Try this;)
DELIMITER $$
CREATE FUNCTION `test`.`pro`(depart_id int) RETURNS varchar(10)
BEGIN
DECLARE title varchar(10);
if depart_id = 1 then
set title='IT Department';
elseif depart_id = 2 then
set title='HR Department';
else
set title='Admin';
end if;
return title;
END $$
DELIMITER ;

Mysql - How to split text variable in stored procedure

I have a MySQL variable as below.
DECLARE str TEXT DEFAULT '2014-01-02 13:00:00|2014-02-04 12:59:59#0#2014-02-04 13:00:00|2014-03-04 12:59:59#0#2014-03-04 13:00:00|2014-04-02 13:59:59#0#2014-04-02 14:00:00|2014-05-02 14:59:59#0#2014-05-02 15:00:00|2014-06-03 14:59:59';
I want to break this whole string first by using the separator #0# and from the results break the string using separator |.
I have tried MySQL split_str function but I am not able to do it.
Its giving me the error split_str does not exist.
Please suggest some other way to do this.
Finally i have resolved my problem using below procedure.
I am able to solve it using temporary table and procedure. I am no more using mysql variable for this. We can call the procedure as below....
CALL SplitString('2014-01-02 13:00:00|2014-02-04 12:59:59#0#2014-02-04 13:00:00|2014-03-04 12:59:59#0#2014-03-04 13:00:00|2014-04-02 13:59:59#0#2014-04-02 14:00:00|2014-05-02 14:59:59#0#2014-05-02 15:00:00|2014-06-03 14:59:59', '#0#', 'tblindex');
Here tblindex is temporary table i have used.
My procedure is below....
DELIMITER $$
DROP PROCEDURE IF EXISTS `SplitString`$$
CREATE PROCEDURE `SplitString`( IN input TEXT,IN delm VARCHAR(10),tblnm varchar(50))
BEGIN
DECLARE cur_position INT DEFAULT 1 ;
DECLARE remainder TEXT;
DECLARE cur_string VARCHAR(1000);
DECLARE delm_length TINYINT UNSIGNED;
set #sql_drop = concat("DROP TABLE IF EXISTS ",tblnm);
prepare st_drop from #sql_drop;
execute st_drop;
set #sql_create = concat("CREATE TEMPORARY TABLE ",tblnm," (value VARCHAR(2000) NOT NULL ) ENGINE=MEMORY;");
prepare st_create from #sql_create;
execute st_create;
SET remainder = input;
SET delm_length = CHAR_LENGTH(delm);
WHILE CHAR_LENGTH(remainder) > 0 AND cur_position > 0
DO
SET cur_position = INSTR(remainder, delm);
IF cur_position = 0 THEN
SET cur_string = remainder;
ELSE
SET cur_string = LEFT(remainder, cur_position - 1);
END IF;
-- select cur_string;
IF TRIM(cur_string) != '' THEN
set #sql_insert = concat("INSERT INTO ",tblnm," VALUES ('",cur_string,"');");
prepare st_insert from #sql_insert;
execute st_insert;
END IF;
SET remainder = SUBSTRING(remainder, cur_position + delm_length);
END WHILE;
END$$
DELIMITER ;

error in trigger creation also how to optimise it

Please tell me whats wrong with the code. Not able to fine the bug
DELIMITER $$
CREATE TRIGGER update_status BEFORE Update ON listing_basic_new_updated
FOR EACH ROW
if new.processing_status is not null
then begin
SET new.rep_status = New.processing_status;
end;
elseif new.televeri_status is not null
then begin
SET new.rep_status = New.televeri_status;
end;
elseif new.verification_status is not null
then begin
SET new.rep_status = New.verification_status;
end;
end if;
END$$
DELIMITER ;
I think you're missing a BEGIN that would match up to your END at the end:
DELIMITER $$
CREATE TRIGGER update_status BEFORE Update ON listing_basic_new_updated
FOR EACH ROW
BEGIN
if new.processing_status is not null
then begin
SET new.rep_status = New.processing_status;
end;
elseif new.televeri_status is not null
then begin
SET new.rep_status = New.televeri_status;
end;
elseif new.verification_status is not null
then begin
SET new.rep_status = New.verification_status;
end;
end if;
END$$
DELIMITER ;
I think you might be able to replace all of it with
SET new.rep_status = COALESCE(new.processing_status, new.televeri_status,
new.verification_status, new.rep_status);
COALESCE: "Returns the first non-NULL value in the list, or NULL if there are no non-NULL values."

mysql update trigger old, new row's column log

my update trigger
BEGIN
DECLARE s VARCHAR(100);
IF (OLD.authUId <> NEW.authUId) THEN SET s = CONCAT(s,'&authUId=', OLD.authUId); END IF;
IF (OLD.autPId <> NEW.autPId) THEN SET s = CONCAT(s,'&autPId=', OLD.autPId); END IF;
.....
INSERT INTO l_dblog(src,newId,oldValue) VALUE('auth_up',new.authId,s);
END
inserted row oldValue column always null insert, whats my problem ?
I have delete trigger is succes work
my Delete trigger:
BEGIN
DECLARE s VARCHAR(60);
SET s = CONCAT('&authUId=', OLD.authUId,'&autPId=', OLD.autPId,'&authTypeId=', OLD.authTypeId,'&authValue=', OLD.authValue);
INSERT INTO l_dblog(src,newId,oldValue)
VALUE('auth_del',OLD.authId,s);
END
Thank you...
Set default value of variable s as blank in your update query and then check weather trigger is working or not. If not than what error you got after running the trigger.
BEGIN
DECLARE s VARCHAR(100) DEFAULT '';
IF (OLD.authUId <> NEW.authUId) THEN SET s = CONCAT(s,'&authUId=', OLD.authUId); END IF;
IF (OLD.autPId <> NEW.autPId) THEN SET s = CONCAT(s,'&autPId=', OLD.autPId); END IF;
.....
INSERT INTO l_dblog(src,newId,oldValue) VALUE('auth_up',new.authId,s);
END