SQL 多重插入过程
SQL multiple insert 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
谢谢!
我认为您使用的是反引号,而您打算在其中使用单引号。此外,您需要确保您有一个 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
)。
您能否在每个 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
一上午都卡住了,我似乎无法弄清楚这里的问题是什么,好心人可以帮助我吗:
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
谢谢!
我认为您使用的是反引号,而您打算在其中使用单引号。此外,您需要确保您有一个 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
)。
您能否在每个 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