Virtual tables / Table data type and joins

  • Hi Folks

    If I create two table data type variables, can one still do union queries on them? This doesn't seem to work.

    Error = Server: Msg 170, Level 15, State 1, Line 17

    Line 17: Incorrect syntax near '.'.

    Declare @Table_1 Table (Member varchar(20), Age int)

    Declare @Table_2 Table (Member varchar(20), Score int)

    Insert into @Table_1 (Member, Age) values ('John', '21')

    Insert into @Table_1 (Member, Age) values ('Bruce', '23')

    Insert into @Table_2 (Member, Score) values ('Bruce', '675')

    Insert into @Table_2 (Member, Score) values ('John', '500')

     

    Select

                @Table_1.Member,

                @Table_1.Age,

                @Table_2.Score

    FROM

                @Table_1 INNER JOIN @Table_2 

                ON @Table_1.Member = @Table_2.Member

    Any ideas?

  • Please put an alias to your tables and then it should work fine.

    Something like this

     

    Declare @Table_1 Table (Member varchar(20), Age int)

    Declare @Table_2 Table (Member varchar(20), Score int)

    Insert into @Table_1 (Member, Age) values ('John', '21')

    Insert into @Table_1 (Member, Age) values ('Bruce', '23')

    Insert into @Table_2 (Member, Score) values ('Bruce', '675')

    Insert into @Table_2 (Member, Score) values ('John', '500')

     

    Select

                a.Member,

                a.Age,

               b.Score

    FROM

                @Table_1  a INNER JOIN @Table_2 b

                ON a.Member = b.Member

    Hope this solves the problem

     

    Regards

    Meghana

     


    Regards,

    Meghana

  • Thanks Meghana. I don't know why using aliases works, but it works great

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply