SQL多插入程序

时间:2016-08-31 09:36:42

标签: sql insert mariadb procedure

我整个上午都被困住了,我似乎无法弄清楚这里的问题是什么,一个善良的灵魂可以帮助我:

CREATE PROCEDURE value(
    IN group1 CHAR(20), 
    IN ns1 CHAR(20), 
    IN title1 CHAR(20))   
BEGIN
  insert into translationmarker 
      (site, `group`, ns, title, `type`) 
      values 
      (`cms`, group1, ns1, title1, `value`)
  insert into translation 
      (marker, locale, `value`) 
      select id, 'en', 'test' 
      from translationmarker 
      where `group` = group1 and ns = ns1 and title = title1
  insert into translationjavascript 
      select id 
      from translationmarker 
      where `group` = group1 and ns = ns1 and title = title1
END

错误:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'insert into translation 
      (marker, locale, `value`) 
      selec' at line 10 

谢谢!

2 个答案:

答案 0 :(得分:1)

我认为你正在使用反引号,你想要单引号。此外,您需要确保拥有delimiter语句。也许这会奏效:

DELIMITER $$
CREATE PROCEDURE translationvalue(
    IN in_group1 CHAR(20), 
    IN in_ns1 CHAR(20), 
    IN in_title1 CHAR(20)
)   
BEGIN
  insert into vitanet__translationmarker(site, `group`, ns, title, `type`) 
      values ('cms', in_group1, in_ns1, in_title1, 'value');

  insert into vitanet__translation (marker, locale, `value`) 
      select id, 'en', 'test' 
      from vitanet__translationmarker 
      where `group` = in_group1 and ns = in_ns1 and title = in_title1;

  insert into vitanet__translationjavascript(id)
      select id 
      from vitanet__translationmarker 
      where `group` = in_group1 and ns = in_ns1 and title = in_title1;
END$$
DELIMITER ;

注意:

  • 我将参数名称更改为in_前缀。这有助于将它们与表格中的列区分开来。
  • 在第一个values()语句中,我用单引号替换了反引号。
  • 我将delimiter语句和分号添加到行尾。
  • 命名列group是一个非常糟糕的主意,因为这是一个SQL关键字和一个MySQL保留字。
  • 我已将id列明确添加到最后一个insert。即使有一列,我认为列出列的名称仍然是最佳做法(我猜它被称为id)。

答案 1 :(得分:0)

是否可以在每个;块之后添加查询终止符INSERT

CREATE PROCEDURE translationvalue(
    IN group1 CHAR(20), 
    IN ns1 CHAR(20), 
    IN title1 CHAR(20))   
BEGIN
  insert into vitanet__translationmarker 
      (site, `group`, ns, title, `type`) 
      values 
      (`cms`, group1, ns1, title1, `value`);

  insert into vitanet__translation 
      (marker, locale, `value`) 
      select id, 'en', 'test' 
      from vitanet__translationmarker 
      where `group` = group1 and ns = ns1 and title = title1;

  insert into vitanet__translationjavascript 
      select id 
      from vitanet__translationmarker 
      where `group` = group1 and ns = ns1 and title = title1;

END