AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / dba / 问题 / 199005
Accepted
VansFannel
VansFannel
Asked: 2018-03-01 01:14:24 +0800 CST2018-03-01 01:14:24 +0800 CST 2018-03-01 01:14:24 +0800 CST

嵌套选择:使用两个子查询将值插入表中

  • 772

我正在为 SQL Server 2016 开发存储过程。

我必须这样做(下面的 sql 语句不起作用):

Insert into AggregationChildren (AggregationId, AggregationChildrenId, Position)
SELECT Parent, Serial, Position
FROM
    OPENJSON (@json, '$.Aggregations')
    WITH (  Parent nvarchar(20) '$.Parent',
            Children nvarchar(max) AS JSON )
        CROSS APPLY 
            OPENJSON (Children)
            WITH ( Serial nvarchar(20), Position int)

我的问题是AggregationIdand是AggregationChildrenId整数和Parentand 。Serialnvarchar(20)

和 之间存在关系Parent,AggregationId和 之间存在Serial关系AggregationChildrenId。我可以用Code桌子得到它。

我知道当我必须为一列插入值时我该怎么做:

Insert into AggregationChildren (AggregationId)
    Select CodeId from Code Where Serial = (SELECT Parent
    FROM
        OPENJSON (@json, '$.Aggregations')
        WITH (  Parent nvarchar(20) '$.Parent',
                Children nvarchar(max) AS JSON )
            CROSS APPLY 
                OPENJSON (Children)
                WITH ( Serial nvarchar(20), Position int))

我要插入的数据是:

{
    ...,
    "Aggregations": [{
        "Parent": "88962730000000004051",
        "Children": [{
            "Serial": "81861400000000020227",
            "Position": "1"
        }, {
            "Serial": "81861400000000033191",
            "Position": "2"
        }, {
            "Serial": "81861400000000046051",
            "Position": "3"
        },
        ...
        ]
    }, {
        "Parent": "88962730000000016653",
        "Children": [{
            "Serial": "81861400000001825849",
            "Position": "1"
        }, {
            "Serial": "81861400000001832643",
            "Position": "2"
        }, {
            "Serial": "81861400000001841911",
            "Position": "3"
        }, {
            "Serial": "81861400000001850803",
            "Position": "4"
        }, {
            "Serial": "81861400000001862474",
            "Position": "5"
        }, {
            "Serial": "81861400000001874774",
            "Position": "6"
        }, {
            "Serial": "81861400000001884159",
            "Position": "7"
        }, {
            "Serial": "81861400000001898352",
            "Position": "8"
        }, {
            "Serial": "81861400000001904764",
            "Position": "9"
        },
        ...
        ]
    }]
}

