这是我的存储过程,在我的本地机器上工作正常,但在PRODUCTION上,它给我上面的错误:它抱怨OUT参数,最后一个:
-- --------------------------------------------------------------------------------
-- Routine DDL
-- Note: comments before and after the routine body will not be stored by the server
-- --------------------------------------------------------------------------------
DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `insert_projects`(
IN userName varchar(20),
IN projectName text,
IN projectDescription text,
IN projectCurrency text,
IN projectBudget1 double,
IN projectBudget2 double,
IN projectEndDate DATETIME,
IN expectedDelivDate DATETIME,
IN projectMessageForBidder text,
OUT projectCode bigint)
BEGIN
DECLARE
l_project_code bigint;
insert into projects
(PROJECT_CODE,
SR_USER_NAME ,
PROJECT_NAME,
PROJECT_DESCRIPTION,
PROJECT_CURRENCY,
PROJECT_BUDGET1 ,
PROJECT_BUDGET2 ,
PROJECT_STATUS,
PROJECT_END_DATE,
PROJECT_PAID,
EXPECTED_DELIV_DATE,
PROJECT_MESSAGE_FOR_BIDDER,
PROJECT_CREATION_DATE,
PROJECT_UPDATE_DATE)
values
(
0,
userName,
projectName,
projectDescription,
projectCurrency,
projectBudget1,
projectBudget2,
'Active',
projectEndDate,
null,
expectedDelivDate,
projectMessageForBidder,
NOW(),
NOW());
SELECT LAST_INSERT_ID() into l_project_code;
SET projectCode = l_project_code;
END
下面是我的C#代码,用于设置参数:
MySqlCommand command = new MySqlCommand("insert_projects", dbcontroller.conn);
command.CommandType = System.Data.CommandType.StoredProcedure;
// Add parameters for the insert_projects STORED PROC
command.Parameters.Add(new MySqlParameter("userName", SessionBag.Current.UserName));
command.Parameters["@userName"].Direction = System.Data.ParameterDirection.Input;
command.Parameters.Add(new MySqlParameter("projectCode", MySqlDbType.Int64));
command.Parameters["@projectCode"].Direction = System.Data.ParameterDirection.Output;
command.Parameters.Add(new MySqlParameter("projectName", model.projectName));
command.Parameters.Add(new MySqlParameter("projectDescription", model.projectDescription));
command.Parameters.Add(new MySqlParameter("projectCurrency", model.projectCurrency));
command.Parameters.Add(new MySqlParameter("projectBudget1", model.projectBudget1));
command.Parameters.Add(new MySqlParameter("projectBudget2", model.projectBudget2));
command.Parameters.Add(new MySqlParameter("projectEndDate", model.projectEndDate));
command.Parameters.Add(new MySqlParameter("expectedDelivDate", model.expectedDelivDate));
command.Parameters.Add(new MySqlParameter("projectMessageForBidder", model.projectMessageForBidder));
try
{
command.ExecuteNonQuery();
TempData["projectCode"] = (Int64)command.Parameters["?projectCode"].Value;
TempData["frompage"] = "AddProject";
dbcontroller.conn.Close();
return RedirectToAction("MyProjectsHeaderSR", "Projects");
}
为什么抱怨这个?我的本地机器工作正常。表“projects”包含一个名为project_code的字段,该字段设置为BIGINT自动递增...其余字段是普通数据类型。
好的,我创建了一个变量..并将其传递给..同样的错误
if (dbcontroller.DBConnection())
{
Int64 projectCode = 0;
MySqlCommand command = new MySqlCommand("insert_projects", dbcontroller.conn);
command.CommandType = System.Data.CommandType.StoredProcedure;
// Add parameters for the insert_projects STORED PROC
command.Parameters.Add(new MySqlParameter("userName", SessionBag.Current.UserName));
command.Parameters["@userName"].Direction = System.Data.ParameterDirection.Input;
command.Parameters.Add(new MySqlParameter("projectCode", projectCode));
command.Parameters["@projectCode"].Direction = System.Data.ParameterDirection.Output;
它仍然无效
答案 0 :(得分:0)
您描述的错误是旧版MySQL resolved in MySQL 5.5.3中的错误导致的结果。您的本地计算机可能正在运行已修复错误的MySQL版本,并且生产服务器正在运行旧版本。
虽然最佳解决方案可能是升级服务器,但在此之前作为解决方法,您可以更改过程,只需选择LAST_INSERT_ID()
(不是变量,而是变为结果集),以及导致变量的过程。
顺便提一下,使用INOUT
参数时可能会出现类似情况。虽然您使用OUT
参数(因此以下内容不适用于您的情况),但是当INOUT
参数但未提供填充变量时会出现这种情况,以下内容同样适用。
该行:
command.Parameters.Add(new MySqlParameter("projectCode", MySqlDbType.Int64));
...为命令对象提供了一个命名参数和一个类型,但没有给它填充任何东西。如果您使用INOUT
参数,则需要提供一个空变量并将其传入;它会给SP输出一些东西。