MySQL从变量

时间:2015-09-17 10:29:20

标签: mysql triggers

我有一个触发器,它将case语句的值存储在变量@SHIFTIME中,这可以通过返回值Shift1,Shift2或Shift3来正常工作。我现在需要在引用COLUMN NAME的select语句中使用此变量的结果。我已经尝试将其存储在变量@SHIFTSELECT:

  SET @SHIFTSELECT := (SELECT @SHIFTIME FROM `oee_machinenames` where `oee_machinenames`.`ID` = New.NAME); 

所以@SHIFTIME是列名,我希望@SHIFTSELECT将其解释为:

SELECT `Shift1` FROM `oee_machinenames` where `oee_machinenames`.`ID` = New.NAME

但问题是@SHIFTSELECT返回的值与@ SHIFTIME相同,即列名(Shift1,Shift2或Shift3),而不是所需的ROW结果。这是我的完全触发器:

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

2 个答案:

答案 0 :(得分:2)

不,您不能将列名用作变量,除非它是您在应用程序代码中进行的动态查询(或)。动态查询方式就像

SET @sql = CONCAT("SELECT ", @SHIFTIME," FROM `oee_machinenames` where `oee_machinenames`.`ID` = New.NAME");
 PREPARE stmt FROM @sql;
 EXECUTE stmt;
 DEALLOCATE PREPARE stmt;

然后将准备好的陈述改为

SET @result = '';
SET @sql = CONCAT("SELECT ? INTO @result FROM `oee_machinenames` where `oee_machinenames`.`ID` = New.NAME");
 PREPARE stmt FROM @sql;
 EXECUTE stmt  USING @SHIFTIME;
SELECT @result; // Set the data to some other variable
 DEALLOCATE PREPARE stmt;

编辑:啊!没注意到它是一个触发器。认为这是一个程序。是的,你不能但是你的查询得到@SHIFTIME和SET @SHIFTSELECT := ...这两部分可以合并到一个修改过的查询,如下所示,所以不需要动态查询。

Select 

      (Case
        When ((CurTime() > oee_machinenames.Shift1) And
        (CurTime() < oee_machinenames.Shift2)) Then oee_machinenames.Shift1
        When ((CurTime() > oee_machinenames.Shift2) And
        (CurTime() < oee_machinenames.Shift3)) Then oee_machinenames.Shift2
        When ((CurTime() > oee_machinenames.Shift3) Or
        (CurTime() < oee_machinenames.Shift1)) Then oee_machinenames.Shift3 End) 
    From
      oee_machinenames
      where oee_machinenames.ID = New.NAME

    Group By
      oee_machinenames.ID);

答案 1 :(得分:0)

您可以按照以下方式使用 -

SET @qry = CONCAT('SELECT ',@SHIFTIME,' INTO @SHIFTSELECT FROM `oee_machinenames` where `oee_machinenames`.`ID` = New.NAME');
PREPARE stmt FROM @qry;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;