简体   繁体   English

插入内部十字架申请

[英]Insert into inside cross apply

    SELECT DISTINCT CODE
    FROM   T1
    CROSS APPLY 
    (
        INSERT  INTO T4(TEXT1, TEXT2, TEXT3)
        SELECT  T2.TEXT1, T2.TEXT2, T3.TEXT3
        FROM    T2,
                T3
        WHERE   T2.ID = T3.ID
        AND     T2.CODE = T1.CODE
    ) AS T

When using executing this query I get this error: 使用执行此查询时,出现以下错误:

A nested INSERT, UPDATE, DELETE, or MERGE statement must have an OUTPUT clause. 嵌套的INSERT,UPDATE,DELETE或MERGE语句必须具有OUTPUT子句。

What am I doing wrong? 我究竟做错了什么?

EDIT 编辑

What I was planning to achieve was simulate a WHILE..LOOP. 我打算实现的是模拟WHILE..LOOP。 Looping through all the CODEs in T1 and for each CODE get the TEXT fields from T2 and T3 (joining them with the ID) and insert them into table T4. 遍历T1中的所有CODE,并为每个CODE获取T2和T3中的TEXT字段(将其与ID结合在一起)并将其插入表T4中。

I am trying to separate the inserts by CODE because both tables have a large amount of data and I was trying to improve performance (maybe?!) 我试图通过CODE分隔插入,因为两个表都有大量数据,并且我试图提高性能(也许吗?!)

You can't do what you're attempting to do. 您无法做您想做的事。 The language will not allow you. 该语言不允许您使用。 To set the stage, I created the following 3 tables 为了做好准备,我创建了以下3个表

SET NOCOUNT ON;
CREATE TABLE dbo.Final
(
    ID int IDENTITY(1,1) NOT NULL
,   text1 varchar(50) NOT NULL
,   text2 varchar(50) NOT NULL
,   text3 varchar(50) NOT NULL
);

CREATE TABLE dbo.Chain
(
    ID int NOT NULL
);

CREATE TABLE dbo.Chained
(
    ID int NOT NULL
,   Foo int NOT NULL
);

Just to demo where the OUTPUT clause goes, we will insert 4 rows and see the nice pretty INSERTED virtual table and the associated ID values. 为了演示OUTPUT子句的去向,我们将插入4行,并查看漂亮的漂亮INSERTED虚拟表和关联的ID值。

-- Works
    INSERT INTO
        dbo.Final
    (
        text1
    ,   text2
    ,   text3
    )
    OUTPUT
        INSERTED.*
    SELECT
        D.t1
    ,   D.t2
    ,   D.t3    
    FROM
    (
        VALUES
            ('A', 'B', 'C')
        ,   ('D', 'B', 'C')
        ,   ('G', 'B', 'C')
        ,   ('J', 'B', 'C')
    ) D (t1,t2,t3);

Now, if I perform the following statement, it will work fine. 现在,如果我执行以下语句,它将可以正常工作。 However, if I neglect the INSERT just to visually inspect what I want to put into the table, SQL Server will raise the following error 但是,如果我忽略INSERT只是为了直观地检查要放入表中的内容,SQL Server将引发以下错误

A nested INSERT, UPDATE, DELETE, or MERGE statement is not allowed in a SELECT statement that is not the immediate source of rows for an INSERT statement. SELECT语句(不是INSERT语句的直接行源)中不允许嵌套的INSERT,UPDATE,DELETE或MERGE语句。

-- Comment out the insert portion to generate
-- A nested INSERT, UPDATE, DELETE, or MERGE statement is not allowed in a SELECT statement that is not the immediate source of rows for an INSERT statement.
INSERT INTO
    dbo.Chain
(
    ID
)
SELECT
    X.ID
FROM
(
    INSERT INTO
        dbo.Final
    (
        text1
    ,   text2
    ,   text3
    )
    OUTPUT
        INSERTED.*
    SELECT
        D.t1
    ,   D.t2
    ,   D.t3    
    FROM
    (
        VALUES
            ('A', 'B', 'C')
        ,   ('D', 'B', 'C')
        ,   ('G', 'B', 'C')
        ,   ('J', 'B', 'C')
    ) D (t1,t2,t3)
) x

But, you want to go the extra mile and APPLY, or JOIN the results of that virtual table with something else and that's not going to fly. 但是,您想要付出更多努力并应用,或者将虚拟表的结果与其他内容结合起来,那将不会成功。

A nested INSERT, UPDATE, DELETE, or MERGE statement is not allowed on either side of a JOIN or APPLY operator. 在JOIN或APPLY运算符的任何一侧都不允许嵌套INSERT,UPDATE,DELETE或MERGE语句。

I suppose it's just one level of complexity too much. 我想这仅仅是过于复杂的一个层次。

-- Now, try the same bit except we use the derived table as a JOIN/APPLY
-- Can't fix what's not supported 
-- A nested INSERT, UPDATE, DELETE, or MERGE statement is not allowed on either side of a JOIN or APPLY operator.
INSERT INTO
    dbo.Chained
(
    ID
,   Foo
)
SELECT
    X.ID
,   D.foo
FROM
(
    VALUES
    (1)
) D(Foo)
CROSS APPLY
(
    INSERT INTO
        dbo.Final
    (
        text1
    ,   text2
    ,   text3
    )
    OUTPUT
        INSERTED.*
    SELECT
        D.t1
    ,   D.t2
    ,   D.t3    
    FROM
    (
        VALUES
            ('A', 'B', 'C')
        ,   ('D', 'B', 'C')
        ,   ('G', 'B', 'C')
        ,   ('J', 'B', 'C')
    ) D (t1,t2,t3)
) x;

If you really need something like that, then you'll have to break it out into separate statements. 如果您确实需要类似的内容,则必须将其分解为单独的语句。

Kind of a guess 有点猜测

INSERT  INTO FINAL (TEXT1   , TEXT2   , TEXT3)
SELECT  DISTINCT    T2.TEXT1, T2.TEXT2, T3.TEXT3
  FROM  T1
  JOIN  T2
   on   T2.ID = T1.ID
  JOIN  T3
    on  T3.VALUE = T2.VALUE 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM