插入SQL Server 2017中的边缘表

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了插入SQL Server 2017中的边缘表相关的知识,希望对你有一定的参考价值。

目前我正在尝试学习SQL Server 2017的图形数据库功能,为此我试图将一组当前数据加载到一组节点和边缘表中。

我没有从SELECT查询加载到节点表中的问题。但是在尝试将数据加载到我的边缘表时遇到了麻烦。

我有一个名为kontomappingDetaljeret的表,其中包含kontoNummer和refNr

我还有包含这些功能的节点表作为节点。我试图映射kontoNummer和refNr列之间的关系。

到目前为止,我有这个不执行的查询

INSERT INTO brugtIRef 
VALUES 
    ((SELECT $node_id FROM o90Konto WHERE kontoNummer = konti.konto),
     (SELECT $node_id FROM refNummer WHERE refNr = konti.refNr))
FROM
    KontomappingDetaljeret konti
INNER JOIN
    o90Konto ON konti.konto = o90Konto.kontoNummer
INNER JOIN
    refNummer ON konti.refNr = refNummer.refNr

编辑

WHEN运行以下查询时,我得到了这个结果

SELECT TOP (10)
    (SELECT $node_id FROM dbo.o90Konto WHERE kontoNummer = konti.konto) AS KontoNr,
    (SELECT $node_id FROM dbo.refNummer WHERE refNr = konti.refNr) AS refNr

FROM
    KontomappingDetaljeret konti

INNER JOIN
    dbo.o90Konto
ON 
    konti.konto = o90Konto.kontoNummer

INNER JOIN
    dbo.refNummer
ON 
    konti.refNr = refNummer.refNr

结果是

KontoNr refNr
{"type":"node","schema":"dbo","table":"o90Konto","id":180657}   {"type":"node","schema":"dbo","table":"refNummer","id":0}
{"type":"node","schema":"dbo","table":"o90Konto","id":211723}   {"type":"node","schema":"dbo","table":"refNummer","id":1}
{"type":"node","schema":"dbo","table":"o90Konto","id":216400}   {"type":"node","schema":"dbo","table":"refNummer","id":2}
{"type":"node","schema":"dbo","table":"o90Konto","id":205844}   {"type":"node","schema":"dbo","table":"refNummer","id":3}
{"type":"node","schema":"dbo","table":"o90Konto","id":388939}   {"type":"node","schema":"dbo","table":"refNummer","id":4}
{"type":"node","schema":"dbo","table":"o90Konto","id":536204}   {"type":"node","schema":"dbo","table":"refNummer","id":5}
{"type":"node","schema":"dbo","table":"o90Konto","id":130331}   {"type":"node","schema":"dbo","table":"refNummer","id":6}
{"type":"node","schema":"dbo","table":"o90Konto","id":83983}    {"type":"node","schema":"dbo","table":"refNummer","id":7}
{"type":"node","schema":"dbo","table":"o90Konto","id":344522}   {"type":"node","schema":"dbo","table":"refNummer","id":7}
{"type":"node","schema":"dbo","table":"o90Konto","id":523743}   {"type":"node","schema":"dbo","table":"refNummer","id":7}

我只需要一些方法将此结果插入到边表中

答案

终于想通了

INSERT INTO 
    dbo.brugtIRef
    SELECT 
        (SELECT DISTINCT $node_id FROM o90Konto WHERE kontoNummer = konti.konto) AS KontoNr,
        (SELECT DISTINCT $node_id FROM refNummer WHERE refNr = konti.refNr) AS refNr

    FROM
        (SELECT DISTINCT konto, refNr FROM KontomappingDetaljeret) konti

    INNER JOIN
        (SELECT DISTINCT * FROM o90Konto) o90Konto
    ON 
        konti.konto = o90Konto.kontoNummer

    INNER JOIN
        (SELECT DISTINCT * FROM refNummer) refNummer 
    ON 
        konti.refNr = refNummer.refNr

我现在可以针对此图运行查询

以上是关于插入SQL Server 2017中的边缘表的主要内容,如果未能解决你的问题,请参考以下文章

SQL Server 触发器在插入表之前验证数据

SQL Server 从表变量插入临时表

SQL Server 中的历史表

插入前Microsoft SQL Server转换中的存储过程

SQL Server:将日期时间插入表中

SQL Server 存储过程创建临时表并插入值