【问题标题】:Mysql - Concat - Procedure execution failedMysql - Concat - 过程执行失败
【发布时间】:2012-06-18 12:10:29
【问题描述】:

我也有 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

感谢大家的帮助

【问题讨论】:

  • 您将MasterX 数字值与字符串“lecturas.M0”连接起来。你期待什么?
  • 可以将记录保存在表 lecturas.M00123 或 lecturas.M0013 或 lecturas.M001 中,具体取决于 MasterX

标签: mysql triggers procedure concat


【解决方案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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-22
    • 2014-01-03
    • 2023-04-11
    • 1970-01-01
    相关资源
    最近更新 更多