简体   繁体   English

MySQL存储过程中的语法错误

[英]Syntax Error in MySQL stored procedure

The below SP is not giving any result even though there are 48 rows as per the where clause 即使根据where子句有48行,下面的SP也没有给出任何结果

BEGIN
DECLARE SelectClause VARCHAR(2000);
  if v_mode='SearchByString' then
    SET SelectClause ='select SURVEY_USER.username,SURVEY.* from SURVEY, SURVEY_USER';

if v_SearchString is not null then
    SET SelectClause=CONCAT(@SelectClause,'  where ');
    Set SelectClause=CONCAT(@SelectClause,v_SearchString);
end if;
SET SelectClause=CONCAT(@SelectClause,'  order by SURVEY.created_date DESC;') ;
select SelectClause;
SET @query = SelectClause;
    PREPARE stmt FROM  @query;
    EXECUTE stmt;
 select stmt;
end if;
END

I tried a lot but not getting any problem. 我尝试了很多,但没有遇到任何问题。 I also tried select clause to print the command at various places to not able to print it. 我也试过select子句在各个地方打印命令,无法打印它。 Please give me some solution. 请给我一些解决方案。 There are my parameters that I am passing v_mode='SearhByString' v_SearchString='SURVEY_USER.username=chiragfanse' 我传递的参数是v_mode ='SearhByString'v_SearchString ='SURVEY_USER.username = chiragfanse'

It should return 48 rows but does not return anything. 它应返回48行但不返回任何内容。

BEGIN

  DECLARE SelectClause VARCHAR(2000);

  IF v_mode = 'SearchString' THEN

    SET SelectClause = CONCAT('select SURVEY_USER.username,SURVEY.* from SURVEY, SURVEY_USER');

    IF SearchString IS NOT NULL THEN
      SET SelectClause = CONCAT(SelectClause, ' where ', SearchString);
    END IF;

    SET SelectClause = CONCAT(SelectClause, '  order by SURVEY.created_date DESC;');

    SET @query = SelectClause;
    PREPARE stmt FROM  @query;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;

  END IF;
END
  1. All declaration have to be at the begining. 所有声明都必须在开头。
  2. Rename @SelectClause to SelectClause, because you are declaring this variable. 将@SelectClause重命名为SelectClause,因为您要声明此变量。
  3. Check the usage of SET clauses. 检查SET子句的用法。 I have added one. 我添加了一个。
  4. Have a look at prepared statements reference, it will help you to execute the query you built. 看看准备好的语句参考,它将帮助您执行您构建的查询。

you have wrong concat functions. 你有错误的concat函数。 Try this. 尝试这个。

if v_mode='SearchString' then
DECLARE @SelectClause varchar(2000);
SET @SelectClause =CONCAT(select (SURVEY_USER.username,SURVEY.*) from SURVEY, 'SURVEY_USER');
if SearchString is not null then
@SelectClause=CONCAT(@SelectClause, 'where' ,SearchString);
end if;
SET @SelectClause=@SelectClause
order by SURVEY.created_date DESC
execute(@SelectClause)
end if;

try this. 尝试这个。 let me know if you need anything else. 需要帮助请叫我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM