简体   繁体   English

子查询在mysql中返回多个行

[英]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: 常规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. 如果条件EmpId=_EmpId and EndDate='9999-12-31'导致多个记录,则失败。 Use LIMIT and ORDER BY to choose one particular record (startdate), eg 使用LIMIT和ORDER BY选择一个特定的记录(开始日期),例如

set _startdate=(select date(startdate) from tblEmployeeBankData  where
           EmpId=_EmpId and
           EndDate='9999-12-31'
           ORDER BY startdate DESC
           LIMIT 1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM