MySQL存储过程的行为不一致

时间:2017-09-16 16:27:35

标签: mysql stored-procedures

我正在尝试使用以下函数编写mysql存储过程:

  1. 输入是报告参数,包括报告类型,要运行的数据库 反对和一些日期和过滤器参数
  2. 该过程在表(报告)中查找报告并读取报告参数。
  3. 某些报告是简单查询,而其他报告是2个报告(isunion=1
  4. 的UNIONS
  5. 从报告表中读取报告参数后,该过程将这些参数传递给build_report函数,该函数将它放在一起 返回sql查询。
  6. 在单个查询的情况下,程序完美地工作(即只有1个报告部分),但是有2个部分,程序 失败。似乎无法从中获得价值 build_report函数,即使是多部分查询的part1 与具有单个查询的报告的情况几乎相同 (is_union = 0),但从报告表中获取部件的report_id除外。
  7. 我错过了什么?任何见解都会非常感激。

    以下程序在isunion=0时完美无效,但在isunion=1时失败(即使我完全退出第2部分)

        CREATE PROCEDURE `runreport_u`(
        in all_active INT(1),
        in reportid INT(10), 
        in db_id INT(10), 
        in start_date date, 
        in end_date date, 
        in inc_grpby INT(1), 
        OUT qry_part varchar(20000), #only outputing these to help debug
        OUT qry_part1 varchar(20000),
        OUT qry_part2 varchar(50000),
        OUT qry varchar(40000),
        OUT union_rep_id1 int(10),
        OUT union_rep_id2 int(10),
        OUT isunion int(10))
        BEGIN
        # purpose - this procedures looks up the specifications of a report query in table report. If the report is a single query the procedure works perfectly (case isunion=0) . When the qry is made up of 2 queries to be unionized, the build_report function which builds the parts seems to return nothing. 
    
        DECLARE part int DEFAULT NULL;
    
        # is this report a union
        SELECT is_union INTO isunion FROM report WHERE report_id=@reportid;
    
        IF isunion=0 THEN
        #report is not a union
                SET part=1;
                SET union_rep_id1=reportid;
              SET qry_part = build_report(all_active,union_rep_id1,db_id,start_date,end_date,inc_grpby); 
        #log the components into gen table (more for debugging than anything else)
                INSERT INTO gen (report_number,run_date,part_number,part_report_number,qry_part,part_order,param_all_active,param_report_id,param_db_id,param_start_date,param_end_date,param_incgrp)   VALUES (reportid, now(),part,COALESCE(union_rep_id1,999),qry_part,part,all_active,reportid,db_id,start_date,end_date,inc_grpby);        
    
        SET qry=qry_part;
    
        ELSE 
        #report has 2 or more parts
        #*** first part
            SET part=1;
        #set the first report part - works (returns same union_rep_id as above)     
            SELECT union_report_id1 INTO union_rep_id1 FROM report WHERE report_id=reportid;
        #get the query - in principle identical to the above but works above and returns nothing here 
                SET qry_part1 = build_report(all_active,union_rep_id1,db_id,start_date,end_date,inc_grpby); 
    
                INSERT INTO gen (report_number,run_date,part_number,part_report_number,qry_part,part_order,param_all_active,param_report_id,param_db_id,param_start_date,param_end_date,param_incgrp)   VALUES (reportid, now(),part,COALESCE(union_rep_id1,999),qry_part1,part,all_active,reportid,db_id,start_date,end_date,inc_grpby);       
    
        #*** second part
            SET part=2;
            SELECT union_report_id2 INTO union_rep_id2 FROM report WHERE report_id=reportid;
              SET qry_part2 = build_report(all_active,union_rep_id2,db_id,start_date,end_date,inc_grpby); 
                INSERT INTO gen (report_number,run_date,part_number,part_report_number,qry_part,part_order,param_all_active,param_report_id,param_db_id,param_start_date,param_end_date,param_incgrp)   VALUES (reportid, now(),part,COALESCE(union_rep_id2,999),qry_part,part,all_active,reportid,db_id,start_date,end_date,inc_grpby);        
    
        #*******
        #join the parts together in UNION
        SET qry=CONCAT(qry_part,' UNION ',qry_part2);
    
        END IF;
    
    #had to add the line below otherwise Prepare stmt failed to recognize qry as variable
    
        SET @final_qry=qry;
    
        PREPARE stmt FROM @final_qry;
        EXECUTE stmt;
        DEALLOCATE PREPARE stmt;
    
        #fails if isunion=1
    
        END
    

0 个答案:

没有答案