2017-09-12 2 views
0

Ich habe eine Tabelle book_meetings, die 70000 Datensatz haben, und ich möchte diese Daten in eine andere Tabelle mit wenig Modifikation migrieren dafür habe ich eine Mysql gespeicherte Prozedur erstellt. Datensätze werden in eine neue Tabelle eingefügt, Werte werden jedoch als null festgelegt. Ich wähle nur vier Spalten aus book_meetings Tabelle und möchte sie in Tabelle einfügen.Aktualisieren und Einfügen von Tabellenzeilen mit einer gespeicherten Mysql-Prozedur

id int 
date date 
meet_at time 
duration_in_hours decimal 

Was ich will, ist die start_date und end_date basierend auf obigen Werte berechnen.

Zum Beispiel:

if date ="2017-09-08" , meet_at is "09:00:00" and duration_in_hours is 1.5 

dann start_date will be "2017-09-08 09:10:00"

end_date= start_date_duration_in_hour 
end_date will be "2017-09-08 09:10:00" 

start_date = concat date and meet_at 
end_date = start_date + duration_in_hours 

and insert this values in new table 

wenn es eine andere bessere Idee ist dann bitte vorschlagen

CREATE PROCEDURE book_meetings8() 
BEGIN 
     -- Declare local variables 
    DECLARE done BOOLEAN DEFAULT 0; 
    DECLARE meet_at TIME; 
    DECLARE start_date DATETIME; 
    DECLARE tmp_date VARCHAR(255); 
    DECLARE end_date DATETIME; 
    DECLARE end_recurring_date DATE; 
    DECLARE date1 DATE ; 
    DECLARE id INTEGER(11); 
    DECLARE duration DECIMAL(8,2); 
    DECLARE minutes INTEGER(11); 


    -- Declare the cursor 
     DECLARE iter CURSOR 
    FOR 
     SELECT id,date, meet_at,duration_in_hours FROM 
     book_meetings LIMIT 100; 
    -- Declare continue handler 
    DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done=1; 

    -- Open the cursor 
    OPEN iter; 
    -- Loop through all rows 
    REPEAT 
    -- Get order number 

    FETCH iter INTO id,date1,meet_at,duration; 
    SET minutes = duration * 60; 
    SET start_date = CAST(date1 as char) + " "+CAST(meet_at as 
    char); 
    SET end_date = CAST(start_date as datetime) + INTERVAL 
minutes MINUTE;  

    INSERT INTO 
    book_meetings_1(start_date,end_date) 
    VALUES(start_date,end_date); 
    -- End of loop 
    UNTIL done END REPEAT; 
-- Close the cursor 
CLOSE iter; 
    END; 

Antwort

0

Nun, ich obige Problem mit einzelnen SQL-Anweisung gelöst haben (Einfügung und Aktualisierung alle auf einmal ohne Speicherprozedur aufnehmen)

INSERT INTO temp_diary.book_meetings (id,start_date,end_date) SELECT id,CONCAT(`date`, ' ', `meet_at`) as start_date,DATE_ADD(concat(date,' ',meet_at), INTERVAL `duration_in_hours` HOUR) as end_date FROM estate.book_meetings; 
Verwandte Themen