日期:2014-05-16  浏览次数:20822 次

mysql 存储过程 自定义表名错误
建立一个过程
CREATE DEFINER=`root`@`localhost` PROCEDURE `insert_me`(IN `Src` char(80),IN `Dst` char(80),in Uid char(30))
BEGIN
  declare Dbname varCHAR(32) DEFAULT('2012-05-07') ;
  IF Src <> '' and Dst <> '' then
insert into Dbname (calldate,src,dst,tag)
  select `calldata`,`src`,`dst`,`tag` from `ask`.`tome` WHERE uid = Uid;
  end if;
END

怎么在call insert_my('802','13632510278','1333107047.19') 总是提交test.Dbname 不存在呢?过程里Dbname 不是已经设为2012-05-07了吗

------解决方案--------------------
set @asql='insert into Dbname (calldate,src,dst,tag)
select `calldata`,`src`,`dst`,`tag` from `ask`.`tome` WHERE uid = Uid';
prepare stml from @asql;
execute stml;
------解决方案--------------------
set @asql=concat('insert into ',Dbname,' (calldate,src,dst,tag)
select `calldata`,`src`,`dst`,`tag` from `ask`.`tome` WHERE uid = Uid');
prepare stml from @asql;
execute stml;
------解决方案--------------------
SQL code

SET @sqltext=concat('insert into ',Dbname,' (calldate,src,dst,tag)
  select `calldata`,`src`,`dst`,`tag` from `ask`.`tome` WHERE uid = Uid'');
    PREPARE sqlstr from @sqltext;
    Execute sqlstr;

------解决方案--------------------
SQL code

drop PROCEDURE if exists insert_me;
CREATE PROCEDURE `insert_me`(IN `Src` char(80),IN `Dst` char(80),in Uid char(30))
BEGIN
  declare Dbname varCHAR(32) DEFAULT('2012-05-07') ;
  SET @UUid = Uid;
  IF Src <> '' and Dst <> '' then
  SET @sqltext=concat('insert into ',Dbname,' (calldate,src,dst,tag)
  select `calldata`,`src`,`dst`,`tag` from `ask`.`tome` WHERE uid = @UUid');
    PREPARE sqlstr from @sqltext;
    Execute sqlstr;
  end if;
END;