0

can someone please tell me what I am missing I have invested 4 hours and watch lots of article can't figure out why is showing return null

mysql mysql procedure is as follows.

create  procedure proc_studymaterial_listing(
    get_exam_id int(11)
)
begin
    set @query1 = concat( "select c.content_type, 
                                  c.title, 
                                  c.status, 
                                  c.created_on, 
                                  c.pdf_is_downloadable, 
                                  a.content_id, 
                                  a.expiry_date, 
                                  exam_Name,  
                                  fun_subject_name(a.subject_id) as subject_name, 
                                  fun_subject_name(a.chapter_id) as chapter_name
                           from  tbl_studymaterial_exam_chapter_map as a 
                           join tbl_exam_master as b on a.exam_id= b.exam_ID
                           join tbl_studymaterial as c on c.id= a.content_id  
                           where c.status=1 " );

    if( get_exam_id = 0 ) 
    then
        set @exam_id = " and 1=1 ";
    else
        set @exam_id = concat( ' and  b.exam_ID = " ', @get_exam_id , '"' );
    end if;     

    SET @final_query =  CONCAT ( @query1,@exam_id );    

    PREPARE stmt FROM @final_query ; 
    # select stmt; 
    # EXECUTE stmt;
    #  DEALLOCATE PREPARE stmt;

end;
1
  • Replace PREPARE statement with SELECT @final_query; and show the output. PS. get_exam_id is int(11) - why you quote it with dquote chars as it is a string? Commented May 7, 2020 at 13:23

1 Answer 1

1

User-defined variable @get_exam_id doesn't appear to be assigned a value anywhere. If that's not assigned a non-NULL value, then the result of evaluating this line:

set @exam_id = concat( ' and  b.exam_ID = " ', @get_exam_id , '"' );

is that @exam_id will be NULL. And that will cascade into the next CONCAT, which will also evaluate to NULL.


User-defined variables start with an @ at sign character.

Those are distinct and separate from procedure arguments and variables, which do not start with an @ at sign character.


Looks like maybe the intent was to reference the local variable get_exam_id, not a user-defined variable.


FOLLOWUP

Personally, if I had to write the body of the procedure, I'd do it something like this:

DELIMITER $$
CREATE PROCEDURE proc_studymaterial_listing( get_exam_id INT(11))
BEGIN
   SET @sql = CONCAT( 'SELECT c.content_type'
              ,'\n'  ,'     , c.title'
              ,'\n'  ,'     , c.status' 
              ,'\n'  ,'     , c.created_on' 
              ,'\n'  ,'     , c.pdf_is_downloadable' 
              ,'\n'  ,'     , a.content_id' 
              ,'\n'  ,'     , a.expiry_date' 
              ,'\n'  ,'     , b.exam_name'
              ,'\n'  ,'     , fun_subject_name(a.subject_id)  AS subject_name'
              ,'\n'  ,'     , fun_subject_name(a.chapter_id)  AS chapter_name'
              ,'\n'  ,'  FROM tbl_studymaterial_exam_chapter_map a' 
              ,'\n'  ,'  JOIN tbl_exam_master b'
              ,'\n'  ,'    ON b.exam_id = a.exam_id'
              ,'\n'  ,'  JOIN tbl_studymaterial c'
              ,'\n'  ,'    ON c.id = a.content_id'
              ,'\n'  ,' WHERE c.status = 1'
              ,'\n'
              ,CASE
                 WHEN get_exam_id <> 0
                 THEN '   AND b.exam_id = ? '
                 ELSE ''
               END
              ,'\n'  ,' ORDER BY subject_name, chapter_name, exam_name'
              );
   PREPARE stmt FROM @sql;
   IF (get_exam_id <> 0) THEN
      SET @id = get_exam_id;
      EXECUTE stmt USING @id;
      SET @id = '';
   ELSE
      EXECUTE stmt;
   END IF;
   DEALLOCATE PREPARE stmt;
   SET @sql = '';
END;
$$
DELIMITER ;
Sign up to request clarification or add additional context in comments.

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.