2017-08-21 42 views
0

我想創建一個過程,允許我輸入表的名稱作爲變量(tblname),然後在該表上執行一些操作。我在函數中引用的調用方法和調用方法的列是任何將使用的表的列。我是創建程序的新手,每次修復出現另一個錯誤時都會彈出。在MySQL中創建用戶定義的過程

目前我收到的錯誤:

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 ; 
+1

我不確定,但我認爲您必須使用準備好的語句。搜索「mysql動態表名」,你已經有很多答案了。 – rlanvin

+0

@rlanvin好的,我會研究一下!謝謝 – Caitlin

+0

我看到的一件事就是'concat'是mysql中的一個保留字,所以你要麼改變它,要麼使用反斜槓。 –

回答

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 ;