mercredi 5 août 2015

Store Mysql CASE select statement result in a variable


I have the following MySQL trigger:

CREATE TRIGGER `upd_interim_final` AFTER INSERT ON `oee_main_interim`
 FOR EACH ROW 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) 
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

  (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) As curr_Shift
From
  oee_machinenames
  where 
  oee_machinenames.ID = New.NAME

Group By
  oee_machinenames.ID),


  (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))

  )

I am trying to store the result of this CASE clause in a variable to use it later on in the trigger:

 ( 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) As curr_Shift
    From
      oee_machinenames
      where 
      oee_machinenames.ID = New.NAME

    Group By
      oee_machinenames.ID),

I have tried adding the following to the beginning of the trigger to no success:

DECLARE Shifts TEXT;
SET @Shifts := ( 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) As curr_Shift
    From
      oee_machinenames
      where 
      oee_machinenames.ID = New.NAME

    Group By
      oee_machinenames.ID);

Any help would be appreciated. Thanks



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire