简体   繁体   English

mySQL 将过程存储到 JSON

[英]mySQL store Procedure to JSON

I created a stored procedure that generates a new table in the database consisting of many parts of other tables.我创建了一个存储过程,它在数据库中生成一个由其他表的许多部分组成的新表。 The thing is that the table generated is not created.问题是没有创建生成的表。 I want to send the table generated, let's say to a local location but in JSON format.我想将生成的表发送到本地位置,但格式为 JSON。 Here is the procedure v1 I created.这是我创建的程序 v1。

USE DataBase;

DELIMITER $$
CREATE PROCEDURE ActaReuniones()
BEGIN
    CREATE TABLE IF NOT EXISTS meeting(
        idActaReunion INT AUTO_INCREMENT PRIMARY KEY,
        fecha_registro DATE,
        clientid INT,
        idgestor INT,
        Nombre_Cliente VARCHAR(255),
        Gestor VARCHAR(255),
        Perfil VARCHAR(255),
        TipoCliente VARCHAR(255),
        NombreCiudad VARCHAR(255),
        NombreDepartamento VARCHAR(255)
    )  ENGINE=INNODB;

    INSERT INTO meeting (
        idActaReunion,
        fecha_registro,
        clientid,
        idgestor,
        Nombre_Cliente,
        Gestor,
        Perfil,
        TipoCliente,
        NombreCiudad,
        NombreDepartamento
    )
    SELECT * FROM 
        # A lot of queries and joins from other tables to fill the table created.  
        # It works, believe me.
    WHERE AR.fecha_registro < '2020-11-01';
END$$
DELIMITER ;

After thinking about this, I do not want to duplicate de DataBase (which is exactly what will happen if I run this procedure), so I want to cut off the sections CREATE and INSERT, letting with only the SELECT section.考虑到这一点后,我不想复制 de DataBase(如果我运行此过程,这正是会发生的情况),所以我想切断 CREATE 和 INSERT 部分,只保留 SELECT 部分。 This section is what I want to send as a JSON file to a local location.这部分是我想作为 JSON 文件发送到本地位置的内容。 I assume that the data collected in the SELECT section is possible to write in a file without passing through a table created previously.我假设在 SELECT 部分收集的数据可以写入文件而无需通过之前创建的表。 Is it possible to do that?有可能这样做吗? Thanks in advance.提前致谢。

I solved this issue after some research.经过一番研究,我解决了这个问题。 My approach was as follows: As the table the query will go to is already created, but we don't want to duplicate the database, so we send the result of the query to the table (with a WHERE previously established);我的方法如下:由于查询将 go 到的表已经创建,但我们不想复制数据库,所以我们将查询结果发送到表(之前建立了 WHERE); With this result, the JSON is created and this JSON is sent to the desired location.使用此结果,将创建 JSON,并将此 JSON 发送到所需位置。 Then a TRUNCATE is performed on the table (all within the procedure):然后在表上执行 TRUNCATE(都在过程中):

DELIMITER $$
CREATE PROCEDURE ActaReuniones()
BEGIN
    INSERT INTO meeting (  #the table is already created in the DB
        idActaReunion,
        fecha_registro,
        clientid,
        idgestor,
        Nombre_Cliente,
        Gestor,
        Perfil,
        TipoCliente,
        NombreCiudad,
        NombreDepartamento  
        WHERE DATE
             # the query
        SELECT JSON_OBJECT
            ('idActareunion', idActaReunion,
             'fechaRegistro', fecha_registro, 
             'clientid', clientid,
             'idGestor', idgestor,
             'NombreCliente', Nombre_Cliente,
             'Gestor', Gestor,
             'Perfil', Perfil,
             'TipoCliente', TipoCliente,
             'NombreCiudad', NombreCiudad,
             'NombreDepartamento', NombreDepartamento
             )
        FROM meeting
        INTO OUTFILE '/location/meeting.json';
        
        TRUNCATE TABLE meeting;
           
    END$$
    DELIMITER ;

    
    
    

I call this procedure and it works, Now.我调用这个程序,它现在可以工作了。 I am trying to figure it out how could I update the json file with new data that it is include in the tables included in the query.我试图弄清楚如何使用包含在查询中的表中的新数据更新 json 文件。 I tried to update this file but it is not possible to re-write a json file that is already created.我尝试更新此文件,但无法重新编写已创建的 json 文件。 Let's see.让我们来看看。 Any hint I appreciate it.任何提示我都很感激。 Kind regards亲切的问候

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM