Subquery returns more than one row in mysql
Can any one tell what's going wrong with my routine this is what I have written
Routine DDL:
DELIMITER $$
CREATE DEFINER=`root`@`%` PROCEDURE `uspEmployeeBankDataUpdate`(_EmpID int,
_PayeeName varchar(20),
_BankRoutingNumber varchar(9),
_BankTypeID varchar(5),
_AccountType varchar(2),
_BankAccount int,
_Amount DOUBLE,
_Comments varchar(50),
_Address1 varchar(30),
_Address2 varchar(30),
_ZipCode DECIMAL(9,0),
_City varchar(25),
_StateName VARCHAR(30),
_Country varchar(20),
_BankAccountType varchar(30),
_EndDate datetime)
BEGIN
declare p_ecount int;
declare _startdate Date;
set _startdate=(select date(startdate) from tblEmployeeBankData where
EmpId=_EmpId and
EndDate='9999-12-31');
set p_ecount=(select count(1) from tblEmployeeBankData where
PayeeName=_PayeeName and
BankRoutingNumber=_BankRoutingNumber and
BankTypeID=_BankTypeID and
AccountType=_AccountType and
BankAccount=_BankAccount and
Amount=_Amount and
Comments=_Comments and
Address1=_Address1 and
Address2=_Address2 and
ZipCode=_ZipCode and
City=_City and
StateName=_StateName and
Country=_Country and
BankAccountType=_BankAccountType and
EndDate='9999-12-31');
if p_ecount=0 and _startdate<curdate() then
begin
update tblEmployeeBankData set EndDate=_EndDate
where EmpID=_EmpID and EndDate="9999-12-31";
end;
end if;
END
This is my sample code of passing
m_oCmd.Parameters.AddWithValue("_EmpID", EmpID);
m_oCmd.Parameters.AddWithValue("_PayeeName", PayeeName);
m_oCmd.Parameters.AddWithValue("_BankTypeID", BankTypeID);
m_oCmd.Parameters.AddWithValue("_AccountType", AccountType);
m_oCmd.Parameters.AddWithValue("_BankRoutingNumber", BankRoutingNumber);
m_oCmd.Parameters.AddWithValue("_BankAccountType", BankAccountType);
m_oCmd.Parameters.AddWithValue("_BankAccount", BankAccount);
m_oCmd.Parameters.AddWithValue("_Amount", Amount);
m_oCmd.Parameters.AddWithValue("_Comments", Comments);
m_oCmd.Parameters.AddWithValue("_Address1", Address1);
m_oCmd.Parameters.AddWithValue("_Address2", Address2);
开发者_运维百科 m_oCmd.Parameters.AddWithValue("_ZipCode", ZipCode);
m_oCmd.Parameters.AddWithValue("_City", City);
m_oCmd.Parameters.AddWithValue("_StateName", StateName);
m_oCmd.Parameters.AddWithValue("_Country", Country);
m_oCmd.Parameters.AddWithValue("_EndDate", EndDate);
The error is here
set _startdate=(select date(startdate) from tblEmployeeBankData where
EmpId=_EmpId and
EndDate='9999-12-31');
If the condition EmpId=_EmpId and EndDate='9999-12-31'
results in more than one record, it fails. Use LIMIT and ORDER BY to choose one particular record (startdate), e.g.
set _startdate=(select date(startdate) from tblEmployeeBankData where
EmpId=_EmpId and
EndDate='9999-12-31'
ORDER BY startdate DESC
LIMIT 1);
精彩评论