在动态查询的 mysql 存储过程中返回 null

问题描述 投票:0回答:1

谁能告诉我,我错过了什么,我已经投入了4个小时,看了很多的文章,不明白为什么显示返回null

mysql mysql过程如下。

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;
mysql stored-procedures
1个回答
1
投票

用户自定义变量 @get_exam_id 似乎没有在任何地方被分配一个值。如果没有给它分配一个非NULL值,那么评估这一行的结果。

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

就是... @exam_id 将为NULL。这将会级联到下一个CONCAT中,它也会计算为NULL。


用户定义的变量以 @ 的符号字符。

这些都是与存储过程参数和变量不同的,它们不以 @ 在符号字符处。


看来,也许我们的目的是要引用本地变量 get_exam_id而不是用户定义的变量。


FOLLOWUP

个人认为,如果要写程序的正文,我会这样写。

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 ;
© www.soinside.com 2019 - 2024. All rights reserved.