如何在使用dynamic where子句时在mysql中使用execute语句

时间:2012-01-23 11:53:58

标签: asp.net mysql

Below is my store procedure .please help........



BEGIN



DECLARE selectQuery VARCHAR(2000);
declare finalquery varchar(2000);
declare stmt3 varchar(2000);

SET selectQuery = 'SELECT tbl_property.intId, strAddressLine1,(select strItemName from tbl_lk_item where intId=tbl_property.intPropertyCountyTypeId) as strCountyName ,(select strItemName from tbl_lk_item where intId=tbl_property.intPropertyCountryTypeId) as strCountryName ,strpostCode,(tbl_pro_adver_matchcriteria.floatAskingPrice),tbl_pro_adver_matchcriteria.intBedrooms
FROM tbl_property LEFT OUTER JOIN tbl_pro_adver_matchcriteria on tbl_property.intId = tbl_pro_adver_matchcriteria.intPro
set finalquery =CONCAT(selectQuery,strSqlQuery,' AND tbl_property.intId=1 ');  

execute finalquery;

END

当我运行存储过程并传递参数'where tbl_property.intId = 1'时,它给出了Procedure执行失败 1243 - 给予EXECUTE

的未知预处理语句处理程序(finalquery)

我通过select语句检查查询结果,它给出了正确的查询并返回结果。所以请帮我使用Execute语句。

3 个答案:

答案 0 :(得分:2)

感谢您的帮助,我已经尝试过了,只需要很少的修改就可以了。我的商店程序是:

BEGIN
DECLARE selectQuery VARCHAR(2000);
declare finalquery varchar(2000);

SET selectQuery = 'SELECT tbl_property.intId, strAddressLine1,(select strItemName from tbl_lk_item where intId=tbl_property.intPropertyCountyTypeId) as strCountyName ,(select strItemName from tbl_lk_item where intId=tbl_property.intPropertyCountryTypeId) as strCountryName ,strpostCode,(tbl_pro_adver_matchcriteria.floatAskingPrice),tbl_pro_adver_matchcriteria.intBedrooms
FROM tbl_property LEFT OUTER JOIN tbl_pro_adver_matchcriteria on tbl_property.intId = tbl_pro_adver_matchcriteria.intProId ';

set @finalquery =CONCAT(selectQuery,strSqlQuery,' AND tbl_property.intId=1 ');  

PREPARE result from @finalquery;
EXECUTE result;
DEALLOCATE PREPARE result;

END

答案 1 :(得分:0)

试试此链接http://docs.oracle.com/javase/tutorial/jdbc/basics/storedprocedures.html,它可能对您有帮助, BEGIN

DECLARE selectQuery VARCHAR(2000);
declare finalquery varchar(2000);
declare stmt3 varchar(2000);

SET selectQuery = 'SELECT tbl_property.intId, strAddressLine1,(select strItemName from tbl_lk_item where intId=tbl_property.intPropertyCountyTypeId) as strCountyName ,(select strItemName from tbl_lk_item where intId=tbl_property.intPropertyCountryTypeId) as strCountryName ,strpostCode,(tbl_pro_adver_matchcriteria.floatAskingPrice),tbl_pro_adver_matchcriteria.intBedrooms
FROM tbl_property LEFT OUTER JOIN tbl_pro_adver_matchcriteria on tbl_property.intId = tbl_pro_adver_matchcriteria.intProId ';

set finalquery =CONCAT(selectQuery,strSqlQuery);  


EXECUTE finalquery;

答案 2 :(得分:0)

动态声明必须在使用后准备和解除分配。这是一个例子。

BEGIN

SET @selectQuery = 'SELECT * from table1 where'

SET @QUERY = CONCAT(@selectQuery, ' field = 1');            
SELECT @QUERY;
PREPARE s FROM @QUERY;

EXECUTE s;
DEALLOCATE PREPARE s;

END