从Select中插入另一个Insert的标识?

时间:2012-04-27 21:31:16

标签: sql sql-server sql-server-2008 sql-server-2005

这可能吗?

这是我正在寻找的东西,一起执行:

首先,它将根据SELECT

中的行数执行INSERT
  INSERT INTO TABLE2 (xID, NAME)
  SELECT xID, NAME FROM TABLE

然后获取每个INSERTED ROW的@@ IDENTITY,它将创建一个包含第一个SELECT语句的相同数据的新Insert:

  INSERT INTO TABLE3 (xID, NAME, ID)
  SELECT xID, NAME, ID as Scope_IdentitY()

如果没有,没有使用光标的最佳方法是什么?

5 个答案:

答案 0 :(得分:3)

至少有两种选择:

1)OUTPUT...INTO target_table子句(SQL2005 +)

2)或者你可以写composable DML(SQL2008 +)。

示例:

DECLARE @Table2 TABLE(
    ID  INT IDENTITY PRIMARY KEY, --IDENTITY 
    xID INT NOT NULL,
    NAME VARCHAR(25) NOT NULL
);

DECLARE @Table3 TABLE(
    ID  INT PRIMARY KEY, --No IDENTITY 
    xID INT NOT NULL,
    NAME VARCHAR(25) NOT NULL
);

--First solution: OUTPUT ... INTO
INSERT  INTO @Table2 (xID, NAME)
OUTPUT  inserted.xID, inserted.NAME, inserted.ID INTO @Table3(xID, NAME, ID)
SELECT  t.Col1, t.Col2
FROM    (SELECT 11,'A' UNION ALL SELECT 22,'B' UNION ALL SELECT 33,'C') AS t(Col1,Col2);

--Second solution: composable DML
INSERT  INTO @Table3(xID, NAME, ID)
SELECT  src.xID, src.NAME, src.ID
FROM
(
        INSERT  INTO @Table2 (xID, NAME)
        OUTPUT  inserted.xID, inserted.NAME, inserted.ID 
        SELECT  t.Col1, t.Col2
        FROM    (VALUES(44,'D'),(55,'E'),(66,'F')) AS t(Col1,Col2)
) src

SELECT * FROM @Table2 
SELECT * FROM @Table3

答案 1 :(得分:2)

INSERT INTO TABLE2 (xID, NAME)
OUTPUT
INSERTED.xID, INSERTED.NAME, INSERTED.ID
INTO TABLE3 (xID, NAME, ID)
SELECT xID, NAME FROM [TABLE]

答案 2 :(得分:1)

您可以声明一个表变量并将插入 dbo.Table2 中的行的输出存储在此变量中,并使用表变量作为表 dbo.Table3

CREATE TABLE dbo.Table1
(
        xid     int NOT NULL
    ,   name    varchar(30) NOT NULL
);

CREATE TABLE dbo.Table2
(
        id      int NOT NULL IDENTITY
    ,   xid     int NOT NULL
    ,   name    varchar(30) NOT NULL
);

CREATE TABLE dbo.Table3
(
        id      int NOT NULL
    ,   xid     int NOT NULL
    ,   name    varchar(30) NOT NULL
);

INSERT INTO dbo.Table1 (xid, name) VALUES
    (195, 'abc'),
    (242, 'def'),
    (332, 'ghi');
GO

DECLARE @tempTable table
    (       id      int
        ,   xid     int
        ,   name    varchar(30)
    );

INSERT dbo.Table2
    OUTPUT INSERTED.id, INSERTED.xid, INSERTED.name
        INTO @tempTable
        SELECT xid, name FROM dbo.Table1;

INSERT dbo.Table3 (id, xid, name) 
    SELECT id, xid, name FROM @tempTable;

SELECT id, xid, name FROM dbo.Table2;
SELECT id, xid, name FROM dbo.Table3;

GO

答案 3 :(得分:0)

好的,根据您的评论,请尝试以下方法:

  INSERT INTO TABLE2 (xID, NAME)
  SELECT xID, NAME FROM TABLE;

  INSERT INTO TABLE3 (xID, NAME, ID)
  SELECT xID, NAME, @@identity
  FROM TABLE2;

答案 4 :(得分:0)

假设这些表结构:

TABLE_A
-----------
X_ID
NAME

TABLE_B
----------------
TABLE_B_ID [PK]
X_ID
NAME

TABLE_C
----------------
TABLE_C_ID [PK]
X_ID
NAME
TABLE_B_ID [FK]

然后这不会起作用(最好在交易中)?:

-- Grab data from TABLE_A and INSERT INTO TABLE_B
INSERT INTO TABLE_B (
    X_ID,
    NAME
)
SELECT
    X_ID,
    NAME
FROM
    TABLE_A

-- Grab data from TABLE_B that matches the data imported from TABLE_A
-- and INSERT that data into TABLE_C (incl. the PK from TABLE_B)

INSERT INTO TABLE_C (
    X_ID,
    NAME,
    TABLE_B_ID
)
SELECT 
    b.X_ID,
    b.NAME,
    b.TABLE_B_ID
FROM
    TABLE_B b
INNER JOIN 
    TABLE_A a ON a.X_ID = b.X_ID