【问题标题】:How to use execute statement in mysql while using dynamic where clause如何在使用动态where子句时在mysql中使用execute语句
【发布时间】:2012-02-16 18:37:26
【问题描述】:
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 - 为 EXECUTE 提供了未知的准备好的语句处理程序(finalquery)

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

【问题讨论】:

标签: asp.net mysql


【解决方案1】:

试试这个链接http://docs.oracle.com/javase/tutorial/jdbc/basics/storedprocedures.html,它可能对你有帮助, 开始

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】:

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

    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
    

    【讨论】:

      【解决方案3】:

      感谢您的帮助,我已经尝试过了,只需稍作修改即可工作。以下是我的存储过程:

      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
      

      【讨论】:

      • 不要忘记在系统允许时将此标记为已接受的答案,或者将其编辑到您的问题中并接受对您帮助最大的答案。
      • 并指出您所做的更改。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-14
      • 2011-09-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多