如何JOIN表变量

时间:2014-12-19 00:10:51

标签: sql sql-server

这是我的问题:

    DECLARE @T1 TABLE (Id int, Name varchar(max));
    INSERT INTO @T1 VALUES (1, 'A');
    INSERT INTO @T1 VALUES (2, 'B');
    INSERT INTO @T1 VALUES (3, 'C');

    DECLARE @T2 TABLE (Id int, Name varchar(max));
    INSERT INTO @T2 VALUES (1, 'X');
    INSERT INTO @T2 VALUES (2, 'Y');
    INSERT INTO @T2 VALUES (3, 'Z');

    SELECT t1.*, t2.*
    FROM @T1 t1
    JOIN @T2 t2;

在最后一个';'附近出现语法错误。我错过了什么?

2 个答案:

答案 0 :(得分:2)

JOIN @T2 t2 ON t1.SomeColumn = t2.SomeColumn

要进行交叉加入,请使用CROSS JOIN而不是JOIN

答案 1 :(得分:-1)

DECLARE @T1 TABLE (Id int, Name varchar(max));
    INSERT INTO @T1 VALUES (1, 'A');
    INSERT INTO @T1 VALUES (2, 'B');
    INSERT INTO @T1 VALUES (3, 'C');

    DECLARE @T2 TABLE (Id int, Name varchar(max));
    INSERT INTO @T2 VALUES (1, 'X');
    INSERT INTO @T2 VALUES (2, 'Y');
    INSERT INTO @T2 VALUES (3, 'Z');

    SELECT t1.*, t2.*
    FROM @T1 t1
    JOIN @T2 t2 on t1.id=t2.id