首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在使用动态where子句的同时使用mysql中的execute语句

如何在使用动态where子句的同时使用mysql中的execute语句
EN

Stack Overflow用户
提问于 2012-01-23 19:53:58
回答 3查看 1.1K关注 0票数 1
代码语言:javascript
复制
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‘时,它给出了过程执行失败1243 -给定要执行的未知预准备语句处理程序(finalquery)

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

EN

回答 3

Stack Overflow用户

发布于 2012-01-24 13:38:17

谢谢你的帮助,我已经尝试过了,它和小modifications..below一起工作是我的存储过程:

代码语言:javascript
复制
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
票数 2
EN

Stack Overflow用户

发布于 2012-01-23 20:24:04

尝试此链接http://docs.oracle.com/javase/tutorial/jdbc/basics/storedprocedures.html,它可能会帮助您,开始

代码语言:javascript
复制
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;
票数 0
EN

Stack Overflow用户

发布于 2012-01-23 20:51:48

动态语句必须在使用后进行准备和释放。下面是一个例子。

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8971184

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档