I am very new to SQL. I want to access a variable dynamically in a select statement.
declare @sql NVARCHAR(MAX)
declare @tableName varchar(100)
set @tableName='xxxx'
set @sql='select * from ' +@tableName+
EXEC sys.sp_executesql @sql
But every time I am executing the above query I am getting an error:
Incorrect syntax near the keyword 'EXEC'.