我正在尝试创建一个允许我输入表名作为变量(tblname)的过程,然后对该表执行一些操作。我在函数中引用的callingparty和callduration列是将要使用的任何表的预先存在的列。我是创建程序的新手,每次修复错误时都会弹出另一个程序。
目前我收到错误:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'concat(short, tblname); create temporary table temp (index callingparty (calling' at line 8
我不知道如何解决这个或我的语法有什么问题。是否只能创建一个包含旧表名称的新表?
drop procedure if exists shortcallcount;
delimiter $$
create procedure shortcallcount(`tblname` varchar(32))
begin
drop table if exists temp;
drop table if exists temp2;
drop table if exists temp3;
drop table if exists join1;
drop table if exists concat(short, tblname);
create temporary table temp (index callingparty (callingparty)) as select callingparty, count(*) as totalcount from tblname group by callingparty;
create temporary table temp2 (index callingparty (callingparty)) as select callingparty, count(*) as tencount from tblname where callduration<1000 group by callingparty;
create temporary table temp3 (index callingparty (callingparty)) as select callingparty, count(*) as fourcount from tblname where callduration<492 group by callingparty;
create temporary table join1 (index callingparty (callingparty)) as select temp.callingparty, ifnull(temp3.fourcount, 0) as fourcount, temp.totalcount from temp left outer join temp3 on temp.callingparty=temp3.callingparty;
create table concat(short, tblname) (index callingparty (callingparty)) as select join1.callingparty, join1.fourcount; ifnull(temp2.tencount, 0) as tencount, join1.totalcount from join1 left outer join temp2 on join1.callingparty=temp2.callingparty;
drop table temp;
drop table temp2;
drop table temp3;
drop table join1;
end$$
delimiter ;
答案 0 :(得分:0)
您可以使用预准备语句,因为变量名称不会对表名或列名进行评估。
delimiter $$
create procedure shortcallcount(tblname varchar(32))
begin
drop table if exists temp;
drop table if exists temp2;
drop table if exists temp3;
drop table if exists join1;
-- drop table if exists concat(short, tblname);
set @sql = concat("drop table if exists short", tblname,";");
PREPARE stmt1 FROM @sql;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
create temporary table temp (index callingparty (callingparty)) as select callingparty, count(*) as totalcount from tblname group by callingparty;
create temporary table temp2 (index callingparty (callingparty)) as select callingparty, count(*) as tencount from tblname where callduration<1000 group by callingparty;
create temporary table temp3 (index callingparty (callingparty)) as select callingparty, count(*) as fourcount from tblname where callduration<492 group by callingparty;
create temporary table join1 (index callingparty (callingparty)) as select temp.callingparty, ifnull(temp3.fourcount, 0) as fourcount, temp.totalcount from temp left outer join temp3 on temp.callingparty=temp3.callingparty;
-- create table concat(short, tblname) (index callingparty (callingparty)) as select join1.callingparty, join1.fourcount; ifnull(temp2.tencount, 0) as tencount, join1.totalcount from join1 left outer join temp2 on join1.callingparty=temp2.callingparty;
set @sql = concat("create table short",tblname," (index callingparty (callingparty)) as select join1.callingparty, join1.fourcount; ifnull(temp2.tencount, 0) as tencount, join1.totalcount from join1 left outer join temp2 on join1.callingparty=temp2.callingparty;");
PREPARE stmt2 FROM @sql;
EXECUTE stmt2;
DEALLOCATE PREPARE stmt2;
drop table temp;
drop table temp2;
drop table temp3;
drop table join1;
end$$
delimiter ;