0

The below SP is not giving any result even though there are 48 rows as per the where clause

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. Please give me some solution. There are my parameters that I am passing v_mode='SearhByString' v_SearchString='SURVEY_USER.username=chiragfanse'

It should return 48 rows but does not return anything.

1
  • 2
    Including the error message might be helpful. Commented Oct 18, 2011 at 8:24

2 Answers 2

2
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.
  3. Check the usage of SET clauses. I have added one.
  4. Have a look at prepared statements reference, it will help you to execute the query you built.
Sign up to request clarification or add additional context in comments.

4 Comments

@Devart.I Wrote the code same as you provided but it shows the error near DECLARE SelectClause VARCHAR(2000)
I have no problems with this code. Please show full procedure (CREATE PROCEDURE...).
Note, than SelectClause and @SelectClause are different variables; have a look at my code. Fix it firstly. About the last error - check genarated query, try to output it with SELECT statement.
0

you have wrong concat functions. 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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.