存储过程搜索具有空值

时间:2015-05-14 10:33:24

标签: mysql

我对supplier_search的查询是..

CREATE DEFINER=`root`@`localhost` PROCEDURE `supplier_Search`(in strLedgerName varchar(255),in strAddress varchar(255),
       in inPhoneNo int(45),  in inMobNo int(45), in strPriceLevel varchar(255),in strCountry varchar(255),in strState varchar(255))
BEGIN
    if inPhoneNo = '' then SET inPhoneNo =Null ;end if;
    if inMobNo = '' then SET inMobNo =Null ;end if;
    if strLedgerName ='' then SET strLedgerName = Null; end if;      
    if  strAddress ='' then set strAddress = null; end if;    
    if  strPriceLevel = '' then set strPriceLevel = null; end if;
    if strCountry = '' then set strCountry = null; end if;
    if strState = '' then set strState = null; end if;
    select ledgerName,address, phoneNo , mobNo ,priceLevel,stateName, CountryName from
    (
        select joined_ab.ledgerName,joined_ab.address ,joined_ab.phoneNo, joined_ab.mobNo ,joined_ab.priceLevel,c.countryName,joined_ab.stateId
        from (select  a.ledgerName, a.address , a.phoneNo , a.mobNo ,b.priceLevel,
        a.countryId,a.stateId from tbl_ledger as a inner join tbl_price_level as b on  a.pricingLevelId =b.priceLevelId)
        as joined_ab inner join tbl_country as c on joined_ab.countryId = c.countryId
    ) as joined_abc inner join tbl_state 
    as d on joined_abc.stateId = d.stateId
    where((strLedgerName is null or joined_abc.ledgerName LIKE concat(strLedgerName,"%"))
    and(strAddress is null or address LIKE concat(strAddress ,"%"))
    and(inPhoneNo is Null or phoneNo lIke concat(inPhoneNo , "%"))
    and (strPriceLevel is null or priceLevel Like concat(strPriceLevel,"%"))      
    and(inMobNo is Null or mobNo Like concat(inMobNo , "%")) 
    and(strCountry is null or CountryName LikE concat(strCountry,"%"))
    and(strState is null or StateName LikE concat(strState,"%")));
END

我希望在传递一个或多个值时获得输出。 但问题是当我没有为mobileNo或phoneNo传递值并执行错误时

call db_account.supplier_Search('1', '', '', '', '', '', '')    
Error Code: 1366. Incorrect integer value: '' for column 'inPhoneNo' at row 1   0.000 sec

1 个答案:

答案 0 :(得分:1)

问题是由于您将整数值传递为''。

SQL不知道哪种类型的整数;尝试设置0或NULL。

环境建议你问题是:你正在使用''作为整数值,SQL不知道哪种类型的整数;尝试设置0或NULL。如果问题不在调用操作中,则必须更改过程正文中数据的比较方式:

  

如果inPhoneNo =''然后SET inPhoneNo = Null;结束if;