存储过程错误concat()

时间:2014-05-19 11:53:41

标签: mysql sql

创建存储过程时出错......

DELIMITER $$

CREATE DEFINER=`root`@`localhost` PROCEDURE `CreateTableProj`(IN projName VARCHAR(200), IN tablName VARCHAR(200))
BEGIN

 SET @comnd = CONCAT('
 create table ',tablName,'(
                  select s1.name , p1.fpath , s1.version 
                  FROM test.prj1 s1 , test.prj2 p1
                  WHERE 
                        s1.id=(select id 
                                from test.prj2 p2 
                            where p2.name=',projName') 
                        and s1.id=p1.id
                  ');

 PREPARE stmt FROM @comnd;
 EXECUTE stmt;

END

获得以下错误

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 'DELIMITER $$

知道如何解决此错误吗?

2 个答案:

答案 0 :(得分:1)

您的查询有多个错误和错误的编码示例:

  • 创建表后需要as
  • 您需要在名称周围加双引号(除非名称是数字)。
  • 这也修复了join语法。

以下是我认为你想要的:

create table ', tablName, ' as
     select s1.name , p1.fpath , s1.version 
     from test.prj1 s1 join
          test.prj2 p1
          on s1.id = p1.id
     where s1.id = (select id
                    from test.prj2 p2 
                    where p2.name = ''', projName'''
                   )

这仍然没有意义。子查询坚持只返回一行。所以这个查询应该与:

相同
create table ', tablName, ' as
     select s1.name , p1.fpath , s1.version 
     from test.prj1 s1 join
          test.prj2 p1
          on s1.id = p1.id
     where p1.name = ''', projName'''

答案 1 :(得分:0)

DELIMITER $$是错误的snytax,它似乎没有在代码中的任何地方使用.Tr删除它并运行脚本。