简体   繁体   中英

create a table with a procedure : error 1064

I try to create a table with a procedure but I have an error 1064 at this line: PRIMARY KEY ( Dates )');

this is the code:

DELIMITER $$

CREATE DEFINER=`root`@`localhost` PROCEDURE `new_procedure`(IN tname varchar(20))
BEGIN

SET @s = CONCAT('CREATE TABLE `test`.',tname,'(
  `Dates` DATE NOT NULL,
  `ncl1104` VARCHAR(45) NULL,
  `ncl1204` VARCHAR(45) NULL,
  PRIMARY KEY (`Dates`)');

PREPARE stm FROM @s;
EXECUTE stm;
END

to call the procedure:

CALL new_procedure ('`new_oil_table`');

Thanks for your help.

You are missing ) at the end. I have tried the same but in SQL Server though. See below

CREATE PROCEDURE new_procedure
@tname varchar(20)
as
BEGIN
declare @s varchar(200);
SET @s = 'CREATE TABLE '+@tname+'(
  Dates DATE NOT NULL,
  ncl1104 VARCHAR(45) NULL,
  ncl1204 VARCHAR(45) NULL,
  PRIMARY KEY (Dates))';

select @s
exec (@s);
END

In your code the error is @ below

SET @s = CONCAT('CREATE TABLE `test`.',tname,'(
  `Dates` DATE NOT NULL,
  `ncl1104` VARCHAR(45) NULL,
  `ncl1204` VARCHAR(45) NULL,
  PRIMARY KEY (`Dates`)'); <-- Here ) outside quote

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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