Mysql使用动态数据库名创建表 [英] Mysql Create Table with dynamic database name

查看:476
本文介绍了Mysql使用动态数据库名创建表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在运行时创建数据库,我想在该数据库中同时创建表。任何人都可以给我任何想法如何做到这一点?
例如 -



我创建了一个名为'mydb'
的数据库,现在在同一个进程中我试图创建表使用mysql存储过程相同。
我的proc输入将是我的dbname。所以,我的proc看起来像

  create procedure test(IN dbname varchar(100))
begin
创建表`dbname`.`testing`(testid int,testname varchar(45));
end


解决方案

href =http://dev.mysql.com/doc/refman/5.1/en/sql-syntax-prepared-statements.html =nofollow noreferrer> PREPARE功能执行动态SQL。 / p>

I am creating database at runtime and I want to create the tables in that database at the same time. Can anyone give me any thought on how to do that? For Example -

I have created one database named 'mydb' and now in the same process I am trying to create the table I am using the mysql stored procedure for the same. My proc input will be my dbname. So, my proc looks like

create procedure test(IN dbname varchar(100))
begin 
    create table `dbname`.`testing`(testid int, testname varchar(45));
end

解决方案

You can use the PREPARE feature to execute dynamic SQL.

这篇关于Mysql使用动态数据库名创建表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