2

I have two tables:

tbl_profiles_and_users:

[id] [int] IDENTITY(1,1) NOT NULL,
[profile_id] [int] NOT NULL,
[user_id] [nvarchar](50) NULL

tbl_profiles_and_activities:

[id] [int] IDENTITY(1,1) NOT NULL,
[profile_id] [int] NOT NULL,
[user_id] [nvarchar](50) NOT NULL,
[activity_name] [nvarchar](50) NULL,
[check] [int] NULL,
[preferred] [int] NULL

both tables have entries in them, and I am trying to select the user, and activities, only from within a specified profile.

I am trying to do this with the following join:

use [dbName]

SELECT p1.user_id AS User_ID, [p2.activity_name] as [Activity_Name]
FROM tbl_profiles_and_users AS p1 
INNER JOIN tbl_profiles_and_activities AS p2 
ON p1.user_id = p2.user_id AND p1.profile_id = p2.profile_id

but I receive the message Invalid column name 'p2.activity_name' What am I doing wrong? Can I achieve the desired results with a Join?

1 Answer 1

6

because you are escaping the whole column, thus, the server reads it as [p2.activity_name] as whole column name which is very different from [p2].[activity_name]

SELECT p1.user_id AS User_ID, [p2].[activity_name] as [Activity_Name]
FROM ....
Sign up to request clarification or add additional context in comments.

3 Comments

(Actually a delimited identifier, not escaped.)
Thank you, this was the solution. I will accept your answer when the timer allows it.
@RedFilter yeah you're right. was trying to figure out what the correct term. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.