I am trying to create a MySQL trigger but don't know why error #1064 occurs. Here is the full error 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 'DECLARE popularity_cur CURSOR FOR select (sum(tbl_reviews.vote_value)/count(tbl_' at line 22
Line #22 is close cur;
and here is what I've done so far:
delimiter //
CREATE TRIGGER business_popularity_trigger_after_review_insert
AFTER INSERT ON tbl_reviews FOR EACH ROW
BEGIN
DECLARE data_finished INTEGER DEFAULT 0;
DECLARE businessId, reviewId, dealId, businessServiceId INT DEFAULT 0;
DECLARE businessPopularity INTEGER DEFAULT 0;
DECLARE popularity_finished INTEGER DEFAULT 0;
DECLARE cur CURSOR FOR select tbl_reviews.review_id, tbl_reviews.deal_id, deals.deal_id, deals.business_service_id, business_services.business_service_id, business_services.business_id from tbl_reviews INNER JOIN deals ON deals.deal_id = new.deal_id INNER JOIN business_services ON business_services.business_service_id = deals.business_service_id
where tbl_reviews.review_id = new.review_id
group by business_services.business_id;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET data_finished = 1;
OPEN cur;
get_data: LOOP
fetch cur into reviewId, dealId, businessServiceId, businessId;
IF data_finished = 1 THEN
LEAVE get_data;
END IF;
END LOOP get_data;
close cur;
DECLARE popularity_cur CURSOR FOR select (sum(tbl_reviews.vote_value)/count(tbl_reviews.vote_value)) from tbl_reviews INNER JOIN business_services ON business_services.business_id = businessId INNER JOIN deals ON deals.business_service_id = business_services.business_service_id where deals.deal_is_deleted = 'N' and tbl_reviews.review_is_deleted = 'N' and tbl_reviews.review_status = 'Y';
set data_finished = 0;
IF businessId > 0 THEN
OPEN popularity_cur;
get_popularity: LOOP
fetch popularity_cur into businessPopularity;
IF data_finished = 1 THEN
LEAVE get_popularity;
END IF;
END LOOP get_popularity;
close popularity_cur;
Update business
set business.business_popularity = businessPopularity
where business.business_id = businessId;
END IF;
delimiter;
Please help me.
Thanks
You should use correct delimiter // at the end and close the BEGIN ... END construction. So change last lines to:
END IF;
END//
DELIMITER;
UPDATE Since you use aggregating functions SUM, COUNT you need GROUP BY statement. So try this way:
DECLARE popularity_cur CURSOR FOR
SELECT (sum(tbl_reviews.vote_value)/count(tbl_reviews.vote_value))
FROM tbl_reviews
INNER JOIN business_services
ON business_services.business_id = tbl_reviews.businessId
INNER JOIN deals
ON deals.business_service_id = business_services.business_service_id
AND deals.deal_is_deleted = 'N'
WHERE tbl_reviews.review_is_deleted = 'N'
AND tbl_reviews.review_status = 'Y'
GROUP BY tbl_reviews.businessId;
I did some optimization and you had wrong condition business_services.business_id = businessId so I did change to business_services.business_id = tbl_reviews.businessId.
NOTE Probably that wasn't a mistake and you need that business_services.business_id = businessId condition. But even if you need that you should use one condition for JOIN and another one to filter. so it could be like:
INNER JOIN business_services
ON business_services.business_id = tbl_reviews.business_id
AND business_services.business_id = businessId
UPDATE 2 According to the screenshot with your database schema your query could be like:
DECLARE popularity_cur CURSOR FOR
SELECT (sum(tbl_reviews.vote_value)/count(tbl_reviews.vote_value))
FROM tbl_reviews
INNER JOIN deals
ON tbl_reviews.deal_id = deals.deal_id
AND deals.deal_is_deleted = 'N'
INNER JOIN business_services
ON business_services.business_service_id = deals.business_service_id
WHERE tbl_reviews.review_is_deleted = 'N'
AND tbl_reviews.review_status = 'Y'
GROUP BY deals.business_service_id;
Related
I keep getting error messages when I try to execute this statement in phpmyadmin. I'm running mysql 5.7
DELIMITER //
CREATE OR REPLACE TRIGGER update_counts_trigger AFTER INSERT OR UPDATE OR DELETE ON tickets
FOR EACH ROW
BEGIN
DECLARE V_uitvoeringId, V_ReserveringId varchar(50);
DECLARE V_tekoop, V_gereserveerd, V_wachtlijst int;
SET V_ReserveringId = NEW.reserveringId OR OLD.reserveringId;
SET V_uitvoeringId = ( SELECT uitvoeringId FROM reservering WHERE id=V_ReserveringId )
SET V_tekoop = (
SELECT count(*)
FROM tickets t
WHERE t.tekoop AND NOT t.geannuleerd AND NOT t.verkocht AND t.uitvoeringId = V_uitvoeringId);
SET V_gereserveerd = (
SELECT count(*)
FROM tickets t
WHERE NOT t.wachtlijst AND NOT t.geannuleerd AND NOT t.verkocht AND t.uitvoeringId = V_uitvoeringId);
SET V_wachtlijst = (
SELECT count(*) FROM tickets t WHERE t.wachtlijst AND NOT t.geannuleerd AND NOT t.verkocht AND t.uitvoeringId = V_uitvoeringId);
update uitvoering
set tekoop = V_tekoop,
gereserveerd = V_gereserveerd,
wachtlijst = V_wachtlijst,
vrije_plaatsen = aantal_plaatsen - V_gereserveerd + V_tekoop
WHERE id=V_uitvoeringId;
END //
DELIMITER ;
Can anyone see what's wrong?
Unrecognized statement type. (near "DECLARE" at position 176)
This type of clause was previously parsed. (near "SET" at position 398)
Unrecognized statement type. (near "END" at position 1192)
There are other syntax errors causing those complaints.
There is no OR REPLACE option with triggers in MySQL. The trigger must be dropped and then recreated.
There is no ORing with actions, only one, which means that you'll have to create 3 triggers even though the definition may be the same. However, it seems like you only need an AFTER UPDATE action? ... because AFTER INSERT does not have an OLD reference, and AFTER DELETE does not have a NEW reference.
Getting past those two items, there is also a ; missing after the query with SELECT uitvoeringId.
Putting all that together, becomes:
DELIMITER //
CREATE TRIGGER update_counts_trigger AFTER UPDATE ON tickets
FOR EACH ROW
BEGIN
DECLARE V_uitvoeringId, V_ReserveringId varchar(50);
DECLARE V_tekoop, V_gereserveerd, V_wachtlijst int;
SET V_ReserveringId = NEW.reserveringId OR OLD.reserveringId;
SET V_uitvoeringId = ( SELECT uitvoeringId FROM reservering WHERE id=V_ReserveringId );
SET V_tekoop = (
SELECT count(*)
FROM tickets t
WHERE t.tekoop AND NOT t.geannuleerd AND NOT t.verkocht AND t.uitvoeringId = V_uitvoeringId);
SET V_gereserveerd = (
SELECT count(*)
FROM tickets t
WHERE NOT t.wachtlijst AND NOT t.geannuleerd AND NOT t.verkocht AND t.uitvoeringId = V_uitvoeringId);
SET V_wachtlijst = (
SELECT count(*) FROM tickets t WHERE t.wachtlijst AND NOT t.geannuleerd AND NOT t.verkocht AND t.uitvoeringId = V_uitvoeringId);
update uitvoering
set tekoop = V_tekoop,
gereserveerd = V_gereserveerd,
wachtlijst = V_wachtlijst,
vrije_plaatsen = aantal_plaatsen - V_gereserveerd + V_tekoop
WHERE id=V_uitvoeringId;
END //
DELIMITER ;
Then with that in phpMyAdmin:
Then after executing the SQL:
Then checking the triggers tab:
...and clicking Edit at the trigger shows the definition:
While trying to update a trigger, MySQL tells me the query ";" failed. How is ";" even a query in MySQL's view is beyond me.
The exact message is:
The following query has failed: ";" MySQL said: #1065 - Query was empty
Here's the new trigger (AFTER INSERT):
BEGIN
DECLARE vIdPlacet VARCHAR(40);
DECLARE vTypeTravaux VARCHAR(32);
DECLARE vEssence VARCHAR(3) DEFAULT '-';
DECLARE vClasseHau VARCHAR(5) DEFAULT '-';
DECLARE vNoMesurag int;
DECLARE new_id_parcelle INT UNSIGNED DEFAULT 0;
DECLARE new_no_microplacette INT UNSIGNED DEFAULT 0;
IF NEW.deleted = 0 THEN
SELECT id_parcelle, no_microplacette
INTO new_id_parcelle, new_no_microplacette
FROM microplacette
WHERE id_microplacette = NEW.id_microplacette;
SELECT travaux, no_mesurag, id__placet
INTO vTypeTravaux, vNoMesurag, vIdPlacet
FROM secteur
LEFT JOIN parcelle ON secteur.id_secteur = parcelle.id_secteur
WHERE id_parcelle = new_id_parcelle;
IF vTypeTravaux = 'inventaire' THEN
SELECT abbreviation INTO vEssence FROM essences WHERE _id = NEW.id_essence;
IF NEW.hauteur_15 = 1 THEN
SET vClasseHau = '15CM+';
END IF;
IF (SELECT COUNT(*) FROM imported_pres_ess WHERE id__placet = vIdPlacet AND
caracteris = '-' AND
classe_hau = vClasseHau AND
essence = vEssence AND
no_mesurag = vNoMesurag AND
no_micro_p = new_no_microplacette) = 0 THEN
INSERT INTO imported_pres_ess (id__placet, caracteris, classe_hau, essence, no_mesurag, no_micro_p)
VALUES (vIdPlacet, '-', vClasseHau, vEssence, vNoMesurag, new_no_microplacette);
END IF;
IF (SELECT COUNT(*) FROM imported_semi_gau WHERE id__placet = vIdPlacet AND
classe_hau = vClasseHau AND
essence = vEssence AND
no_mesurag = vNoMesurag AND
no_micro_p = new_no_microplacette) = 0 THEN
INSERT INTO imported_semi_gau (id__placet, classe_hau, essence, no_mesurag, no_micro_p)
VALUES (vIdPlacet, vClasseHau, vEssence, vNoMesurag, new_no_microplacette);
END IF;
IF NEW.diametre > 0 THEN
SET vClasseHau = 'D2_D8';
ELSE
SET vClasseHau = '-';
END IF;
IF (SELECT COUNT(*) FROM imported_pres_ess WHERE id__placet = vIdPlacet AND
caracteris = '-' AND
classe_hau = vClasseHau AND
essence = vEssence AND
no_mesurag = vNoMesurag AND
no_micro_p = new_no_microplacette) = 0 THEN
INSERT INTO imported_pres_ess (id__placet, caracteris, classe_hau, essence, no_mesurag, no_micro_p)
VALUES (vIdPlacet, '-', vClasseHau, vEssence, vNoMesurag, new_no_microplacette);
END IF;
IF (SELECT COUNT(*) FROM imported_semi_gau WHERE id__placet = vIdPlacet AND
classe_hau = vClasseHau AND
essence = vEssence AND
no_mesurag = vNoMesurag AND
no_micro_p = new_no_microplacette) = 0 THEN
INSERT INTO imported_semi_gau (id__placet, classe_hau, essence, no_mesurag, no_micro_p)
VALUES (vIdPlacet, vClasseHau, vEssence, vNoMesurag, new_no_microplacette);
END IF;
END IF;
END IF;
END
I tried creating the procedure you show, but I don't get any error.
The error about "empty statement" happens when you try to execute a query through the API but the query string is empty.
I can duplicate the error in the mysql client this way:
mysql> set #s = '';
mysql> prepare stmt from #s;
ERROR 1065 (42000): Query was empty
So I suggest you look not at the stored procedure, but whatever code you're executing this from, and check that every time you try to execute a query, that you submit a non-empty string.
It turns out, the trigger I was updating got deleted in the meantime, so I was updating a trigger that didn't exist anymore.
I found out after refreshing the page (the trigger was gone from the trigger list).
I simply recreated the trigger anew and it worked.
When trying to run the following stored procedure from django, I get an OperationError (1172, 'Result consisted of more than one row') Any idea what I might be doing wrong?
-- --------------------------------------------------------------------------------
-- Routine DDL
-- Note: comments before and after the routine body will not be stored by the server
-- --------------------------------------------------------------------------------
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `UpdatePrices`(IN storeId int, IN bottleSize VARCHAR(50))
BEGIN
DECLARE amount DECIMAL(10,2); DECLARE isCustom INT DEFAULT 0;
DECLARE changeType VARCHAR(50) DEFAULT 'State'; DECLARE updateType INT DEFAULT 0;
IF bottleSize = '1000 Ml' THEN
SELECT S1000IncreaseChoices INTO changeType FROM store_store WHERE StoreID = storeId;
IF changeType = 'State' THEN
SELECT updateType = 0;
END IF;
IF changeType = 'Flat' THEN
SELECT S1000IncreaseAmount INTO amount FROM store_store WHERE StoreID = storeId;
SELECT updateType = 1;
END IF;
IF changeType = 'Percent' THEN
SELECT 1 - S1000IncreaseAmount/100 INTO amount FROM store_store WHERE StoreID = storeId;
SELECT updateType = 2;
END IF;
END IF;
IF updateType = 0 THEN
update store_storeliquor SL
inner join liquor_liquor LL
on liquorID_id = id
set StorePrice = ShelfPrice
where BottleSize = bottleSize
and storeID_id = storeId
and custom = 0;
END IF;
IF updateType = 1 THEN
update store_storeliquor SL
inner join liquor_liquor LL
on liquorID_id = id
set StorePrice = OffPremisePrice + amount
where BottleSize = bottleSize
and storeID_id = storeId
and custom = 0;
END IF;
IF updateType = 1 THEN
update store_storeliquor SL
inner join liquor_liquor LL
on liquorID_id = id
set StorePrice = OffPremisePrice / amount
where BottleSize = bottleSize
and storeID_id = storeId
and custom = 0;
END IF;
END
I'm not sure if it matters, but I initiate the stored procedure like so:
def priceupdate(request, store_id):
cursor = connection.cursor()
cursor.callproc("UpdatePrices", (store_id, '1000 ML'))
cursor.close()
return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
Your SELECT...INTO queries give result sets with more then one record. The WHERE filters are incorrect - they compare two the same values StoreID = storeId. Rename IN storeId int parementer to another name. For example - IN storeId_param int
The query will be like this -
SELECT S1000IncreaseChoices INTO changeType FROM store_store WHERE StoreID = storeId_param;
This is a Bug and you need to apply something like that:
SELECT id,data INTO x,y FROM test.t1 LIMIT 1;
I have a stored procedures with the following code. The reason i use cursor is to join table which something will return NULL value and cause the record to be disappear. By using this method, I am able to get all data without losing any.
The only problem now is that when i try to call the stored precedures, it return
Error Code : 1329
No data - zero rows fetched, selected, or processed
but when i do a manual select * from TMOMain, the table is created and there is data in it but no data from SignUpCur and UnSubCur mean it was not updated.
1st time using mysql stored procedures so there might be something i miss out.
My Code
ROOT:BEGIN
DECLARE pTotal,pShortCode,pSignUp,pUnSub,pJunk,pT INT;
DECLARE pTc NVARCHAR(10);
DECLARE SignTotal,UnSubTotal, JunkTotal INT;
DECLARE pSignTotal,pSignTeamID,pUnSubTotal,pUnSubT,pSignUpS,pUnSubS INT;
DECLARE pSignTeam,pUnSubTeam NVARCHAR(10);
DECLARE no_more_rows BOOLEAN;
DECLARE MoMainCur CURSOR FOR
SELECT COUNT(*) AS GrandTotal,pShort,(CASE WHEN r= 1 THEN 'A'
WHEN r= 2 THEN 'B' WHEN r= 3 THEN 'C' ELSE 'UV' END) AS Team,recvTeamID
FROM tbli
INNER JOIN tblK ON keywordid = rkey
WHERE recvDate >='2011-11-15' AND recvDate < '2011-11-16'
GROUP BY pShort,Team,recvTeamID;
DECLARE SignUpCur CURSOR FOR
SELECT COUNT(*) AS SignUp,(CASE WHEN r= 1 THEN 'A'
WHEN r= 2 THEN 'B' WHEN r= 3 THEN 'C' ELSE 'UV' END) AS Team,
recvTeamID,pShort
FROM tbli INNER JOIN tbl_user ON recvphone = userphone
INNER JOIN tblK ON keywordid = userpublicstatus
WHERE userdatejoined >='2011-11-15' AND userdatejoined < '2011-11-16'
AND recvdate >='2011-11-15' AND recvdate < '2011-11-16'
GROUP BY Team,recvTeamID,pShort;
DECLARE UnSubCur CURSOR FOR
SELECT COUNT(*) AS UnSub,(CASE WHEN r= 1 THEN 'A'
WHEN r= 2 THEN 'B' WHEN r= 3 THEN 'C' ELSE 'UV' END) AS Team,
recvTeamID,pShort
FROM tbliINNER JOIN tbl_user ON recvphone = userphone
INNER JOIN tblK ON keywordid = userpublicstatus
WHERE userdateExpire >='2011-11-15' AND userdateExpire <'2011-11-16'
AND recvdate >='2011-11-15' AND recvdate < '2011-11-16'
GROUP BY Team,recvTeamID,pShort;
DROP TABLE IF EXISTS `TMoMain`;
CREATE TEMPORARY TABLE TMOMain
(GrandTotal INT,ShortCode INT,Team NVARCHAR(10),SignUp INT,UnSub INT, Junk INT, TeamID INT);
OPEN MoMainCur;
-- Main Table
read_loop:LOOP
FETCH MoMainCur INTO pTotal,pShortCode,pTc,pT;
INSERT INTO TMOMain
VALUES
(pTotal,pShortcode,pTc,0,0,0,pT);
END LOOP read_loop;
CLOSE MoMainCur;
-- Insert Signup Details into Main Table
OPEN SignUpCur;
SignUp_Loop:LOOP
FETCH SignUpCur INTO pSignTotal,pSignTeam,pSignTeamID,pSignUpS;
UPDATE TMOMain
SET SignUp = pSignTotal
WHERE Team = pSignTeam AND Shortcode =pSignUpS;
END LOOP SignUp_Loop;
CLOSE SignUpCur;
-- Insert UnSub Details into Main Table
OPEN UnSubCur;
UnSub_Loop:LOOP
FETCH UnSubCur INTO pUnSubTotal,pUnSubTeam,pUnSubT,pUnSubS;
UPDATE TMOMain
SET UnSub = pSignTotal
WHERE Team = pUnSubTeam AND pShort = pUnSubShortCode;
END LOOP UnSub_Loop;
CLOSE UnSubCur;
SELECT * FROM TMOMain;
END$$
Please try this out:
Add this declaration once (at the top):
DECLARE curIsDone INT DEFAULT FALSE;
Then after you declare your cursor add this:
DECLARE CONTINUE HANDLER FOR NOT FOUND SET curIsDone = TRUE;
After your FETCH commands and before the actions you intend to perform:
IF curIsDone THEN
LEAVE read_loop;
END IF;
my question is too easy (I guess), below is an example of trigger that I am trying to remove the aliases. I don't know what I am doing wrong I just can't get it right.
DELIMITER #
CREATE TRIGGER StartOfShift BEFORE INSERT ON shift
FOR EACH ROW
BEGIN
IF(NEW.CashierCode NOT IN ( SELECT w.EmployeeID FROM WorksOn as w
JOIN shop AS s ON w.ShopID = s.ShopID
JOIN CashMachineCode AS c ON s.ShopID = c.ShopID
WHERE c.CashMachineCode = NEW.CashMachineCode ))
THEN SET NEW.CashierCode = NULL;
END IF;
END;
The following should be what you are looking for:
DELIMITER #
CREATE TRIGGER StartOfShift BEFORE INSERT ON shift
FOR EACH ROW
BEGIN
IF(NEW.CashierCode NOT IN (
SELECT WorksOn.EmployeeID FROM WorksOn
JOIN shop ON WorksOn.ShopID = shop.ShopID
JOIN CashMachineCode ON shop.ShopID = CashMachineCode.ShopID
WHERE CashMachineCode.CashMachineCode = NEW.CashMachineCode )
) THEN
SET NEW.CashierCode = NULL;
END IF;
END#
DELIMITER ;