silent_hunter
silent_hunter

Reputation: 2508

Join exiting table with table variable

I try to made inner join between table which exist into database called like 'Location' and also table variable called 'Locations'. You can see code below:

# CODE
    DECLARE @Locations TABLE (CityId int)
    INSERT INTO @Locations  values ('5'), ('2')
    SELECT *
    from @Locations

So first I create table variable with code above. But I have problem when I want to make inner join with other table called like 'Location'. Column CityId is have some value in integer format.In order to do this I try with this code:

DECLARE @Locations TABLE (CityId int)
INSERT INTO @Locations  values ('5'), ('2')
SELECT lo.CityId
FROM dbo.Location
INNER JOIN dbo.Location lo ON lo.CityId = @Locations
GO

But unfortunately this code is not work well.So can anybody help ho to fix this code with properly sintax and resolve this problem?

Upvotes: 0

Views: 22

Answers (1)

SR25
SR25

Reputation: 81

The way you had written inner join is wrong. It should be like this. Try this out

DECLARE @Locations TABLE (CityId int)
INSERT INTO @Locations  values ('5'), ('2')
SELECT lo.CityId
FROM dbo.Location a
INNER JOIN @location lo ON lo.CityId = a.cityid
GO

Upvotes: 1

Related Questions