0

I'm using this method (How to use a variable for the database name in T-SQL?) to pass a DBname into a variable. This variable is then used with something like this (simplified):

Select column From @DBname

However, when executed...You get, Command(s) completed successfully

Is there a way to adapt this method so that you can see the results of a query? (Not just create or alter a DB)

Community
  • 1
  • 1
Zee
  • 1,780
  • 3
  • 16
  • 27

1 Answers1

0

Something like this should work for you.

[[put code here that sets up @DBname]]
DECLARE @stmt NVARCHAR(MAX) = 'SELECT column FROM ' + @DBname;
EXEC sp_ExecuteSQL @stmt;

Edit: Note that I'm assuming you're using SQL server (you didn't specify in the question).

ean5533
  • 8,884
  • 3
  • 40
  • 64