MySql update procedure error - mysql

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`;

Related

MySQL procedure results in a syntax error

This is my MySQL procedure. I got an error while executing this.
DELIMITER //
CREATE PROCEDURE GET_ORDER_HISTORY_LIST(IN distid int)
BEGIN
SELECT
ort.order_id,
ort.transaction_id,
ort.user_id,
ort.transaction_date,
ort.insert_by,
ort.organization_id,
odt.course_id,
count(odt.quantity),
ct.course_name,
ct.course_code
FROM cdp_order_master as ort
JOIN cdp_order_detail as odt ON odt.order_id = ort.order_id
JOIN cdp_course as ct ON ct.course_id = odt.course_id
WHERE ort.user_id = distid
GROUP BY ort.order_id
END //
DELIMITER ;
Error is
#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 'END' at line 19
DELIMITER //
CREATE PROCEDURE GET_ORDER_HISTORY_LIST(IN distid int)
BEGIN
SELECT
ort.order_id,
ort.transaction_id,
ort.user_id,
ort.transaction_date,
ort.insert_by,
ort.organization_id,
odt.course_id,
count(odt.quantity),
ct.course_name,
ct.course_code
FROM cdp_order_master as ort
JOIN cdp_order_detail as odt ON odt.order_id = ort.order_id
JOIN cdp_course as ct ON ct.course_id = odt.course_id
WHERE ort.user_id = distid
GROUP BY ort.order_id; //I had made change at this line.
END //
DELIMITER ;
You can try above query.
Here is a SQL Fiddle.

MIssing semicolon error in stored procedure mysql workbench

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;

How to Create Trigger After Insert with select data from Multiple table with Join in MYSql

I am trying to Create Trigger in MySQL with Select Column data from Join multiple table. But Trigger is not allow me to DECLARE temp variable.
I would like to join 4 table on the bases of newly inserted record in one table and select the data from different table and insert r update in another table (DashboardStatus)
I am getting error [ SQL 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 'SET _BedId = (SELECT bd.BedId
FROM LifetouchHeartRate lthr
JOIN Device' at line 4 */ ]
CREATE TABLE `dashboardstatus` (
`LTHR` INT(11) NULL DEFAULT NULL,
`BedId` INT(11) NULL DEFAULT NULL
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB;
CREATE TRIGGER triggerDashboard AFTER INSERT ON LifetouchHeartRate for each row
BEGIN
DECLARE _BedId INT
SET _BedId = (SELECT bd.BedId As _BedId
FROM LifetouchHeartRate lthr
JOIN DeviceSession ds ON ds.DeviceSessionID = lthr.ByDevSessionId
JOIN PatientSession ps ON ps.PatientSessionId = ds.ByPatientSessionId
JOIN PatientDetails pd ON pd.PatientDetailsId = ps.ByPatientId
JOIN BedDetails bd ON bd.BedDetailsId = pd.ByBedId
WHERE lthr.LifeTouchHeartRateID = new.LifeTouchHeartRateID Limit 1 );
IF _BedId > 0
BEGIN
INSERT OR REPLACE INTO DashboardStatus (LTHR, BedId) VALUES ( new.LifeTouchHeartRateID, _BedId)
END
END
Remember ;
...
CREATE TRIGGER triggerDashboard AFTER INSERT ON LifetouchHeartRate for each row
BEGIN
-- DECLARE _BedId INT
DECLARE _BedId INT;
...
UPDATE
DELIMITER //
CREATE TRIGGER triggerDashboard AFTER INSERT ON LifetouchHeartRate
FOR EACH ROW
BEGIN
-- DECLARE _BedId INT
DECLARE _BedId INT;
SET _BedId = (SELECT bd.BedId As _BedId
FROM LifetouchHeartRate lthr
JOIN DeviceSession ds ON ds.DeviceSessionID = lthr.ByDevSessionId
JOIN PatientSession ps ON ps.PatientSessionId = ds.ByPatientSessionId
JOIN PatientDetails pd ON pd.PatientDetailsId = ps.ByPatientId
JOIN BedDetails bd ON bd.BedDetailsId = pd.ByBedId
WHERE lthr.LifeTouchHeartRateID = new.LifeTouchHeartRateID Limit 1 );
/*IF _BedId > 0
BEGIN
INSERT OR REPLACE INTO DashboardStatus (LTHR, BedId) VALUES ( new.LifeTouchHeartRateID, _BedId)
END
END*/
IF _BedId > 0 THEN
-- BEGIN
INSERT INTO DashboardStatus (LTHR, BedId) VALUES ( new.LifeTouchHeartRateID, _BedId);
-- END
END IF;
END//
DELIMITER ;
SQL Fiddle demo

stored procedure in mysql in ubuntu

I have created stored procedure in MySQL in Window-7. it run successfully on windows. But when I switch to Ubuntu it gives error in the stored procedure. On windows I am using SQLyog for creating stored procedure. On Ubuntu, I run the SQL script and call that stored procedure but it gives error. Below is my stored procedure.
DELIMITER $$
USE `adserver`$$
DROP PROCEDURE IF EXISTS `getDaypartTimeDetail`$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `getDaypartTimeDetail`
(currentDate DATE,noOfdays INT,cityId BIGINT)
BEGIN
DECLARE i INT;
DECLARE dateCnt INT;
SET dateCnt = 0;
DROP TEMPORARY TABLE IF EXISTS OnlyDate;
DROP TEMPORARY TABLE IF EXISTS AdvScheduleData;
CREATE TEMPORARY TABLE OnlyDate(dday DATE);
CREATE TEMPORARY TABLE AdvScheduleData(dday BIGINT,daypartId INT,totalFile BIGINT,totalDur BIGINT);
/* Generate Dates */
WHILE(dateCnt < noOfdays) DO
SET i = 1;
INSERT INTO OnlyDate(dday) VALUES (DATE_ADD(currentDate, INTERVAL dateCnt DAY));
SET dateCnt = dateCnt + 1;
END WHILE;
/* Insert all dayparts for all dates */
INSERT INTO AdvScheduleData (dday, daypartID) SELECT (UNIX_TIMESTAMP(dday)*1000), id FROM OnlyDate, daypart;
/* Update total files and duration */
UPDATE AdvScheduleData SET
TotalFile = (SELECT COUNT(advt_id)
FROM adv_schedule AdvSch
INNER JOIN advertisement Adv ON Adv.id = AdvSch.advt_id
WHERE AdvScheduleData.dday BETWEEN AdvSch.start_date AND AdvSch.end_date
AND AdvSch.status = 2
AND AdvSch.active = 1
AND AdvSch.id IN (SELECT schedule_id FROM schedule_daypart
WHERE daypart_id = AdvScheduleData.daypartId )
AND AdvSch.id IN (SELECT schedule_id FROM schedule_cities WHERE city_id = cityId)
AND Adv.is_active = 1
AND Adv.is_deleted = 0
AND Adv.status = 2
AND Adv.expiry_date >= AdvScheduleData.dday),
totalDur = (SELECT SUM(Adv.duration)
FROM adv_schedule AdvSch
INNER JOIN advertisement Adv ON Adv.id = ADVSCH.advt_id
WHERE AdvScheduleData.dday BETWEEN AdvSch.start_date AND AdvSch.end_date
AND AdvSch.status = 2
AND AdvSch.active = 1
AND AdvSch.id IN (SELECT schedule_id FROM schedule_daypart
WHERE daypart_id = AdvScheduleData.daypartId )
AND AdvSch.id IN (SELECT schedule_id FROM schedule_cities WHERE city_id = cityId)
AND Adv.is_active = 1
AND Adv.is_deleted = 0
AND Adv.status = 2
AND Adv.expiry_date >= AdvScheduleData.dday);
SELECT * FROM AdvScheduleData;
END$$
DELIMITER ;
The output I get in Ubuntu is
mysql> call getDaypartTimeDetail('2012-08-13',5,30534); ERROR 1054
(42S22): Unknown column 'ADVSCH.advt_id' in 'on clause'
Table name and aliases are case sensitive in Ubuntu.
Hence, this reference is giving an error:
totalDur = (SELECT SUM(Adv.duration)
FROM adv_schedule AdvSch
INNER JOIN advertisement Adv ON Adv.id = ADVSCH.advt_id
Change it to AdvSch.advt_id

Help on removing the aliases from a MySQL trigger

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 ;