mysql stored procedure, the data in the database stored column-row type of data conversion, data inserted bulk

1. A table of contents in the content stored in each field, one record contains only the content of a field, represents Table B column_id field name, a field name for each record.

2. Tables A and B binding, the plurality of records into line A in the table storage, and into column_id of Table B field name.

3. The line data tables A and B was inserted into the temporary table 20 is inserted at every temporary table form.

persion_id content unique_flag
1 Small 123
1 22 123
1 male 123
column_id table_id
Full name info
age info
gender info
persion_id Full name age gender unique_flag
1 Small 22 male 123

Data obtained in the form of the above requirements, according to FIG. 1, FIG. 2 FIG. 3 to obtain table information.

Solutions:

1. Define variable from the required assignment in a database table to a variable field, and cyclic operation, the number of fields per insert, and the like.

- definition of variables
    the DECLARE INSERT SQL VARCHAR (10000) the CHARACTER the SET UTF8;  
    the DECLARE valueSql VARCHAR (40000) the CHARACTER the SET UTF8;
    the DECLARE the INT DONE the DEFAULT 0; - determining whether the cursor cycle is completed
    the DECLARE the INT In Flag the DEFAULT 0;
    the DECLARE patient_id1 VARCHAR (80) the CHARACTER the SET UTF8;
    the DECLARE column_id1 VARCHAR (4000) the CHARACTER the SET UTF8 the DEFAULT 0;
    the DECLARE content1 VARCHAR (4000) the CHARACTER the SET UTF8;
    the DECLARE unique_flag2 VARCHAR (40) the CHARACTER the SET UTF8 the DEFAULT. 1;
    the DECLARE unique_flag1 VARCHAR (40) the CHARACTER the SET UTF8;
    the DECLARE J VARCHAR (40) DEFAULT 1; - number of fields in a record change, dynamic insertion vALUE variable names and values
    DECLARE CNT VARCHAR (40) DEFAULT 0 ; - counter counts the number of records has been added

2. Define a cursor, the tables A and B are connected to obtain data to be processed.

DECLARE mycursor CURSOR FOR SELECT 
        a.patient_id,
        b.table_id,
        b.column_id,
        a.content,
        a.unique_flag 
    from qs_answer  a
    LEFT JOIN ques_columns_master  b ON a.unique_key=b.answer_id  
    and a.question_id=b.question_id WHERE b.table_id='health' and a.delete_flag=0 
    ORDER BY a.unique_flag;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;

3. variable initialization, initialization of insertSql and valueSql

SET insertSql=CONCAT('insert into healthtmp(patient_id,unique_flag,');
SET valueSql='VALUES';

4. Open the cursor, the cursor processing each read value, and the values ​​passed to insertSql valueSql, and at every 20 counts and splice insertSql valueSql statement and executes sql statement.

- Open the cursor
    the OPEN MyCursor;
    param_loop: LOOP    
        the FETCH MyCursor the INTO patient_id1, table_id1, column_id1, content1, unique_flag1;
        the IF DONE =. 1 THEN
            the LEAVE param_loop;
        the END the IF;
    the SET In Flag =. 1;
    the IF the CNT <= 20 is THEN
                the IF THEN unique_flag2 = unique_flag1 // in different unique_flag to insert a record
                        IF j <12 THEN // every record there is a field 12 of storage columns, into the line memory required
                            SET insertSql = CONCAT (insertSql, column_id1 , ',');
                        the IF the END;
                        the IF THEN J = 12 is 
                            the SET iNSERT SQL = CONCAT (iNSERT SQL, column_id1, ')'); // for each 20 iNSERT SQL insert statements requires only one insert field
                        The IF the END;
            the IF THEN // content1 the IS NULL when content1 is empty, the result is null the concat connection, stored in the database needs to be treated as a field null value
                                the SET content1 = '';
                        the END the IF;
            the IF MOD 12 is J = 0 THEN @ 12 is a value per content
                            the SET valueSql = CONCAT (valueSql, '' '', content1, '' '', '),');
                        the END the IF;
             ! 0 the IF THEN J = MOD 12 is
                            the SET valueSql = CONCAT (valueSql, '' '', content1, '' '', ',');
                        the END the IF;
                            the SET J = J +. 1;
             the END the IF;
                the IF unique_flag2!=unique_flag1 THEN
                        SET CNT=CNT+1;
                         THEN the CNT = 21 is the IF
                            the IF = In Flag. 1 THEN 
                                the SET = valueSql the substring (valueSql,. 1, CHAR_LENGTH (valueSql) -1); 
                                the SET @ SqlCmd1 = CONCAT (INSERT SQL, valueSql); // 20 when recording, stitching and INSERT SQL valueSql, performing SqlCmd1, inserts the result into the database
                                the PREPARE stmt the FROM @ SqlCmd1;
                                the eXECUTE stmt;
                                SET iNSERT SQL = CONCAT ( 'iNSERT iNTO healthtmp (patient_id, unique_flag,');
                                SET valueSql = CONCAT ( 'the VALUES');
                            the END the IF;
                                the SET the CNT . 1 =;
                                the SET. 1 = J;
                        END IF;
                        IF j<12 THEN 
                                SET insertSql=CONCAT(insertSql,column_id1,',');    
                        END IF;
                        IF j=12 THEN
                                SET insertSql=CONCAT(insertSql,column_id1,')');    
                        END IF;
                        IF update_date1 IS NULL THEN 
                                SET update_date1='';
                        END IF;
                        IF content1 IS NULL THEN 
                                SET content1='';
                        END IF;
                        SET valueSql = CONCAT (valueSql, ' (' '', patient_id1, '' ',' '', unique_flag1, '' ',' '', content1, '' ','); // When the initial values of other fields assignment
                        the SET J = J +. 1;
                        the SET unique_flag2 = unique_flag1;
                the END the IF;
         the END the IF;
  the END LOOP param_loop;
   - inserting the last recording 
    the SET = valueSql the substring (valueSql,. 1, CHAR_LENGTH (valueSql) -1); 
        the IF = In Flag. 1 THEN 
            the SET @ SqlCmd1 = CONCAT (INSERT SQL, valueSql);     
             the PREPARE stmt the FROM @ SqlCmd1;
            the EXECUTE stmt;
         the END the IF;
    Close MyCursor;

    For inserted, after each value values ​​need to add a comma, comma need to be removed when the last value, needs to be extracted out of the last value valueSql, because there are Chinese in the field, it is necessary to correctly use char_length interception interception.

Published 36 original articles · won praise 19 · views 30000 +

Guess you like

Origin blog.csdn.net/qq_27182767/article/details/84728455