MySQL结果由存储过程-Insert Statement上的多行组成

时间:2014-07-04 10:25:21

标签: mysql stored-procedures

这是我的存储过程,我遇到了麻烦......我正在

insert into PK_SAMPLES(U_KEY) select cf_extEmployeeId from salesreps;
set v_queryid = v_queryid + 1;
select RULEID INTO v_RuleId from TAB_RULE_LIST WHERE RULEID=3;
select DISTINCT T_TABLENAME INTO v_T_TABLENAME from TAB_RULE_LIST WHERE RULEID=3 and T_TABLENAME is not null;

select T_COLUMNNAME INTO v_t_columnname FROM mapping_table where constraint_type='PK';

IF (v_RuleId = 3) THEN

        SET V_JOIN_QUERY = ''; 
        SET source_columns = '';
        SET target_columns = '';

        CREATE TEMPORARY TABLE IF NOT EXISTS source_target_column_list 
        AS 
        (
            SELECT                        *
          FROM (
            ( SELECT DISTINCT
              CASE
                WHEN TAB.transformation_type='HARDCODED'
                THEN IFNULL(TAB.TRANSFORMATION,TAB.s_columnname)
                ELSE TAB.s_columnname
              END s_columnname,
              TAB.transformation,
              RM.t_columnname,
              TAB.S_OWNER,
              TAB.transformation_type,
          RM.RULEID RULEID
            FROM RULEMAPPING RM
            INNER JOIN
              (SELECT *
              FROM mapping_table t1
              WHERE ( (t1.constraint_type <>'Derived'
              OR t1.constraint_type       IS NULL) 
              AND t1.ISVALIDMAPPING        ='Y'
              AND t1.transformation_type<>'MODIFIED_DATE'
              AND t1.transformation_type<>'GENERATED'
              AND t1.version_number        =1
                )
              ) TAB
            ON RM.T_TABLENAME        = TAB.T_TABLENAME
            WHERE ( RM.EXECUTION_FLAG='Y'
            OR RM.EXECUTION_FLAG    IS NULL )AND RM.T_TABLENAME='salesreps'
            ) temp )
        );

        SELECT COUNT(*) FROM source_target_column_list INTO n_source_target_column_list;
        SET i_source_target_column_list = 0;
        WHILE i_source_target_column_list < n_source_target_column_list DO 
            select S_OWNER INTO v_S_OWNER from source_target_column_list WHERE @v_RuleId=3;
    select TRANSFORMATION_TYPE INTO v_TRANSFORMATION_TYPE from source_target_column_list WHERE @v_RuleId=3;
    select s_columnname INTO v_s_columnname from source_target_column_list WHERE @v_RuleId=3;
    select TRANSFORMATION INTO v_TRANSFORMATION from source_target_column_list WHERE @v_RuleId=3;

            IF v_S_OWNER = 'ATF' OR v_TRANSFORMATION_TYPE = 'HARDCODED' THEN
                CASE WHEN v_TRANSFORMATION IS NOT NULL 
                    THEN SET v_s_columnname_changed = CONCAT('to_char(', v_TRANSFORMATION, ')'); 
                    ELSE SET v_s_columnname_changed = v_s_columnname; 
                END CASE;
                UPDATE source_target_column_list set s_columnname = v_s_columnname_changed WHERE s_columnname = v_s_columnname;
                UPDATE mapping_table set s_columnname = CONCAT('to_char(', source_target_column_list.t_columnname, ')') WHERE s_columnname = v_s_columnname;

            ELSE
                UPDATE source_target_column_list SET s_columnname = coalesce(v_TRANSFORMATION,v_s_columnname) WHERE s_columnname = v_s_columnname;
                UPDATE mapping_table SET s_columnname = v_t_columnname;
            END IF;
            SET i_source_target_column_list = i_source_target_column_list + 1;
        END WHILE;

      SET V_SOURCE_QUERY  = 'Select';
      SET V_SOURCE_QUERY = CONCAT(V_SOURCE_QUERY, ' ', source_columns, ' ', V_JOIN_QUERY);
      SET v_target_query = 'Select';
      SET v_target_query = CONCAT(v_target_query, ' ', target_columns, ' ', 'from ', v_T_TABLENAME);


  select count(0) INTO V_MODIFIED_DATE_COUNT FROM mapping_table WHERE T_TABLENAME=v_T_TABLENAME AND TRANSFORMATION_TYPE='MODIFIED_DATE';

        SELECT COUNT(*) FROM PK_SAMPLES INTO n_pk_sample;
        SET i_pk_sample = 0;
        WHILE i_pk_sample < n_pk_sample DO 
            select U_KEY into v_value from PK_SAMPLES;
            SET v_source_query_1 = CONCAT(v_source_query, ' where ', v_s_columnname, ' = ''', v_value, '''');
            SET V_TARGET_QUERY_1 = CONCAT(V_TARGET_QUERY, ' where ', v_t_columnname, ' = ''', v_value, '''');
            CALL QUERY_INSERT_PROC(v_queryid, v_RULEID,v_T_TABLENAME,V_SOURCE_QUERY_1,V_TARGET_QUERY_1,v_version_number,v_RMID,v_MODULE_NAME);                              
            SET i_pk_sample = i_pk_sample + 1;
        END WHILE;
END IF;

END//
DELIMITER ;

由于insert语句,结果由多行组成。

如果有人能帮助我找到解决方案,我将不胜感激。

感谢。

0 个答案:

没有答案