I'm trying to write a dynamic query that produces the same results as the following, but replaces the fixed tablename with a variable.
SELECT *
WHERE tableName = 'Table2A'
works fine but
DECLARE @tablename AS NVARCHAR(100)
SET @tablename = N'Table2A'
DECLARE @execquery AS NVARCHAR(MAX)
SET @execquery = N'
SELECT *
WHERE tableName = ''' + QUOTENAME(@tablename) + N''''
EXECUTE sp_executesql @execquery
returns no records. What am I doing wrong?