Home >Database >Mysql Tutorial >mysql写存储过程有关问题

mysql写存储过程有关问题

WBOY
WBOYOriginal
2016-06-07 16:23:431505browse

mysql写存储过程问题 我想调用mysql存储过程建表,表名通过in参数来传进。 CREATE PROCEDURE friendList (in tname varchar(20)) BEGIN SET @sqlstr =CONCAT('CREATE TABLE if not exists ', tname, '( userid int unsigned, phone VARCHAR(20) NOT NULL, nam

mysql写存储过程问题
我想调用mysql存储过程建表,表名通过in参数来传进。
CREATE PROCEDURE friendList (in tname varchar(20))
BEGIN
   SET @sqlstr =CONCAT('CREATE TABLE if not exists ',
   tname,
         '(
         userid int unsigned,
         phone VARCHAR(20) NOT NULL,
  name VARCHAR(30) NOT NULL,
  birth int,
  nick VARCHAR(15),
  impression varchar(50)
  )ENGINE=InnoDB  DEFAULT CHARSET=utf8') ;
  prepare stmt from @sqlstr;
  execute stmt;
END;

在windows 的cmd下执行一直报错。 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
原来,在cmd下执行的mysql,其;相当于commit,所以会一直报这个错。
应更改语句结束符号,本实例将语句结束符更改为“//”。

于是:
delimiter //
CREATE PROCEDURE friendList (in tname varchar(20))
BEGIN
   SET @sqlstr =CONCAT('CREATE TABLE if not exists ',
   tname,
         '(
         userid int unsigned,
         phone VARCHAR(20) NOT NULL,
  name VARCHAR(30) NOT NULL,
  birth int,
  nick VARCHAR(15),
  impression varchar(50)
  )ENGINE=InnoDB  DEFAULT CHARSET=utf8') ;
  prepare stmt from @sqlstr;
  execute stmt;
END;
//


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn