MIssing semicolon error in stored procedure mysql workbench - mysql

HI I am new to mysql and trying to update table through stored procedure.I am getting "missing semicolon error.I tried everything but I couldn't able to understand why is this happening.
procedure:
DROP PROCEDURE IF EXISTS abc;
use smartdata;
DELIMITER $$
CREATE PROCEDURE abc(IN datasourceId int)
begin
update DM_Sample_Search_Param_Disposition_Type set DM_Sample_Search_Param_Disposition_Type.Active = 0
From DM_Sample_Search_Param_Disposition_Type dm_ss_param_cl
left join DM_Sample_Search_Param dm_ss_param on dm_ss_param_cl.DM_Sample_Search_Param_id =dm_ss_param.DM_Sample_Search_Param_id
left join DM_Sample_Store dm_ss on dm_ss.DM_Sample_Store_Id=dm_ss_param.DM_Sample_Store_Id
where dm_ss.Datasource_Id=datasourceId;
end $$
DELIMITER //
Error in near below line:
set DM_Sample_Search_Param_Disposition_Type.Active = 0
where "Active" is tinyint in the table.Please help

MySQL uses a different syntax for update than MSSQL: update ... join .. set ... where ...
update DM_Sample_Search_Param_Disposition_Type dm_ss_param_cl
left join DM_Sample_Search_Param dm_ss_param on dm_ss_param_cl.DM_Sample_Search_Param_id =dm_ss_param.DM_Sample_Search_Param_id
left join DM_Sample_Store dm_ss on dm_ss.DM_Sample_Store_Id=dm_ss_param.DM_Sample_Store_Id
set DM_Sample_Search_Param_Disposition_Type.Active = 0
where dm_ss.Datasource_Id=datasourceId;

Wrong join table sintax
update DM_Sample_Search_Param_Disposition_Type as dm_ss_param_cl
left join DM_Sample_Search_Param dm_ss_param on dm_ss_param_cl.DM_Sample_Search_Param_id =dm_ss_param.DM_Sample_Search_Param_id
left join DM_Sample_Store dm_ss on dm_ss.DM_Sample_Store_Id=dm_ss_param.DM_Sample_Store_Id
set DM_Sample_Search_Param_Disposition_Type.Active = 0
where dm_ss.Datasource_Id=datasourceId;

Related

Error while returning multiple ResulSets from a stored procedure in MySQL

Here's shortened script of my stored procedure:
DROP PROCEDURE IF EXISTS `GetVehicleDetails`;
DELIMITER //
CREATE PROCEDURE `GetVehicleDetails`(
IN `inRefNo` VARCHAR(30) COLLATE utf8mb4_general_ci,
IN `inSurveyType` VARCHAR(20) COLLATE utf8mb4_general_ci
)
BEGIN
DECLARE vehicleTypeID VARCHAR(2);
SET FOREIGN_KEY_CHECKS = OFF;
SELECT * FROM vehicle_details V
LEFT JOIN vehicle_types VT ON VT.TypeID = V.VehicleType
LEFT JOIN vehicle_makes VM ON VM.TypeID = V.VehicleType AND VM.MakeID = V.VehicleMake
LEFT JOIN vehicle_models VD
ON VD.TypeID = V.VehicleType AND VD.MakeID = V.VehicleMake AND VD.ModelID = V.VehicleModel
LEFT JOIN vehicle_variants VV
ON VV.TypeID = V.VehicleType
AND VV.MakeID = V.VehicleMake
AND VV.ModelID = V.VehicleModel
AND VV.VariantID = V.VehicleVariant
LEFT JOIN vehicle_body_types VB ON VB.BodyTypeID = V.TypeOfBody
LEFT JOIN vehicle_info_preinspection VP ON VP.RefNo = inRefNo
LEFT JOIN fuel_types F ON F.FuelTypeID = VP.Fuel
WHERE V.RefNo = inRefNo;
# Fetch Vehicle Type
SELECT VehicleType INTO vehicleTypeID FROM vehicle_details WHERE RefNo = inRefNo;
# Get details of body parts
IF vehicleTypeID = 1 THEN /* Personal Car */
SELECT * FROM body_parts_personal_car WHERE RefNo = inRefNo;
/*IF inSurveyType = 'preinspection' THEN
SELECT * FROM accessories_personal_car WHERE RefNo = inRefNo;
END IF;*/
ELSEIF vehicleTypeID = 4 THEN /* 2 Wheeler */
SELECT * FROM body_parts_2_wheeler WHERE RefNo = inRefNo;
ELSE
SELECT * FROM body_parts_commercial_vehicle WHERE RefNo = inRefNo;
END IF;
SET FOREIGN_KEY_CHECKS = ON;
END//
DELIMITER ;
Now, while executing the stored procedure with this statement:
CALL GetVehicleDetails('some ref no', 'interim-survey');
an error is being thrown:
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
I have noticed that the stored procedure is throwing on second SELECT statement -
SELECT * FROM body_parts_personal_car WHERE RefNo = inRefNo;
in my case. Even if I write SELECT Now(); or SELECT vehicleTypeID; before it, the stored procedure throws the same error. If I comment this SELECT statement out, the stored procedure WORKS.
The same stored procedure works on localhost perfectly. I am using phpMyAdmin on remote server to maintain my database.
Any help please?
EDIT: I am receiving same problem in all stored procedures which have multiple SELECT statements to be returned back as ResultSet.
And, if I click Execute from the list of stored procedures in phpMyAdmin, the stored procedure executes. But if I invoke the stored procedure with CALL <proc_name()>;, the above error is displayed.
The "Commands out of sync" error usually indicates the client side error (results from the previous result set have not been processed and remain in the buffer).
If you are running the procedure from the phpMyAdmin, note that phpMyAdmin does not know to handle procedures returning multiple result sets. Try to run the command from MySQL command prompt and see if you get any errors.
The procedure itself looks ok, apart from unnecessary SET FOREIGN_KEY_CHECKS-commands (the procedure does not do any update/insert).
There problem was with one or more of tables involved in the procedure. Some queries and procedure calls inside a procedure were giving errors as "illegal mix of collations xxxxxxxx". I had to set COLLATIONS of all char/varchar/enum fields of tables and parameters of procedures giving errors to utf8mb4_general_ci.

Syntax error when add trigger

I have procedure:
CREATE PROCEDURE channge_max_priority()
BEGIN
update feeds set max_post_priority =
(
select max(feed_posts.priority)
from feed_posts
where feed_posts.feed_id = feed_id
)
where feeds.id = feed_id;
END
And I want to create three triggers:
CREATE TRIGGER feed_post_after_test AFTER INSERT (And update and delete ) ON feed_posts
FOR EACH ROW EXECUTE PROCEDURE change_max_priority();
Procedure work correctly, but when I try start trigger - I get error:
Error in query (1064): Syntax error near 'PROCEDURE change_max_priority()' at line 2
You have a double nn in name procedure :
replace :
CREATE PROCEDURE channge_max_priority()
by
CREATE PROCEDURE change_max_priority()
Try changing the update statement to use a join instead, this should work:
update feeds
set max_post_priority = max(feed_posts.priority)
FROM feeds f
INNER JOIN feed_posts fp on feed_posts.feed_id = f.feed_id
Group BY f.feed_id
In mysql, a procedure is executed using CALL:
CREATE TRIGGER feed_post_after_test AFTER INSERT (And update and delete ) ON feed_posts
FOR EACH ROW CALL change_max_priority();
I don't see why you should execute the procedure for each row though, the whole feeds table is update in one procedure call.
Also not sure the last where feeds.id = feed_id; (just before END) is correct in the procedure.

How to trigger the procedure for the sql database in system files

