DECLARE @providerIdList varchar(400)
DECLARE @q varchar(400)
SELECT @q = ''
SELECT @providerIdList = '(1, 5, 15)'
SET @q = 'SELECT u.Id FROM [user] u
LEFT JOIN Provider p ON u.Provider_FK = p.Id
LEFT JOIN Providers2Users pu ON pu.user_FK = u.Id
LEFT JOIN Provider ap ON ap.Id = pu.provider_fk
WHERE p.Id IN ' + @providerIdList
exec @q
Here is the exception that I am getting when I tried to execute query shown above
Msg 203, Level 16, State 2, Line 18
The name 'SELECT u.Id FROM [user] u
LEFT JOIN Provider p ON u.Provider_FK = p.Id
LEFT JOIN Providers2Users pu ON pu.user_FK = u.Id
LEFT JOIN Provider ap ON ap.Id = pu.provider_fk
WHERE p.Id IN (1, 5, 15)' is not a valid identifier.
I would really appreciate if somebody can point me the cause of these errors