I have a function dbo.getMyTableBasedOnId() that returns a different cache table names and I want to assign it to a variable to use in a SELECT statement with multiple JOINs. I'm not sure how to set the result of this function to a variable and then use WITH and MERGE INTO (if that is the correct path) instead of JOINs. My attempt below did not work.
DECLARE @MyTableName varchar(50)
SET @MyTableName = (SELECT dbo.getMyTableBasedOnId(123))
SELECT @MyTableName
SELECT i.personName, a.weight
FROM peopleTable1 i
JOIN (select [id], [weight] from @MyTableName) as a
ON a.personName = i.personName
WHERE i.City IN ('New York', 'Atlanta')
Thanks in advance for any help.