2011-06-09 40 views
3

可能重複:
T-SQL: How to join @variable tables (another try)T-SQL:如何加入@variable表

第一:我使用的是SQL Server 2008的 在一個複雜的算法,它涉及到很多的數據,我一直在使用創建中間表變量的技術:

DECLARE @table AS TABLE (Col1 INT, Col2 VARCHAR(100)) 

Unfortu最近,SQL Server不支持JOINning @variable表,它只允許連接數據庫中的「真」表。

我能做到 「手動」 加入,像

FROM @table1 t1, @table2 t2 
WHERE t1.Id = t2.Id 

這導致INNER JOIN,但這是錯誤的我。問題是:如何做FULL JOIN兩個@variable表?

+4

因爲至少3人不同意您的「SQL Server不支持......」,也許你可以發佈你試過,你收到此錯誤信息,我們也許能夠提供更多的啓發。 – 2011-06-09 13:26:49

回答

2

你應該能夠做一個加入使用@tableVariable

SELECT * 
FROM table1 t 
FULL JOIN @tableVariable tv 
ON (tv.col = cnc.col) 

莫非有什麼與你的兼容性設置? (我的是100)

sp_dbcmptlevel 'database_name' 

ALTER DATABASE database_name 
    SET COMPATIBILITY_LEVEL = { 80 | 90 | 100 } 
10

你是什麼意思由SQL不支持聯接表變量?

它爲我

DECLARE @table1 AS TABLE (Col1 INT, Col2 VARCHAR(100)) 
DECLARE @table2 AS TABLE (Col1 INT, Col2 VARCHAR(100)) 

SELECT * 
FROM @table1 t1 
FULL JOIN @table2 t2 on t1.Col1 = t2.Col1 
2

我不知道你問什麼,加入的作品就好了表變量。看到這個例子:

declare @table as table (Col1 int, Col2 varchar(100)) 
declare @table2 as table (Col1 int, Col2 varchar(100)) 

insert into @table 
select 1, 'A' 
union all 
select 1, 'C' 
union all 
select 1, 'D' 

insert into @table2 
select 2, 'A' 
union all 
select 2, 'B' 
union all 
select 2, 'D' 
union all 
select 2, 'E' 

select 
    * 
from 
    @table t1 full outer join 
    @table2 t2 on t1.Col2 = t2.Col2 

select 
    * 
from 
    @table t1 left join 
    @table2 t2 on t1.Col2 = t2.Col2 

select 
    * 
from 
    @table t1 right join 
    @table2 t2 on t1.Col2 = t2.Col2 

select 
    * 
from 
    @table t1 join 
    @table2 t2 on t1.Col2 = t2.Col2