0

这可能吗?

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

首先,它将根据 SELECT 中的行数执行 INSERT

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

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

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

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

4

5 回答 5

3

你至少有两个选择:

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

2) 或者您可以编写可组合的 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
于 2012-04-27T22:04:57.740 回答
2
INSERT INTO TABLE2 (xID, NAME)
OUTPUT
INSERTED.xID, INSERTED.NAME, INSERTED.ID
INTO TABLE3 (xID, NAME, ID)
SELECT xID, NAME FROM [TABLE]
于 2012-04-27T21:49:14.747 回答
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
于 2012-04-27T22:08:31.403 回答
0

好的,根据您在下面的评论,试试这个:

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

  INSERT INTO TABLE3 (xID, NAME, ID)
  SELECT xID, NAME, @@identity
  FROM TABLE2;
于 2012-04-27T21:33:47.193 回答
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
于 2012-04-27T22:01:15.013 回答