具有来自SCOPE_IDENTITY的ID的多个插入

时间:2018-01-24 01:57:10

标签: sql-server tsql

我处理的场景是这样的: 我在内存中创建的临时表中有一堆ID。所以它是这样的:

DECLARE @tempIds AS TABLE
(
   Id INT NOT NULL
)

对于临时表中的每个Id,我需要在表A中创建一个INSERT,获取数据库分配的ID并在表B中输入两个ID。看起来像这样:

DECLARE @CommentsId INT
INSERT INTO TableA
(Comments)
VALUES
('Entering some comments here...')
SET @CommentsId = SCOPE_IDENTITY()

INSERT INTO TableB
(Id, CommentsId)
VALUES
(@Id, @CommentsId)

表B的@Id语句中的INSERT是来自tempIds表的Id。所以,基本上,我在临时表中为每个Id输入表A,然后在表B中进行相应的输入。

我想在不使用游标的情况下这样做。我以前做过这个,但也许我的大脑太累了,无法回忆起这种技术。

2 个答案:

答案 0 :(得分:1)

OUTPUT条款是您所需要的。

设置表A&表B:

--DROP TABLE TableA;
CREATE TABLE TableA
(
    Id INT NOT NULL IDENTITY(1,1),
    Comments VARCHAR(256)
);

--DROP TABLE TableB;
CREATE TABLE TableB
(
    Id INT NOT NULL,
    CommentID INT NOT NULL
);

创建并填充@tempIds(假设您也可以使用您的评论填充此表格):

DECLARE @tempIds AS TABLE
(
  Id INT NOT NULL,
  Comments VARCHAR(256) NOT NULL
);


INSERT INTO @tempIds (Id, Comments)
VALUES
   (1, 'Hello'),
   (2, 'Hi'),
   (3, 'How Are you'),
   (10, 'Goodbye');

因为你想输出一个实际上没有插入TableA的字段(@ tempIds.Id),我们必须把这个INSERT伪装成一个MERGE:

MERGE TableA as [target]
USING
  (SELECT Id
         ,Comments
   FROM @tempIds
  ) AS [source]
ON 1=0 --Always evaluate to false since we're only doing INSERTS
WHEN NOT MATCHED BY TARGET THEN
   INSERT (Comments) VALUES ([source].[Comments])
OUTPUT [source].ID, INSERTED.[Id] --We can OUTPUT source.ID here in a MERGE where we could not in an INSERT
    INTO TableB ([Id],[CommentId]);

SELECT * FROM @tempIds
SELECT * FROM TableA
SELECT * FROM TableB

@tempIds:

Id          Comments
----------- --------
1           Hello
2           Hi
3           How Are you
10          Goodbye

表A:

Id          Comments
----------- --------
1           Hello
2           Hi
3           How Are you
4           Goodbye

表B:

Id          CommentID
----------- -----------
1           1
2           2
3           3
10          4

答案 1 :(得分:0)

使用OUTPUT子句并将标识输出到表变量

    <?xml version="1.0"?>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->

<xs:schema version="1.0"
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           elementFormDefault="qualified">

    <xs:complextype name='Layouttype'>
        <xs:sequence>
            <xs:element name='Encabezado' type='xs:Encabezadotype'> </xs:element>
            <xs:element name='Detalle' type='xs:Detalletype'> </xs:element>
            <xs:element name='PieDePagina' type='xs:PieDePaginatype'> </xs:element>
        </xs:sequence>
    </xs:complextype>

    <xs:complextype name='Encabezadotype'>
        <xs:sequence>
            <xs:element name='Campos1' type='xs:Campos1type'> </xs:element>
            <xs:element name='TipoDatos1' type='xs:TipoDatos1type'> </xs:element>
            <xs:element name='Longitudes1' type='xs:Longitudes1type'> </xs:element>
            <xs:element name='Descripciones1' type='xs:Descripciones1type'> </xs:element>
        </xs:sequence>
    </xs:complextype>

            <xs:complextype name='Campos1type'>
                <xs:sequence>
                    <xs:element name='Campo1' type='string'> </xs:element>
                </xs:sequence>
            </xs:complextype>

            <xs:complextype name='TipoDatos1type'>
                <xs:sequence>
                    <xs:element name='td1' type='string'> </xs:element>
                </xs:sequence>
            </xs:complextype>

            <xs:complextype name='Longitudes1type'>
                <xs:sequence>
                    <xs:element name='Lenght1' type='integer'> </xs:element>
                </xs:sequence>
            </xs:complextype>

            <xs:complextype name='Descripciones1type'>
                <xs:sequence>
                    <xs:element name='Descripcion1' type='string'> </xs:element>
                </xs:sequence>
            </xs:complextype>



    <xs:complextype name='Detalletype'>
        <xs:sequence>
            <xs:element name='Campos2' type='xs:Campos2type'> </xs:element>
            <xs:element name='TipoDatos2' type='xs:TipoDatos2type'> </xs:element>
            <xs:element name='Longitudes2' type='xs:Longitudes2type'> </xs:element>
            <xs:element name='Descripciones2' type='xs:Descripciones2type'> </xs:element>
        </xs:sequence>
    </xs:complextype>

            <xs:complextype name='Campos2type'>
                <xs:sequence>
                    <xs:element name='Campo2' type='string'> </xs:element>
                </xs:sequence>
            </xs:complextype>

            <xs:complextype name='TipoDatos2type'>
                <xs:sequence>
                    <xs:element name='td2' type='string'> </xs:element>
                </xs:sequence>
            </xs:complextype>

            <xs:complextype name='Longitudes2type'>
                <xs:sequence>
                    <xs:element name='Lenght2' type='integer'> </xs:element>
                </xs:sequence>
            </xs:complextype>

            <xs:complextype name='Descripciones2type'>
                <xs:sequence>
                    <xs:element name='Descripcion2' type='string'> </xs:element>
                </xs:sequence>
            </xs:complextype>  



    <xs:complextype name='PieDePaginatype'>
        <xs:sequence>
            <xs:element name='Campos3' type='xs:Campos3type'> </xs:element>
            <xs:element name='TipoDatos3' type='xs:TipoDatos3type'> </xs:element>
            <xs:element name='Longitudes3' type='xs:Longitudes3type'> </xs:element>
            <xs:element name='Descripciones3' type='xs:Descripciones3type'> </xs:element>
        </xs:sequence>
    </xs:complextype>

            <xs:complextype name='Campos3type'>
                <xs:sequence>
                    <xs:element name='Campo3' type='string'> </xs:element>
                </xs:sequence>
            </xs:complextype>

            <xs:complextype name='TipoDatos3type'>
                <xs:sequence>
                    <xs:element name='td3' type='string'> </xs:element>
                </xs:sequence>
            </xs:complextype>

            <xs:complextype name='Longitudes3type'>
                <xs:sequence>
                    <xs:element name='Lenght3' type='integer'> </xs:element>
                </xs:sequence>
            </xs:complextype>

            <xs:complextype name='Descripciones3type'>
                <xs:sequence>
                    <xs:element name='Descripcion3' type='string'> </xs:element>
                </xs:sequence>
            </xs:complextype>                    

</xs:schema>