0

我也有 123 张桌子:M001 -> M010 -> M123。每个人都是客户。当它到达带有触发器的父表的记录时,调用如下函数:

  Declare MasterX  int;
  Set MasterX = New.Master;
  Call Lecturas_Insertar(MasterX,New.Id);

这是我的功能:

BEGIN

#Set Master
  If MasterX < 10 Then 
  Set MasterX = Concat("lecturas.M00",MasterX);
  End If;
#Set Master
  If MasterX Between 10 and 99 Then 
  Set MasterX = Concat("lecturas.M0",MasterX);
  End If;

  set @a=concat("INSERT INTO ",MasterX, "(Id) Values(" ,Id, ")");
  PREPARE stmt1 FROM @a;
  EXECUTE stmt1; 
  DEALLOCATE PREPARE stmt1;

END

但它总是向我抛出以下错误:

  Procedure execution failed
  1146 - Table 'lecturas.M' does not exist

感谢大家的帮助

4

1 回答 1

1

试试这个脚本 -

BEGIN
  SET @a = CONCAT('INSERT INTO lecturas.M', LPAD(MasterX, 3, 0), '(Id) Values(', Id, ')');
  PREPARE stmt1 FROM @a;
  EXECUTE stmt1; 
  DEALLOCATE PREPARE stmt1;
END
于 2012-06-18T14:20:28.500 回答