I know I am overthinking this, but I've been banging against this for too long so I'm reaching out for help.
This is the statement I'm trying to run: SELECT @cntMax = MAX(id) FROM [Raw_Item-FieldReport]
BUT, the table name is a variable @reportTable
This doesn't work:
SET @sql = 'SELECT @cntMax = MAX(id) FROM @reportTable'
EXEC sp_executesql @sql
I even tried having the actual table name in the SET @sql and that doesn't work either.
I didn't think it would be this difficult, please tell me I'm missing something easy/obvious.
Here's the full bit of code for those who want it:
DECLARE
@inTable nvarchar(255) = 'Raw_Item',
@reportTable nvarchar(255),
@fieldName nvarchar(255),
@cnt int,
@cntMax int,
@sql nvarchar(max)
SET @reportTable = @inTable + '-FieldReport'
SET @cnt = 1
SELECT @cntMax = MAX(id) FROM [Raw_Item-FieldReport]
PRINT @cntMax
SET @cntMax = 0
SET @sql = 'SELECT @cntMax = MAX(id) FROM [Raw_Item-FieldReport]'
EXEC sp_executesql @sql
PRINT @cntMax
SQL Server 12.0.2008.8 (on Azure)