简体   繁体   English

MySQL-Concat-过程执行失败

[英]Mysql - Concat - Procedure execution failed

I have 123 tables as well: M001 -> M010 -> M123. 我也有123个表格:M001-> M010-> M123。 each is a client. 每个都是一个客户。 when it reaches a record to a parent table with a trigger call a function like this: 当它通过触发器到达父表的记录时,调用以下函数:

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

This is my function: 这是我的功能:

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

But it always throws me the following error: 但这总是引发以下错误:

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

Thanks for the Help of All 感谢大家的帮助

Try this script - 试试这个脚本-

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM