Multiple cursors in nested loops in MySQL

前端 未结 5 620
半阙折子戏
半阙折子戏 2021-02-05 11:14

I wish to do something which appear a bit complicated in MySQL. In fact, I wish to open a cursor, do a loop, and in this loop, open a second cursor using the data from the previ

5条回答
  •  悲哀的现实
    2021-02-05 11:41

    Correct me if I'm wrong, but it looks like what you are trying to do is a bulk insert of records into your "SLA_DEMANDE_STATUS" table. Include every criteria for every indicator found and default it with the values of '0009', 'OK' and 10.0 per each indicator's Criteria ID.

    This can all be done in a single SQL-Insert. INSERT INTO ... from a SQL-Select...

    Now, if you want to only include a single "id_indicateur" entry, you can add that to the WHERE clause of the select statement.

    Notice that my SQL-Select has forced values to correspond with the columns you want to have populated. They will all be inserted to the destination table by the same name. The nice thing about this, you can just run the SQL-SELECT portion just to see the data you would EXPECT to have inserted... if its incorrect, you can adjust it to fit whatever restrictions you want.

    insert into SLA_DEMANDE_STATUS 
        ( iddemande,
          idindicateur,
          indicateur_status,
          progression ) 
        SELECT 
             '0009' iddemande,
             c.id_criterere idindicateur,
             'OK' indicateur_status,
             10.0 progression
           FROM 
              indicateur i;
                 JOIN critere c
                    ON i.id_indicateur = c.id_indicateur
    

提交回复
热议问题