MySQL How to INSERT INTO [temp table] FROM [Stored Procedure]

前端 未结 5 1125
心在旅途
心在旅途 2020-12-03 13:32

This is very similar to question 653714, but for MySQL instead of SQL Server.

Basically, I have a complicated select that is the basis for several stored procedures.

5条回答
  •  暖寄归人
    2020-12-03 14:22

    Maybe it's a closed topic, but I would like to offer a solution based on the properties of MySQL temporary tables. First, the way to create the temporary table would not be to call the stored procedure "CREATE TEMPORARY TABLE tmp EXEC nested_sp ();". The query is to the temporary table of "infrastructure", (to name it somehow).

    To achieve the desired result, it is necessary to create 2 stored procedures, the first stored procedure processes the data and fills the temporary "infrastructure" table, the second stored procedure, reads this table and continues with the process and finally "DROP" the "infrastructure" table

    This is the first stored procedure:

        CREATE DEFINER = 'root'@'localhost'
    PROCEDURE cajareal.priv_test()
    BEGIN
      CREATE TEMPORARY TABLE IF NOT EXISTS  tmp(
          column1 TEXT
        , column2 TEXT
        , column3 TEXT
        );
    
    
    
      INSERT INTO tmp(column1, column2 , column3) VALUES(CURDATE(), CURRENT_DATE(), CURRENT_TIMESTAMP());
    
    END
    

    This is the second stored procedure:

    CREATE DEFINER = 'root'@'localhost'
    PROCEDURE cajareal.priv_caller()
    BEGIN
      CALL priv_test;
    
      -- Read data of "infrastructure" table
      SELECT * FROM tmp;
    
    
      -- Do the business logic
    
    
      -- Delete the "infrastructure" table
      DROP TABLE tmp;
    END
    

    I use this technique to analyze a string and convert it to the table

提交回复
热议问题