1

早上好。

我正在使用先前查询的结果填充多对多表,如下所示...

WITH Temp AS
(
    SELECT      UserID, Result 
    FROM        <Master_Table>
    WHERE       UserID IN (SELECT Name FROM Users)
)   
INSERT INTO UserDecisions 
    (User_Id, Decision_Id)
    VALUES
    (
        (SELECT Id FROM Users WHERE Temp.UserID = Users.Name),
        (SELECT Id FROM Decisions WHERE Temp.Result = Decisions.Name)
    )

在执行时,我收到如下错误。

Msg 4104, Level 16, State 1, Line 22
The multi-part identifier "Temp.UserID" could not be bound.
Msg 4104, Level 16, State 1, Line 23
The multi-part identifier "Temp.Result" could not be bound.

请问你会如何解决这个问题?Temp 子查询是否需要是临时表?

4

1 回答 1

3

你需要把它写成一个SELECT而不是一个VALUES子句:

WITH Temp AS
(
    SELECT      UserID, Result 
    FROM        <Master_Table>
    WHERE       UserID IN (SELECT Name FROM Users)
)   
INSERT INTO UserDecisions 
    (User_Id, Decision_Id)
    SELECT u.Id,d.Id
    FROM Temp t
    INNER JOIN Users u ON t.UserID = u.Name
    INNER JOIN Decisions d ON t.Result = d.Name
于 2013-04-19T07:31:49.787 回答