Skip to content

Instantly share code, notes, and snippets.

@KimiyukiYamauchi
Last active November 5, 2015 08:42
Show Gist options
  • Save KimiyukiYamauchi/b68bfae07f88e2845e5f to your computer and use it in GitHub Desktop.
Save KimiyukiYamauchi/b68bfae07f88e2845e5f to your computer and use it in GitHub Desktop.
引数で指定した名前でtb1テーブルのコピーを作成するストアードプロシージャ
drop procedure if exists tb1X;
delimiter //
create procedure tb1X(in x varchar(100))
begin
set @s = concat('drop table if exists `', x ,'`');
prepare stmt from @s;
execute stmt;
deallocate prepare stmt;
set @s = concat('create table `', x ,'` select * from tb1');
prepare stmt from @s;
execute stmt;
deallocate prepare stmt;
end
//
delimiter ;
@KimiyukiYamauchi
Copy link
Author

使い方は以下
mysql> call tb1X('tb1A');

=> これで、「tb1」テーブルをコピーした「tb1A」テーブルが作成される

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment