• 转换嵌套JSON数据为TABLE


    先准备一些数据:

    创建一张临时表来存储:

    DECLARE @json_table AS TABLE
    (
        [type] NVARCHAR(MAX),
        [desc] NVARCHAR(MAX)
    )
    Source Code

    获取第一层数据:

    INSERT INTO @json_table ([type],[desc])
    SELECT [type],[desc] FROM 
    OPENJSON (@json_text,'$.DB')
    WITH (
        [type] NVARCHAR(MAX) '$.type',
        [desc] NVARCHAR(MAX) '$.desc'
        )
    WHERE [type] IS NOT NULL;
    Source Code

    获取第二层DB_CLR节点的数据:

    INSERT INTO @json_table ([type],[desc])
    SELECT [type],[desc] FROM 
    OPENJSON (@json_text,'$.DB')
    WITH (     
            DB_CLR NVARCHAR(MAX) AS JSON    
         )
    CROSS APPLY 
            OPENJSON (DB_CLR)
            WITH 
            (         
                [type] NVARCHAR(MAX) '$.type',
                [desc] NVARCHAR(MAX) '$.desc'
            );
    Source Code

    同样方法,获取第二层的DB_TABLE节点数据:

    INSERT INTO @json_table ([type],[desc])
    SELECT [type],[desc] FROM 
    OPENJSON (@json_text,'$.DB')
    WITH (     
            DB_TABLE NVARCHAR(MAX) AS JSON
        ) 
    CROSS APPLY 
            OPENJSON (DB_TABLE)
            WITH 
            (         
                [type] NVARCHAR(MAX) '$.type',
                [desc] NVARCHAR(MAX) '$.desc'
            ) ;
    Source Code

    最后查询临时表存储表的数据:

    但是,如果我们想加上节点root名称,用来真正区别记录的类别:

    把临时表添加一个字段[Root]:

    DECLARE @json_table AS TABLE
    (
        [root] NVARCHAR(MAX),
        [type] NVARCHAR(MAX),
        [desc] NVARCHAR(MAX)
    );
    Source Code

    以上三个节点获取的源代码:

    INSERT INTO @json_table ([root],[type],[desc])
    SELECT [key],b.[type],[desc] FROM  
    OPENJSON (@json_text) a
    CROSS APPLY
    OPENJSON (@json_text,'$.DB')
    WITH (
        [type] NVARCHAR(MAX) '$.type',
        [desc] NVARCHAR(MAX) '$.desc'
        )b
    WHERE b.[type] IS NOT NULL;
    
    
    INSERT INTO @json_table ([root],[type],[desc])
    SELECT 'DB_CLR', [type],[desc] FROM 
    OPENJSON (@json_text,'$.DB')
    WITH (     
            DB_CLR NVARCHAR(MAX) AS JSON    
         )
    CROSS APPLY 
            OPENJSON (DB_CLR)
            WITH 
            (         
                [type] NVARCHAR(MAX) '$.type',
                [desc] NVARCHAR(MAX) '$.desc'
            );
            
    
    INSERT INTO @json_table ([root],[type],[desc])
    SELECT 'DB_TABLE', [type],[desc] FROM 
    OPENJSON (@json_text,'$.DB')
    WITH (     
            DB_TABLE NVARCHAR(MAX) AS JSON
        ) 
    CROSS APPLY 
            OPENJSON (DB_TABLE)
            WITH 
            (         
                [type] NVARCHAR(MAX) '$.type',
                [desc] NVARCHAR(MAX) '$.desc'
            ) ;
    Source Code

    最后是查询结果:

    最后再想修改一下,把3段SQL语句,使用UNION ALL串连起来:

    INSERT INTO @json_table ([root],[type],[desc])
    SELECT [key],b.[type],[desc] FROM  
    OPENJSON (@json_text) a
    CROSS APPLY
    OPENJSON (@json_text,'$.DB')
    WITH (
            [type] NVARCHAR(MAX) '$.type',
            [desc] NVARCHAR(MAX) '$.desc'
        )b
    WHERE b.[type] IS NOT NULL
    
    --INSERT INTO @json_table ([root],[type],[desc])
    UNION ALL
    
    SELECT 'DB_CLR', [type],[desc] FROM 
    OPENJSON (@json_text,'$.DB')
    WITH (     
            DB_CLR NVARCHAR(MAX) AS JSON    
         )
    CROSS APPLY 
            OPENJSON (DB_CLR)
            WITH 
            (         
                [type] NVARCHAR(MAX) '$.type',
                [desc] NVARCHAR(MAX) '$.desc'
            )
    
    --INSERT INTO @json_table ([root],[type],[desc])
    UNION ALL
    
    SELECT 'DB_TABLE', [type],[desc] FROM 
    OPENJSON (@json_text,'$.DB')
    WITH (     
            DB_TABLE NVARCHAR(MAX) AS JSON
        ) 
    CROSS APPLY 
            OPENJSON (DB_TABLE)
            WITH 
            (         
                [type] NVARCHAR(MAX) '$.type',
                [desc] NVARCHAR(MAX) '$.desc'
            ) ;
    Source Code
  • 相关阅读:
    day09页面的声明周期函数
    day8小程序的运行环境与基本架构
    day09小程序复习
    day08前后端交互
    day07获取图片
    day07获取用户地址信息
    MySQL 主从同步延迟的原因及解决办法
    升级Oracle 19c经验: TTS 在使用datapump导matadata时EXCLUDE=STATISTICS 不启作用
    12c,19c自动kill长时间未活动会话特性
    SuSE11单实例二进制安装MySQL5.7
  • 原文地址:https://www.cnblogs.com/insus/p/10921218.html
Copyright © 2020-2023  润新知