在MySQL存储例程中创建动态WHERE子句

时间:2013-06-20 09:45:14

标签: mysql

任何有关此问题的帮助都会很好,因为它让我发疯。 我的MYSQL数据库中的表包含monetry硬币的详细信息 虽然那里似乎有很多动态的WHERE子句, 我已经搜索了几十个似乎没有人满足变量 丢失。

TABLE coin (
  coinID smallint(6) NOT NULL AUTO_INCREMENT,
  coinCountry smallint(6) NOT NULL,
  coinDenom smallint(6) NOT NULL,
  coinMinted smallint(6) NOT NULL,
  coinImageLocation varchar(128) NOT NULL,
  coinPgeTitle varchar(45) NOT NULL,
  PRIMARY KEY (coinID)
) 

我希望在存储例程中有一个动态'WHERE'子句,它接受三个参数 其中一个参数是强制性的,并且总是有一个值,另外两个是自由选择的 并且要么具有值,要么

我已经编写了这个存储的例程,并且潜伏在某个地方

DELIMITER $$


CREATE DEFINER=`root`@`localhost` PROCEDURE `procFilterCoins`
(

/*******************************************************

Proc     :  Filter the coins returned by adjusting the WHERE Clause.

IN   :  iCountry (The CountryID) a mandatory value

IN   :  iDenom (The DenominationID) a discretionary value

IN   :  iMinted (The year minted) a discretionary value

OUT      :  filtered coins recordset

********************************************************/

IN iCountry int(11), 

IN iDenom int(11), 

IN iMinted int(11)
)

BEGIN

/* somewhere to hold our where clause*/

DECLARE strWhere varchar(100);


set strWhere = '((coinCountry = ';

IF iDenom > 0 and iDenom IS NOT NULL THEN

    set strWhere = concat(strWhere, iCountry, ") AND (coinDenom = ", iDenom, ')');

ELSE

    set strWhere = concat(strWhere, iCountry, ')');

END IF;


IF iMinted > 0 and iMinted IS NOT NULL THEN

    set strWhere = concat(strWhere, " AND (coinMinted = ", iMinted, '))');

ELSE

    set strWhere = concat(strWhere, ')');
END IF;


/*Extract the records via prepared view*/
SELECT * FROM vAllcoins WHERE + strwhere;


END

我使用 MySQL Workbench 5.2.46.CE

测试了以下数据

调用procfiltercoins(18,null,1948); 产生...... WHERE((coinCountry = 18)AND(coinMinted = 1948)); 没有记录返回

调用procfiltercoins(103,4,1948,@ out); 产生......

WHERE ((coinCountry = 103) AND (coinDenom = 4) AND (coinMinted = 1948));

没有记录返回

调用procfiltercoins(18,2,null,@ out); 产生......

WHERE ((coinCountry = 18) AND (coinDenom = 2));

没有记录返回

我已经在Access数据库中复制了所有这些,没有任何问题

2 个答案:

答案 0 :(得分:4)

尝试使用prepared statement

SET @query = CONCAT("SELECT * FROM vAllcoins WHERE ",strwhere);

PREPARE stmt FROM @query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

不确定要对查询结果执行什么操作。把它放在视图中可能吗?

SET @query = CONCAT("CREATE VIEW myView as SELECT * FROM vAllcoins WHERE ",strwhere);

PREPARE stmt FROM @query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
祝你好运!

答案 1 :(得分:-1)

我已经重新调整了存储的例程,并提出了以下有效的方法。

CREATE DEFINER=root@localhost PROCEDURE procOtherFilter( /****************************** Proc : Filter the coins returned by adjusting the WHERE Clause. IN : iCountry (The CountryID) a mandatory value IN : iDenom (The DenominationID) a discretionary value IN : iMinted (The year minted) a discretionary value OUT : filtered coins recordset *******************************/ IN iCountry int(11), IN iDenom int(11), IN iMinted int(11) ) BEGIN /* If we only have the countryID it will always be there as it's mandatory */ IF (iDenom IS NULL) AND (iMinted IS NULL) THEN SELECT * FROM vAllcoins WHERE (coinCountry = iCountry); /* If the denomination is missing use the minted date */ ELSEIF iDenom IS NULL THEN SELECT * FROM vAllcoins WHERE (coinCountry = iCountry) AND (coinMinted = iMinted); /* If the minted date is missing use the denomination */ ELSEIF iMinted IS NULL THEN SELECT * FROM vAllcoins WHERE (coinCountry = iCountry) AND (coinDenom = iDenom);

root

localhost

感谢所有观看过的人