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)

1 Answer 1

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).

2
  • This works. I'm new to SQL - Didn't know about sp_ExecuteSQL. Thank you!!
    – Zee
    Nov 8, 2011 at 18:44
  • No problem. Please mark the answer as accepted so that people can easily see the solution. Thanks.
    – ean5533
    Nov 8, 2011 at 18:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.