加入两个临时表

时间:2013-01-10 14:23:25

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

我想加入两个临时表。

以下是我的表格:

CREATE TABLE #Result ( Process varchar(50), SuccessCount int, FailureCount int)
CREATE TABLE #SuccessResult ( Process varchar(50), SuccessCount int)
CREATE TABLE #FailureResult ( Process varchar(50), FailureCount int)

我的表中有一些数据,这是我的查询:

INSERT INTO #Result (Process, SuccessCount, FailureCount)
SELECT s.Process,s.SuccessCount,f.FailureCount
FROM #SuccessResult s, #FailureResult as f
INNER JOIN #SuccessResult on f.Process = #SuccessResult.Process

我收到了这个错误:

  

无法找到多部分标识符f.Process

我正在使用Microsoft SQL Server 2008。

我做错了什么?

3 个答案:

答案 0 :(得分:4)

尝试这种方式:

INSERT INTO #Result (Process, SuccessCount, FailureCount)
SELECT s.Process,s.SuccessCount,f.FailureCount
FROM #SuccessResult s 
INNER JOIN #FailureResult f on f.Process = #SuccessResult.Process

答案 1 :(得分:3)

这是INSERT INTO ... SELECT ...的正确语法:

INSERT INTO #Result (Process, SuccessCount, FailureCount)
SELECT s.Process, s.SuccessCount, f.FailureCount
FROM #FailureResult as f
INNER JOIN #SuccessResult s on f.Process = s.Process

答案 2 :(得分:2)

取出隐式联接。

INSERT INTO #Result (Process, SuccessCount, FailureCount)
SELECT s.Process,s.SuccessCount,f.FailureCount
FROM #SuccessResult s
INNER JOIN #FailureResult f on f.Process = s.Process