Usar un nombre de base de datos como variable

I'm using this method (¿Cómo usar una variable para el nombre de la base de datos en 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)

preguntado el 08 de noviembre de 11 a las 17:11

1 Respuestas

Algo como esto debería funcionar para ti.

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

respondido 08 nov., 11:21

This works. I'm new to SQL - Didn't know about sp_ExecuteSQL. Thank you!! - Zee

No problem. Please mark the answer as accepted so that people can easily see the solution. Thanks. - andan5533

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.