I need to return data from a database table whose name is in another table. Ex - TTT.best has only one record with the name of the best salesman and his current sales total. Database TTT has a table named for each salesman with the individual sales data. In an Excel query, I want to return data for the best salesman. I created a query like:
I can execute the query in the Microsoft SQL box and get results. But when I return the data to the spreadsheet, I get only ExternalData_1. No runs, drips or errors, but no data. Am I missing something?
Thanks, folks.
Code:
declare @name1 varchar(13)
declare @cmdstr varchar(500)
set @name1 = (select name from TTT.best )
set @cmdstr = 'SELECT [EMAIL="'+@name1+ '.customer"]'+ @name1 + '.customer[/EMAIL],
Count(*) FROM TTT.'+ @name1 + '
group by ' + @name1 + '.customer'
execute(@cmdstr)
Thanks, folks.