但是,如果我必须使用两个,我该怎么办Select CodeId from Code Where Serial = (SELECT ...?

我想我必须做这样的事情,但不做同样的选择三次:

 Insert into AggregationChildren (AggregationId, AggregationChildrenId, Position)
    Select CodeId from Code Where Serial = (SELECT Parent
    FROM
        OPENJSON (@json, '$.Aggregations')
        WITH (  Parent nvarchar(20) '$.Parent',
                Children nvarchar(max) AS JSON )
            CROSS APPLY
                OPENJSON (Children)
                WITH ( Serial nvarchar(20), Position int)), 
    Select CodeId from Code Where Serial = (SELECT Serial
    FROM
        OPENJSON (@json, '$.Aggregations')
        WITH (  Parent nvarchar(20) '$.Parent',
                Children nvarchar(max) AS JSON )
            CROSS APPLY
                OPENJSON (Children)
                WITH ( Serial nvarchar(20), Position int)), 
    SELECT Position
    FROM
        OPENJSON (@json, '$.Aggregations')
        WITH (  Parent nvarchar(20) '$.Parent',
                Children nvarchar(max) AS JSON )
            CROSS APPLY
                OPENJSON (Children)
                WITH ( Serial nvarchar(20), Position int)
sql-server t-sql
  • 1 1 个回答
  • 2651 Views

1 个回答

  • Voted
  1. Best Answer
    McNets
    2018-03-01T04:52:28+08:002018-03-01T04:52:28+08:00

    如果我理解正确:

    create table AggregationChildren(AggregationId int, AggregationChildrenId int, Position int);
    create table Code (CodeID int, Serial nvarchar(20));
    insert into Code values
    (1, '88962730000000004051'),
    (2, '81861400000000020227'),
    (3, '81861400000000033191'),
    (4, '81861400000000046051'),
    (5, '88962730000000016653'),
    (6, '81861400000001825849'),
    (7, '81861400000001832643'),
    (8, '81861400000001841911'),
    (9, '81861400000001850803'),
    (10, '81861400000001862474'),
    (11, '81861400000001874774'),
    (12, '81861400000001884159'),
    (13, '81861400000001898352'),
    (14, '81861400000001904764');
    GO
    
    14 行受影响
    
    declare @json nvarchar(max);
    set @json = '
    {
        "Aggregations": [{
            "Parent": "88962730000000004051",
            "Children": [{
                "Serial": "81861400000000020227",
                "Position": "1"
            }, {
                "Serial": "81861400000000033191",
                "Position": "2"
            }, {
                "Serial": "81861400000000046051",
                "Position": "3"
            }
            ]
        }, {
            "Parent": "88962730000000016653",
            "Children": [{
                "Serial": "81861400000001825849",
                "Position": "1"
            }, {
                "Serial": "81861400000001832643",
                "Position": "2"
            }, {
                "Serial": "81861400000001841911",
                "Position": "3"
            }, {
                "Serial": "81861400000001850803",
                "Position": "4"
            }, {
                "Serial": "81861400000001862474",
                "Position": "5"
            }, {
                "Serial": "81861400000001874774",
                "Position": "6"
            }, {
                "Serial": "81861400000001884159",
                "Position": "7"
            }, {
                "Serial": "81861400000001898352",
                "Position": "8"
            }, {
                "Serial": "81861400000001904764",
                "Position": "9"
            }
            ]
        }]
    }
    ';
    

    > -- 提取json字段
    > ; x 为
    > (
    > 选择
    > 家长,
    > 串行,
    > 职位
    > 从
    > OPENJSON (@json, '$.Aggregations')
    > WITH (父 nvarchar(20) '$.Parent',子 nvarchar(max) 作为 JSON)
    > 交叉申请
    > OPENJSON(儿童)
    > WITH (串行 nvarchar(20), 位置 int)
    > )
    > INSERT INTO AggregationChildren (AggregationId, AggregationChildrenId, Position)
    > 选择 c1.CodeID、c2.CodeID、位置
    > 从 x
    > 加入代码c1
    > ON c1.Serial = x.Parent
    > 加入代码c2
    > 开 c2.Serial = x.Serial;
    > 去
    >
    12 行受影响
    >

    SELECT * FROM AggregationChildren;
    GO
    
    聚合 ID | 聚合ChildrenId | 位置 ------------: | --------------------: | --------: 1 | 2 | 1 1 | 3 | 2 1 | 4 | 3 5 | 6 | 1 5 | 7 | 2 5 | 8 | 3 5 | 9 | 4 5 | 10 | 5 5 | 11 | 6 5 | 12 | 7 5 | 13 | 8 5 | 14 | 9

    dbfiddle在这里

    • 2

相关问题

  • SQL Server - 使用聚集索引时如何存储数据页

  • 我需要为每种类型的查询使用单独的索引,还是一个多列索引可以工作?

  • 什么时候应该使用唯一约束而不是唯一索引?

  • 死锁的主要原因是什么,可以预防吗?

  • 如何确定是否需要或需要索引

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目

    • 12 个回答
  • Marko Smith

    如何让sqlplus的输出出现在一行中?

    • 3 个回答
  • Marko Smith

    选择具有最大日期或最晚日期的日期

    • 3 个回答
  • Marko Smith

    如何列出 PostgreSQL 中的所有模式?

    • 4 个回答
  • Marko Smith

    列出指定表的所有列

    • 5 个回答
  • Marko Smith

    如何在不修改我自己的 tnsnames.ora 的情况下使用 sqlplus 连接到位于另一台主机上的 Oracle 数据库

    • 4 个回答
  • Marko Smith

    你如何mysqldump特定的表?

    • 4 个回答
  • Marko Smith

    使用 psql 列出数据库权限

    • 10 个回答
  • Marko Smith

    如何从 PostgreSQL 中的选择查询中将值插入表中?

    • 4 个回答
  • Marko Smith

    如何使用 psql 列出所有数据库和表?

    • 7 个回答
  • Martin Hope
    Jin 连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目 2014-12-02 02:54:58 +0800 CST
  • Martin Hope
    Stéphane 如何列出 PostgreSQL 中的所有模式? 2013-04-16 11:19:16 +0800 CST
  • Martin Hope
    Mike Walsh 为什么事务日志不断增长或空间不足? 2012-12-05 18:11:22 +0800 CST
  • Martin Hope
    Stephane Rolland 列出指定表的所有列 2012-08-14 04:44:44 +0800 CST
  • Martin Hope
    haxney MySQL 能否合理地对数十亿行执行查询? 2012-07-03 11:36:13 +0800 CST
  • Martin Hope
    qazwsx 如何监控大型 .sql 文件的导入进度? 2012-05-03 08:54:41 +0800 CST
  • Martin Hope
    markdorison 你如何mysqldump特定的表? 2011-12-17 12:39:37 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 对 SQL 查询进行计时? 2011-06-04 02:22:54 +0800 CST
  • Martin Hope
    Jonas 如何从 PostgreSQL 中的选择查询中将值插入表中? 2011-05-28 00:33:05 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 列出所有数据库和表? 2011-02-18 00:45:49 +0800 CST

热门标签

sql-server mysql postgresql sql-server-2014 sql-server-2016 oracle sql-server-2008 database-design query-performance sql-server-2017

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve