It looks like you would need "dynamic SQL". This is nothing more than using SQL to generate the SQL code you want to execute. There is no way to have a variable table name in a SQL statement. You want something along the lines of:declare @dynSql varchar(max) = '';select @dynSql = @dynSql + 'select ' + tableID + ', ' + tableName + ', (select max(recordID) from ' + tableName + ');' + char(13) + char(10)from listOfTables;print @dynSql -- Debugging: Comment this line out if you are happy with the resultsexec sp_executesql @dynSql;'
Too often we enjoy the comfort of opinion without the discomfort of thought. - John F. Kennedy