2017-08-21 5 views
0

テーブルの名前を変数(tblname)として入力し、そのテーブルに対していくつかの操作を実行できるプロシージャを作成しようとしています。この関数で参照しているcallpartyとcalldurationの列は、使用されるすべての表の既存の列です。私は手続きを作成することに新しいし、別のポップアップエラーを修正するたびに。 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 

と私はこの1つまたは何私の構文が間違っている問題を解決するかどうかはわかりません。それは古いテーブルの名前を含む新しいテーブルを作成することはできないということだけですか?

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

私の目に来ることの1つは、 '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 ; 
関連する問題