Tan078
2017-09-14 06:52
采纳率: 100%
浏览 1.7k

mysql中使用带输入参数的存储过程建表

DECLARE table_name VARCHAR(50);
DECLARE table_pre VARCHAR(20);
DECLARE table_mid VARCHAR(10);
DECLARE table_end VARCHAR(30);
DECLARE sql_text VARCHAR(2000);
DECLARE i INT;
DECLARE j INT;
DECLARE k INT;
SET table_name='';
SET table_pre='tbl_';
SET table_mid='_';
SET table_end='_measurementdata';
SET sql_text='';
SET i=1;
SET j=1;
SET k=1;

SET table_name=CONCAT(table_pre,i,table_mid,j,table_mid,k,table_end);
SET sql_text=CONCAT('CREATE TABLE ', table_name, '(
DataID int(11) NOT NULL AUTO_INCREMENT,
DataValue decimal(10,3) NULL,
MeaFileID int(11) NULL,
NominalID int(11) NULL,
PRIMARY KEY (DataID)

) ' );

SELECT sql_text;
SET @sql_text=sql_text;
PREPARE stmt FROM @sql_text;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

END

各位大神,想把i,j,k换成输入参数,该怎么改呢?今天刚开始学习存储过程,先感谢各位。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • greadworld 2017-09-14 08:17
    已采纳

    create procedure pro_exemple
    (
    in table_pre varchar(20),
    in i int,
    in table_mid varchar(2),
    in j int,
    in k int,
    in table_end varchar(20)
    )
    begin
    set @table_name=CONCAT(table_pre,i,table_mid,j,table_mid,k,table_end);
    set @sql_text=CONCAT('create table ',@table_name,' (
    DataID int(11) NOT NULL AUTO_INCREMENT,
    DataValue decimal(10,3) NULL,
    MeaFileID int(11) NULL,
    NominalID int(11) NULL,
    PRIMARY KEY (DataID))');
    PREPARE stmt FROM @sql_text;
    EXECUTE stmt;
    end
    go
    call pro_exemple ('sad',1,'_',2,3,'asdf');

    点赞 评论

相关推荐 更多相似问题