I'd like to do the following in T-SQL:
EXEC('... ' + (SELECT ...))
A simple concrete example would be
EXEC('DROP TABLE ' + (SELECT name FROM sys.tables WHERE ...))
(Obviously, the WHERE clause is chosen such that the subquery always returns exactly one value.) When I try this, I get an error:
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '('.
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near ')'.
I know that I can work around this by using variables, e.g.:
DECLARE @sql varchar(MAX)
SELECT @sql = 'DROP TABLE ' + name FROM sys.tables WHERE ...
EXEC(@sql)
but for various reasons I'd like to have only one statement. Is that possible?
PS: In case it's relevant, that's the dynamic SQL code I'm trying to squeeze into one statement: