Error mysql syntax trigger - mysql

According phpmyadmin, I have a syntax error in this trigger :
CREATE TRIGGER insert_device
AFTER INSERT ON table_e
FOR EACH ROW
BEGIN
DECLARE m_id_a INTEGER;
DECLARE m_id_d INTEGER;
m_id_d := 0;
SELECT id_a INTO m_id_a FROM table_ua WHERE ua_eui = NEW.eui LIMIT 1;
SELECT id_d INTO m_id_d FROM table_d WHERE d_idapp = m_id_a ORDER BY id asc LIMIT 1;
IF (m_id_d == 0) THEN
INSERT INTO table_d (d_addr, d_eui, d_apps, d_nwks, d_idapp)
VALUE (NEW.addr, NEW.eui, NEW.apps, NEW.nwks, m_id_a);
ELSE
UPDATE TABLE table_d
SET
d_addr = NEW.addr,
d_eui = NEW.eui,
d_apps = NEW.apps,
d_nwks = NEW.nwks
WHERE id_d = m_id_d;
END IF;
END
The error is :
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 ':= 0;
What is the error ? I don't understand what I am doing wrong..
Thanks.

There are several syntax errors in your trigger:
declare a variable, you should invoke it with set;
IF expression can not use ==, just one equal is ok;
update syntax only need to specify table name like UPDATE table_d SET ....
So please try following trigger:
delimiter $$
CREATE TRIGGER insert_device
AFTER INSERT ON table_e
FOR EACH ROW
BEGIN
DECLARE m_id_a INTEGER;
DECLARE m_id_d INTEGER;
set m_id_d = 0;
SELECT id_a INTO m_id_a FROM table_ua WHERE ua_eui = NEW.eui LIMIT 1;
SELECT id_d INTO m_id_d FROM table_d WHERE d_idapp = m_id_a ORDER BY id asc LIMIT 1;
IF (m_id_d = 0) THEN
INSERT INTO table_d (d_addr, d_eui, d_apps, d_nwks, d_idapp)
VALUE (NEW.addr, NEW.eui, NEW.apps, NEW.nwks, m_id_a);
ELSE
UPDATE table_d
SET
d_addr = NEW.addr,
d_eui = NEW.eui,
d_apps = NEW.apps,
d_nwks = NEW.nwks
WHERE id_d = m_id_d;
END IF;
END
$$

Related

How to compare two tables SUM and Update another Table With MySql Trigger

compare two tables SUM and Update another Table With MySql Trigger
DELIMITER $$
CREATE TRIGGER change_com_status_on_sales_table AFTER INSERT ON `commision_calculation` // COMMISSION TABLE
FOR EACH ROW
BEGIN
DECLARE totalQnt1, totalQnt2 DOUBLE;
DECLARE sales_id INT;
SET #sales_id=(SELECT sales_info_id
FROM commercial_invoice WHERE ci_no = NEW.ci_no);
SET #totalQnt1=(SELECT sum(cc.quantity) as total_qnt1
FROM commision_calculation cc
LEFT JOIN commercial_invoice ci ON cc.ci_id = ci.id
WHERE cc.comission_type = 'Remex' AND ci.sales_info_id= #sales_id
GROUP BY #sales_id);
SET #totalQnt2=(SELECT SUM(sii.quantity) as total_income
FROM sales_info_item sii WHERE sii.sales_info_id = #sales_id
GROUP BY #sales_id);
IF (#totalQnt2 > #totalQnt1) THEN
UPDATE sales_information
SET ci_status = 'Partial'
WHERE id = #sales_id;
END IF;
IF (#totalQnt2 = #totalQnt1) THEN
UPDATE sales_information
SET ci_status = 'Full'
WHERE id = #sales_id;
END IF;
END; $$
DELIMITER ;
The error in above as mentioned by Jahid Hossain
I get "SQLSTATE[21000]: Cardinality violation: 1242 Subquery returns more than 1 row"

ERROR 1064 (42000): check the manual that corresponds to your MySQL server version for the right syntax to use near 'END'

here is mycode
i am trying to create a trigger after insert on a table say product table to a change traker table called audit table
like this
DELIMITER //
CREATE TRIGGER product_table_after_insert
AFTER INSERT
ON product_table FOR EACH ROW
BEGIN
DECLARE l_product_description varchar(500);
DECLARE l_product_number int;
set #l_table_name = 'product_table';
set #l_action = 'INSERT';
set #l_table_column = 'all columns';
set #l_description = 'new row inserted';
select p.product_description ,p.product_number into #l_product_description, #l_product_number from product_table p where p.product_description = (select max(pg.product_number)from product_table pg);
-- Insert record into audit table
INSERT INTO audit_table_test
( table_name,
changed_row_id,
action,
table_column,
change_desciption,
change_time
)
VALUES
( l_table_name,
l_product_number,
l_action,
l_table_column,
l_description,
SYSDATE()
)
END
//
DELIMITER ;
this is the error i am getting
i tried all these ways
used | ,S etc instead of // and
removed ; after end, placed // or \ or $$ together and below end
nothing works, some one please help me
ERROR 1064 (42000): 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' at line 32
some one please help me
Place sql terminator ";" after create table statement
remove ";" after end delimeter.
So your code should be as per below,
DELIMITER //
CREATE TRIGGER product_table_after_insert
AFTER INSERT
ON product_table FOR EACH ROW
BEGIN
DECLARE l_product_description varchar(500);
DECLARE l_product_number int;
set #l_table_name = 'product_table';
set #l_action = 'INSERT';
set #l_table_column = 'all columns';
set #l_description = 'new row inserted';
select p.product_description ,p.product_number into #l_product_description, #l_product_number from product_table p where p.product_description = (select max(pg.product_number)from product_table pg);
-- Insert record into audit table
INSERT INTO audit_table_test
( table_name,
changed_row_id,
action,
table_column,
change_desciption,
change_time
)
VALUES
( l_table_name,
l_product_number,
l_action,
l_table_column,
l_description,
SYSDATE()
);
END
//
DELIMITER
The solution provided by #juergen in comments is working fine, that is, Add a ; after the insert statement (before the END) Thanks for finding my mistake. I was looking for it for about 4 hours so Answer is here:
DELIMITER //
CREATE TRIGGER product_table_after_insert
AFTER INSERT
ON product_table
FOR EACH ROW
BEGIN
DECLARE l_product_description varchar(500);
DECLARE l_product_number int;
set #l_table_name = 'product_table';
set #l_action = 'INSERT';
set #l_table_column = 'all columns';
set #l_description = 'new row inserted';
select p.product_description ,p.product_number
into #l_product_description, #l_product_number
from product_table p
where p.product_description =
(select max(pg.product_number)
from product_table pg);
-- Insert record into audit table
INSERT INTO audit_table_test
( table_name,
changed_row_id,
action,
table_column,
change_desciption,
change_time
)
VALUES
( l_table_name,
l_product_number,
l_action,
l_table_column,
l_description,
SYSDATE()
); //<<---- Semicolon needed to be here
END
//
DELIMITER;

MySQL script error

I'm new to SQL programming and I decided to make a script. This one might be quite riddled with errors but I'm getting an error that I'm unable to resolve.
DELIMITER $
DROP FUNCTION IF EXISTS crossref$
CREATE FUNCTION crossref()
BEGIN
DECLARE i INT;
DECLARE names VARCHAR(70);
SET i = 1;
myloop: LOOP
SET i=i+1;
IF i = 6 then
LEAVE myloop;
END IF;
SET names = (SELECT NAME FROM cbase_excel_table WHERE ID = i);
INSERT INTO cbase_master(NAME, PERMALINK, HOMEPAGE_URL, CATEGORY_LIST, MARKET, FUNDING, 'STATUS', COUNTRY, REGION, CITY)
SELECT NAME, PERMALINK, HOMEPAGE_URL, CATEGORY_LIST, MARKET, FUNDING, 'STATUS', COUNTRY, REGION, CITY FROM cbase_excel_table WHERE ID = i;
UPDATE cbase_master
SET DESCRIPTION = (SELECT DESCRIPTION FROM cbase_json_table WHERE NAME = names)
SET DOMAIN = (SELECT DOMAIN FROM cbase_json_table WHERE NAME = names)
SET IMAGE_URL = (SELECT IMAGE_URL FROM cbase_json_table WHERE NAME = names)
SET FACEBOOK_URL = (SELECT FACEBOOK_URL FROM cbase_json_table WHERE NAME = names)
SET TWITTER_URL = (SELECT TWITTER_URL FROM cbase_json_table WHERE NAME = names)
SET LINKEDIN_URL = (SELECT LINKEDIN_URL FROM cbase_json_table WHERE NAME = names)
SET CBASE_UUID = (SELECT CBASE_UUID FROM cbase_json_table WHERE NAME = names);
END LOOP myloop;
END$
DELIMITER;
and I'm getting:
#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
DECLARE i INT;
DECLARE names VARCHAR(70);
SET i = 1;
Any help?
An example of a function which shows a major difference to your function:
CREATE FUNCTION `fnFindMaximum`(`a` INT, `b` INT)
/* Before the BEGIN statement there are other things going on - the most important being the return type statement */
RETURNS int(11)
LANGUAGE SQL
DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT ''
BEGIN
DECLARE returnval INTEGER;
IF a >= b THEN
SET returnval = a;
ELSE
SET returnval = b;
END IF;
RETURN returnval;
END
Your function then goes on to manipulate sql but does not return a value so, as was pointed out by #arkhil, use a StoredProcedure in preference to a function.

mysql: altered function but stays old function

I changed a column name in a table, and i needed to change a function for it. So i changed the function. But it still runs the old function! it gives me :
"Error Code: 1054 Unknown column 'avDoctorID' in 'where clause'"
and i'm sure i changed the function because if i see the function itself it is the new one! how can this be?
i did this in mysql workbench, and if i see the function in any other program it show the new function, but gives the same error when i try to run it.
This is the query to run it
select FNC_CreateAppointment(8,1,"2012-08-06","15:30:00","something");
And this it the function itself:
DELIMITER $$
CREATE DEFINER=`root`#`%` FUNCTION `fnc_CreateAppointment`(clid int,calid int,appdate date,apptime time,commentaar varchar(3000)) RETURNS tinyint(1)
BEGIN
declare succeeded boolean;
declare id2 int;
declare id int;
declare dagweek int;
Declare afwezig int;
set afwezig = 0;
set dagweek = Dayofweek(appdate);
set id =0;
set id2 = 0;
set succeeded = false;
select availableID into id from tbl_available where AVdays = dagweek and AVHours = apptime and avCalendarID = calid
;
if id > 0
then
select appointmentID into id2 from tbl_appointment where appointmentDate = appdate and appointmentTime = apptime and CalendarID = calid;
if id2 = 0
then
select AbsentID into afwezig from tbl_absent where abHoliday = appdate and abAbsent = apptime and abCalendarID = calid;
if afwezig = 0 then
insert into tbl_appointment(clientId,Calendarid,appointmentDate,appointmenttime,remark)
Values
(clid,calid,appdate,apptime,commentaar);
set succeeded = true;
end if;
end if;
end if;
return succeeded;
END
the error returned is:
call db_demo.FNC_CreateAppointment(8,1,"2012-08-06","15:30:00","something") Error Code: 1054 Unknown column 'avDoctorID' in 'where clause'
'avDoctorID' is the old column
You might need to drop the function first and then run the CREATE statement again.
DROP FUNCTION IF EXISTS `<function_name>`

mysql procedure syntax error

I get an error by sending the following query (MYSQL 5.0):
DELIMITER //
CREATE PROCEDURE relationTable ()
BEGIN
DECLARE articlecount int;
DECLARE keywordcount int;
DECLARE articlehits int;
DECLARE ac int DEFAULT 0;
DECLARE kc int;
DECLARE articleid int;
DECLARE word varchar(100);
DECLARE word_id int;
SET articlehits = 0;
SET articlecount = (SELECT count(id) from articles);
SET keywordcount = (SELECT count(id) from keywords);
outerloop: WHILE (ac < articlecount) DO
SET kc = 0;
SET articleid = (SELECT id from articles LIMIT 1 OFFSET ac);
innerloop: WHILE (kc < keywordcount) DO
IF (articlehits < 5) THEN
SELECT keyword, id INTO word, word_id from keywords LIMIT 1 OFFSET kc;
IF (0 < (SELECT COUNT(id) from articles WHERE id=articleid AND CONCAT(title, " ",text) REGEXP word)) THEN
INSERT INTO articles (id, articleID, keywordID, type) VALUES(NULL, articleid, word_id, 'type1');
SET articlehits = articlehits + 1;
END IF;
SET kc = kc + 1;
ELSE
SET kc = keywordcount;
END IF;
END WHILE innerloop;
SET ac = ac + 1;
END WHILE outerloop;
END;
//
DELIMITER ;
This produces the following error:
ERROR 1064 (42000): 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 'LIMIT 1 OFFSET ac); innerloop: WHILE (kc <
keywordcount) DO TO word, word_id from' at line 15
Any idea why this happens?
(Wrote this to create a relation table between articles and keywords, to enable smart-links in article view.)
Try to remove the while labels:
WHILE (ac < articlecount) DO
SET kc = 0;
SET articleid = (SELECT id from articles LIMIT 1 OFFSET ac);
WHILE (kc < keywordcount) DO
IF (articlehits < 5) THEN
SELECT keyword, id INTO word, word_id from keywords LIMIT 1 OFFSET kc;
IF (0 < (SELECT COUNT(id) from articles WHERE id=articleid AND CONCAT(title, " ",text) REGEXP word)) THEN
INSERT INTO articles (id, articleID, keywordID, type) VALUES(NULL, articleid, word_id, 'type1');
SET articlehits = articlehits + 1;
END IF;
SET kc = kc + 1;
ELSE
SET kc = keywordcount;
END IF;
END WHILE;
SET ac = ac + 1;
END WHILE;
MySQL has connection-specific user-defined variables of the form #varname and declared procedure variables of the type you are using in your code sample. In my experience with procedures, sometimes only one of the types is allowed, and if I recall correctly, one of those situations may be when SELECTing INTO. You might try using user-defined variables here, as follows:
SET articleid = (SELECT id from articles LIMIT 1 OFFSET ac);
innerloop: WHILE (kc < keywordcount) DO
IF (articlehits < 5) THEN
SELECT keyword, id INTO #word, #word_id from keywords LIMIT 1 OFFSET kc;
Just an idea.
One problem may be the SET acticleid = (SELECT...). Try with SELECT .. INTO:
SELECT id INTO #articleid FROM articles LIMIT 1 OFFSET ac;
Variable LIMIT in stored procedures is only supported in new MySQL versions. Note that since you dont have an ORDER BY you will get a random row. It looks like you want to use a CURSOR instead. See docs.
At the risk of seeming over-critical, I believe you should rewrite this procedure to use cursors for traversing your databases, instead of individual selects with LIMIT.
see Cursors in MySQL Docs
Thank for your help so far.
The idea of Tom Haws proved to be correct. The variables for a SELECT INTO statement have to be user-defined.
I edited my code to use cursors, and user-defined variables as followed:
delimiter //
CREATE PROCEDURE relationTable ()
BEGIN
DECLARE articlehits int;
DECLARE looparticles int DEFAULT TRUE;
DECLARE loopwords int DEFAULT TRUE;
DECLARE done INT DEFAULT FALSE;
DECLARE keywordcursor CURSOR FOR SELECT keyword, id FROM keywords;
DECLARE articlecursor CURSOR FOR SELECT id FROM articles;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN articlecursor;
WHILE (looparticles) DO
FETCH articlecursor INTO #articleid;
IF done THEN SET looparticles = FALSE;
ELSE
SET articlehits = 0;
OPEN keywordcursor;
WHILE (loopwords) DO
FETCH keywordcursor INTO #word, #wordid;
IF (articlehits < 5) AND NOT done THEN
IF (0 < (SELECT COUNT(id) FROM articles WHERE id=#articleid AND CONCAT(title, " ", text) REGEXP #word)) THEN
INSERT INTO keyword_article_rel (id, meldungID, wordID) VALUES(NULL, #articleid, #wordid);
SET articlehits = articlehits + 1;
END IF;
ELSE
SET loopwords = FALSE;
CLOSE keywordcursor;
SET done = FALSE;
END IF;
END WHILE;
END IF;
END WHILE;
CLOSE articlecursor;
END;
//
delimiter ;
And now I get an other error that I really can't explain:
ERROR 1064 (42000): 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 '; DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN articlecursor; W' at line 6
This error confuses me because it can't have a problem with the handler. The handler is declared as in the example of the mysql-documentation. Could the problem be that I can't create two cursors like this?