Your right, using IF in that statement above doesn't work. I know what I want, I just don't know how to get there.
@command is a SELECT statement that either outputs a number or returns and empty set. If @command returns an empty set, I want to output the database name (@DB_Name).
I tried to eliminate the IF statement (below), but that didn't work either.
SELECT @command = 'USE ' +@DB_Name+ '
SELECT * FROM '
+@DB_Name + '.CQ_DBO.schema_version'
EXEC('sp_executesql N''''' +@command+ ' ISNULL PRINT ''' +@DB_Name+ '''''''')