I have a trigger which is storing the value of a case statement in variable @SHIFTIME and this is working fine by returning values Shift1, Shift2 or Shift3. I now need to use the result of this variable in a select statement referring to the COLUMN NAME. I have tried storing this in variable @SHIFTSELECT:
SET @SHIFTSELECT := (SELECT @SHIFTIME FROM `oee_machinenames` where `oee_machinenames`.`ID` = New.NAME);
So @SHIFTIME is the column name and I expect @SHIFTSELECT to interpret this as:
SELECT `Shift1` FROM `oee_machinenames` where `oee_machinenames`.`ID` = New.NAME
BUT the problem is that @SHIFTSELECT is returning the same value as @SHIFTIME namely the column name (Shift1, Shift2 or Shift3) instead of the required ROW result. This is my complete trigger:
CREATE TRIGGER `oee_upd_final` AFTER INSERT ON `oee_main_interim`
FOR EACH ROW BEGIN
DECLARE SHIFTIME TEXT;
DECLARE SHIFTSELECT TEXT;
SET @SHIFTIME := ( Select
(Case
When ((CurTime() > oee_machinenames.Shift1) And
(CurTime() < oee_machinenames.Shift2)) Then 'Shift1'
When ((CurTime() > oee_machinenames.Shift2) And
(CurTime() < oee_machinenames.Shift3)) Then 'Shift2'
When ((CurTime() > oee_machinenames.Shift3) Or
(CurTime() < oee_machinenames.Shift1)) Then 'Shift3' End)
From
oee_machinenames
where
oee_machinenames.ID = New.NAME
Group By
oee_machinenames.ID);
SET @SHIFTSELECT := (SELECT @SHIFTIME FROM `oee_machinenames` where `oee_machinenames`.`ID` = New.NAME);
INSERT INTO `oee_main_interim_final` (id,NAME,ts,Left_IO,Left_NIO,Recovery,Right_IO,
Right_NIO,RunMode,S_TYPE,Shift,STD,curr_S_Type,Reporting_Date, Shift_TS)
VALUES(NULL, New.NAME, New.TS, NEW.Left_IO, New.Left_NIO, New.Recovery, New.Right_IO, New.Right_NIO, New.RunMode, New.S_TYPE,
(select @SHIFTIME),
(Select
`STD` From `oee_variant` Where `Machine_ID` = New.NAME And `S_TYPE` =
(Select
`S_TYPE`
From
`v_getmaxid`
Where
`NAME` = New.Name And
v_getmaxid.Max_id In (Select
Max(v_getmaxid.Max_id) As Max_Max_id
From
`v_getmaxid`
Where
`NAME` = New.Name))
And `oee_variant`.`Operators` =
(Select `Operators` from `oee_machinenames` where `ID` = New.NAME)),
(Select
`S_TYPE`
From
`v_getmaxid`
Where
`NAME` = New.Name And
v_getmaxid.Max_id In (Select
Max(v_getmaxid.Max_id) As Max_Max_id
From
`v_getmaxid`
Where
`NAME` = New.Name)),
(select((case when (((@SHIFTIME = 'Shift3') and
(cast(New.TS as time) >= '00:00:01'))
or (cast(New.TS as time) <= '05:59:00'))
then (New.TS - interval 1 day) else cast(New.TS as date) end)) AS Reporting_Date),
(SELECT @SHIFTSELECT)
);
END