我试图在我的MYSQL数据库中向Back Order表添加新的Back Order。我遇到的问题是,当我尝试选择Supplier_ID将其添加到延期订单表时,程序会给出一个错误,表示"子查询返回的值超过1"。
供应商表(不确定如何正确构建它)
| Supplier_ID | SupName |
| 1 | EWS |
| 6 |富兰克林面包店|
延期订单表
| BackOr_ID | Supplier_ID | BackOr_Org_Date | Sup_Name |
我目前使用的MYSQL查询是:
CREATE DEFINER=`root`@`%` PROCEDURE `sp_BackOrder`(
in supname varchar(50),
in dat date
)
BEGIN
insert into Back_Order
(
Supplier_ID,
BackOr_Org_Date,
Sup_Name
)
values
(
(select Supplier_ID from Supplier Where SupName = supname),
dat,
supname
);
END

使用该查询的C#代码是:
MySqlCommand cmd = new MySqlCommand("sp_BackOrder", new MySqlConnection(*Connection String*));
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new MySqlParameter("supname", Supp_Name)); //Supp_Name is the Supplier Name (a string variable)
cmd.Parameters.Add(new MySqlParameter("dat", DateTime.Now.ToShortDateString())); //dat is the date the Order is placed
cmd.Connection.Open();
cmd.ExecuteNonQuery();
cmd.Connection.Close();

我得到的唯一错误是Subquery(我假设是Select Query)返回的值超过1。我试过环顾四周,甚至看过Stackoverflow,但无法找到解决问题的方法。任何帮助将不胜感激。
答案 0 :(得分:1)
需要看起来像:
delimiter $$
CREATE DEFINER=`root`@`%` PROCEDURE `sp_BackOrder`
( in p_supname varchar(50),
in p_dat date
)
BEGIN
insert into Back_Order(Supplier_ID, BackOr_Org_Date, Sup_Name)
select Supplier_ID, p_dat, p_supname from Supplier Where SupName = p_supname;
END$$
delimiter ;
或
CREATE DEFINER=`root`@`%` PROCEDURE `sp_BackOrder`
( in p_supname varchar(50),
in p_dat date
)
BEGIN
insert into Back_Order(Supplier_ID, BackOr_Org_Date, Sup_Name)
select Supplier_ID, p_dat, p_supname from Supplier Where SupName = p_supname;
END
根据您的DELIMITER
需求,两者都相同。
答案 1 :(得分:0)
过程和列名supname
的参数名SupName
相同。将参数名称更改为列名以外的名称。
CREATE DEFINER=`root`@`%` PROCEDURE `sp_BackOrder`(
in selectedSupname varchar(50),
in dat date
)
BEGIN
insert into Back_Order
(
Supplier_ID,
BackOr_Org_Date,
Sup_Name
)
values
(
(select Supplier_ID from Supplier Where SupName = selectedSupname),
dat,
selectedSupname
);
END