简体   繁体   English

创建表的存储过程

[英]Stored Procedures that creates a table

Can I create tables using Stored Procedures?我可以使用存储过程创建表吗?

I am using MySQL.我正在使用 MySQL。 I had searched the web for instructions on how to create a table using stored procedure, but I didn't find any results, I just found that you can create temporary tables.我在网上搜索了有关如何使用存储过程创建表的说明,但没有找到任何结果,我只是发现您可以创建临时表。 Is there any problem if I create tables using stored procedures?如果我使用存储过程创建表有什么问题吗?

Actually I want to check whether a table exists or not, then I have to create it.其实我想检查一个表是否存在,然后我必须创建它。

You can create tables using procedures in mysql.您可以使用 mysql 中的过程创建表。

delimiter |
CREATE PROCEDURE SP_CREATE_TABLE_TEST ()
   BEGIN
      CREATE TABLE TEST 
      (
        TestID int(11) default NULL,
        TestName varchar(100) default NULL
      ) 
      ENGINE=InnoDB DEFAULT CHARSET=utf8;
   END;

|

Reference link. 参考链接。


EDIT: Thanks Peter!编辑:谢谢彼得! So the solution to this particular problem:所以这个特定问题的解决方案:

DELIMITER //
CREATE PROCEDURE createTable(IN tableName VARCHAR(40))
BEGIN
SET @table := tableName;
SET @sql_text:=CONCAT('CREATE TABLE ',@table,'(id INT NOT NULL AUTO_INCREMENT, FILENAME VARCHAR(40) NOT NULL, DATA BLOB NOT NULL, PRIMARY KEY (ID))');

PREPARE stmt from @sql_text;
EXECUTE stmt;
END //
DELIMITER ;

In addition to the manual I also found a very helpful tut by Roland Bouman at:除了手册之外,我还找到了 Roland Bouman 的一个非常有用的 tut:

http://rpbouman.blogspot.com/2005/11/mysql-5-prepared-statement-syntax-and.html enter code here http://rpbouman.blogspot.com/2005/11/mysql-5-prepared-statement-syntax-and.html enter code here

El autor de la respuesta: Conrad Ljungström El autor de la respuesta: Conrad Ljungström

link de la respuesta original: https://forums.mysql.com/read.php?98,495352回复原文链接: https ://forums.mysql.com/read.php ? 98,495352

You can create a table in a stored procedure without a problem.您可以毫无问题地在存储过程中创建表。 Example:例子:

delimiter |

create procedure createtab () 
begin 
    create table a(id int);
    insert into a select 1;
    select * from a;
    drop table a;
end
|

delimiter ;

Something like this should work.像这样的事情应该有效。

delimiter |
CREATE PROCEDURE SP_CREATE_TABLE( )
BEGIN
  CREATE TABLE employees (emp_id int, emp_name varchar(100));
END;
|

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM