Error in my stored-procedure - mysql

i have an error in my stored-procedure. I use MySql DB
SET #counter = 1;
SET #last = 0;
UPDATE Customer SET ordre = (IF(#last = customer_id,#counter + 1,#counter = 1)),
#last = customer_id
My Error
Script line: 3 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 '#last = customer_id ORDER BY
customer_id' at line 2

You cannot set variables in SET clause of UPDATE statement. '#last = customer_id' causes the error.
From the reference -
UPDATE syntax - '...SET col_name1=expr1 [, col_name2=expr2 ...]'
The SET clause indicates which columns to modify and the values they should be given.

Related

Trigger with update other table, from aggregation of another table

i'm try to make a trigger on MySql.
Cenario:
i've 3 tables:
com_proposals
com_proposals_items
com_proposals_subitems
IMPORTANT: the three tables has this columns (value_gross,value_disc_auto,value_disc_comercial)
And the source of this values is aways from the last one - com_proposals_subitems
If i updates qty in second table, i've to update the principal table (com_proposals)
And that needs the calculation(sum) of third table(..subitems).
Look:
DELIMITER $$
CREATE TRIGGER trigger_update_com_proposals_cart_items_updatevalues
AFTER UPDATE ON com_proposals_cart_items
FOR EACH ROW
BEGIN
SET #id = OLD.id;
SET #prop_id = OLD.proposal_id;
SELECT
#gross := sum(value_gross),
#auto := sum(value_disc_auto),
#com := sum(value_disc_comercial)
from
com_proposals_cart_subitems
where
cart_item_id = #id;
OLD.value_gross = #gross;
OLD.value_disc_auto = #auto;
OLD.value_disc_comercial = #com;
SELECT
#item_gross := sum(value_gross),
#item_auto := sum(value_disc_auto),
#item_com := sum(value_disc_comercial)
from
com_proposals_cart_items
where
id = #id;
UPDATE com_proposals set
value_gross = #item_gross,
value_disc_auto = #item_autoy,
value_disc_comercial = #item_com
WHERE id = #prop_id;
END$$
DELIMITER ;
But, i'm getting a syntax error:
Error occurred during SQL script execution
Razão:
SQL 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 '.value_gross = #gross;
OLD.value_disc_auto = #auto;
OLD.value_disc_comercial' at line 17
Someone kwons whats happining?? THANKS A LOT!!!
Server version: 5.7.30-0ubuntu0.18.04.1 (Ubuntu)

Syntax error on update SQL statement

I'm getting a syntax error when I want to use md5 over url column. Not sure what's wrong with it:
update table {$GLOBALS['tables']['tableame']} set urlhash = md5(url)
Error message:
Database error 1064 while doing query 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 'table tablename set urlhash = md5(url)' at line 1
Try passing md5 in single quotes as that is a string
update {$GLOBALS['tables']['tableame']} set urlhash = 'MD5_URL_HERE'
following is the syntax
UPDATE table_name SET column_name = `new_value' [WHERE condition];

Error 1064:You have an error in your SQL syntax

I want run this query but get an error:
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 'UPDATE `ads` SET `aDesc` = replace(aDesc, 'amp;', '')' at line 3
My query is:
UPDATE `ads`
SET `aName` = replace(aName, 'amp;', '')
UPDATE `ads`
SET `aDesc` = replace(aDesc, 'amp;', '');
What's the problem?
Your query looks like two queries without a separating delimiter.
The more efficient option is to do both changes in one query:
UPDATE ads
SET aName = replace(aName, 'amp;', ''),
aDesc = replace(aDesc, 'amp;', '');
but if you must run two queries:
UPDATE ads SET aName = replace(aName, 'amp;', '');
UPDATE ads SET aDesc = replace(aDesc, 'amp;', '');

Keep Getting a Syntax Error when Creating MySQL Trigger

I am trying to Create a Trigger that will fire AFTER Insert of a Record where I will see if there is other records similar to this Inserted Record (Same Date) and if so will update a column in the inserted Record. Once I complete this one I will also update it for AFTER Update as well. Any Help would be Greatly Appreciated.
CREATE
TRIGGER `INSERT_POSTDATEINDEX` AFTER INSERT
ON `zoomloca_listings-dev`.`listings_posts`
FOR EACH ROW
BEGIN
DECLARE vNewPostDateIndex INT;
DECLARE vLastPostDateIndex INT DEFAULT '0';
SET vNewPostDateIndex = '0';
SET vLastPostDateIndex = (SELECT POSTDATEINDEX FROM listings_posts WHERE date(POST_DATE) = date(NEW.POST_DATE) ORDER BY POSTDATEINDEX DESC LIMIT 1);
IF vLastPostDateIndex = '0' THEN
SET vNewPostDateIndex = '0';
ELSE
SET vNewPostDateIndex = vLastPostDateIndex + 1;
END IF;
Update `listings_posts` SET POSTDATEINDEX = vNewPostDateIndex where ID = New.ID;
END
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 6
The problem is that there is no TOP in MySQL. You have to use LIMIT instead. Besides, if you are not using mysql client, then you should remove DELIMITER since it is not a feature of the MySQL. Another thing is that to demarcate the end of an IF statement in MySQL, you should use END IF instead of ENDIF.

MySQL triggers on the same table

I have some tables. If I update st_income column, then I want to update total_income column, and when I do this I want update next column (income_per_person). I tried this:
IF (NEW.st_income <> OLD.st_income) THEN
SET NEW.total_income=OLD.total_income + (NEW.st_income-OLD.st_income);
END IF
IF(NEW.total_income <> OLD.total_income) THEN
SET NEW.income_per_person=NEW.total_income/(family_mem_numbers+1)/12;
END IF
but it doesn't work.
EDIT
This is return message:
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 'IF(NEW.total_income <> OLD.total_income) THEN SET NEW.income_per_person=NEW' at line