将row行插入表的mySQL存储过程总是为所有列插入0值

时间:2015-12-14 15:20:22

标签: mysql stored-procedures parameters insert

我正在尝试在mySQL上进行插入存储过程作为一个小测试。 最终结果将所有零插入一行而不是传递给定义器的值。

我的代码:

CREATE PROCEDURE `Test` (
IN IDvar int,
IN FirstNamevar nvarchar(50),
IN LastNamevar nvarchar(50),
IN Nationalityvar nvarchar(50),
In Agevar nvarchar(50),
)

BEGIN

INSERT INTO testtable (
ID,
FirstName,
LastName,
Nationality,
Age)

VALUES(
IDvar,
FirstNamevar,
LastNamevar,
Nationalityvar,
Agevar);

END

我单击“应用”并获取以下代码以构建存储过程

USE `testdb`;
DROP procedure IF EXISTS `Test`;

DELIMITER $$
USE `testdb` $$
CREATE PROCEDURE `Test` (
IN IDvar int,
IN FirstNamevar nvarchar(50),
IN LastNamevar nvarchar(50),
IN Nationalityvar nvarchar(50),
In Agevar nvarchar(50),
)

BEGIN

INSERT INTO testtable (
ID,
FirstName,
LastName,
Nationality,
Age)

VALUES(
IDvar,
FirstNamevar,
LastNamevar,
Nationalityvar,
Agevar);

END$$

DELIMITER;

如果我查看构造的商店程序代码,我会得到以下内容

CREATE DEFINER=`root`@localhost` PROCEDURE `Test`(
IN IDvar int,
IN FirstNamevar nvarchar(50),
IN LastNamevar nvarchar(50),
IN Nationalityvar nvarchar(50),
In Agevar nvarchar(50),
)

BEGIN

INSERT INTO testtable (
ID,
FirstName,
LastName,
Nationality,
Age)

VALUES(
IDvar,
FirstNamevar,
LastNamevar,
Nationalityvar,
Agevar);

END

然后我使用值执行此存储过程:

IDvar = 3
FirstNamevar = TestFirstName
LastNamevar = TestLastName
Nationalityvar = GER
Age = 20

我明白了

set @IDvar = 0;
set @FirstNamevar = '0';
set @LastNamevar = '0';
set @Nationalityvar = '0';
set @Agevar = '0';
call testdb.Test(@IDvar, @FirstNamevar, @LastNamevar, @Nationalityvar, @Agevar);
select @IDvar, @FirstNamevar, @LastNamevar, @Nationalityvar, @Agevar;

显然,这一行的所有零值都是一行。任何人都知道为什么会这样吗?

干杯

1 个答案:

答案 0 :(得分:0)

1)您将所有零都传递给存储过程。这就是插入表中的内容。

2)最后的SELECT不会从您在存储过程中插入的表中读取,而只是返回变量的值 - 所有这些都是0.

所以一切都正常,因为你的代码告诉它。