I have a table that holds a bunch of values for an order, I can do basic calculations on it until I get to a percentage. Right now I have my query as follows
declare #MyNumber decimal
set #MyNumber = (select SalesTax from [OrderHeader] where OrderHeaderID = 20)
select
sum(o.MaterialPrice) as "MatPrice",
sum(o.LaborPrice) as "LaborPrice",
sum(o.MaterialCost) as "MaterialCost",
sum(isnull(o.MaterialPrice,0)) - sum(isnull(o.MaterialCost,0)) - sum(isnull(o.LaborPrice,0)) * #MyNumber as "RESULT"
from [OrderDetail] o
inner join [OrderHeader] oh on oh.OrderHeaderID = o.OrderHeaderID
where o.OrderHeaderID = 20
PLEASE CHANGE AT LEAST NAZOV
DELIMITER $$
CREATE TRIGGER `nazov` BEFORE UPDATE
ON test
FOR EACH ROW BEGIN
SET NEW.OLD_TEXT=OLD.TEXT;
END $$
DELIMITER;

Procedure table in other database

I have a database for a game server and another one for web.
I would like to call a procedure which take a data from a table of the web database.
I have seen many examples like this 'db1.dbo.table_name' but I don't know what is dbo (Data Base Owner ?)
Here is my actual procedure
CREATE DEFINER=`root`#`localhost` PROCEDURE `donationCheck`()
BEGIN
UPDATE `gameserver.players` SET `donatorlvl` = (SELECT `grade` FROM `web.donations` as dona WHERE dona.steamid = player.playerid) where steamid = '76561198125956777';
END
The error is that gameserver.web.donations does'nt exists (normal);
Sorry for my bad english.
CREATE DEFINER=`root`#`localhost` PROCEDURE `donationCheck`()
BEGIN
UPDATE gameserver.players a
INNER JOIN web.donations b ON a.playerid = b.steamid
SET a.donatorlvl = b.grade;
END

MySql update procedure error

I have problem with my update query. I need to make it works :P
That procedure select all duplicated records ( need to be recreated to select and update value)
DELIMITER $$
USE `bgw_r`$$
DROP PROCEDURE IF EXISTS `tpl_pobierz_dodane`$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `tpl_pobierz_dodane`(puzytkownik VARCHAR(50), psesja VARCHAR(50))
BEGIN
SELECT a.`id` AS idd FROM `paliwa_temp` AS a INNER JOIN `paliwo_fv` AS b
ON (a.`numerRejestracyjny` = b.`numer_rejestracyjny`)
WHERE a.`sesja` = psesja AND b.`data_tankowania` = a.`dataTankowania` AND a.`iloscPaliwa` = b.`wydano_litry`; # GROUP BY id
END$$
DELIMITER ;
or create other procedure that update that values:
DELIMITER $$
USE `bgw_r`$$
DROP PROCEDURE IF EXISTS `tpl_ustaw_jako_duplikaty`$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `tpl_ustaw_jako_duplikaty`(puzytkownik VARCHAR(50), psesja VARCHAR(50))
BEGIN
UPDATE a
SET a.`duplikat` = "true"
FROM `paliwa_temp` AS a
INNER JOIN `paliwo_fv` AS b
ON( b.`numer_rejestracyjny` = a.`numerRejestracyjny`)
WHERE a.`sesja` = psesja AND b.`data_tankowania` = a.`dataTankowania` AND a.`iloscPaliwa` = b.`wydano_litry`;
END$$
DELIMITER ;
Error in update query:
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 'FROM `paliwa_temp` as a
INNER JOIN `paliwo_fv` as b
on( b.`numer_rejestracyjny' at line 5
No idea why :/
The correct join update statement should be as
update paliwa_temp a
INNER JOIN `paliwo_fv` AS b
ON( b.`numer_rejestracyjny` = a.`numerRejestracyjny`)
SET a.`duplikat` = "true"
WHERE a.`sesja` = psesja
AND b.`data_tankowania` = a.`dataTankowania`
AND a.`iloscPaliwa` = b.`wydano_lit
The syntax of update with join must be:
UPDATE `paliwa_temp` AS a
INNER JOIN `paliwo_fv` AS b
ON( b.`numer_rejestracyjny` = a.`numerRejestracyjny`)
SET a.`duplikat` = "true"
WHERE a.`sesja` = psesja AND b.`data_tankowania` = a.`dataTankowania` AND a.`iloscPaliwa` = b.`wydano_litry`;