La subconsulta devolvió más de 1 valor: intentaba imprimir nombres de bases de datos

I am trying to find and display a list of database names that are in anything but an ideal mirrored state. The below code works when one database is in such a state, but when more than that is errored I get the error this question refers to: Msg 512, nivel 16, estado 1, línea 4 La subconsulta devolvió más de 1 valor. Esto no está permitido cuando la subconsulta sigue =,! =, <, <=,>,> = O cuando la subconsulta se usa como expresión.

Aquí está mi código:

declare @result int declare @dbs varchar(256)

SET @result = (SELECT database_id from sys.database_mirroring WHERE mirroring_state_desc in ('SUSPENDED','DISCONNECTED','PENDING_FAILOVER'))
SET @dbs = (SELECT name from sys.databases where database_id = @result)

if @@ROWCOUNT > 0

print @dbs

Thank you very much any assistance,

Puntilla.

preguntado el 13 de septiembre de 13 a las 11:09

The error is pretty obvious. What is your question? -

Since , you are trying to store a collection of values into a single variable..you are getting that error -

I should've been more specific but yes, I understand what the error says, I was asking for a way to get around it.. I thought that was implied but apologies if not. -

2 Respuestas

Realmente necesitas print ? Maybe try to display list of db that way:

SELECT name 
from sys.databases 
where database_id in (SELECT database_id 
                      from sys.database_mirroring 
                      WHERE mirroring_state_desc in ('SUSPENDED','DISCONNECTED','PENDING_FAILOVER')
                     )

Respondido el 13 de Septiembre de 13 a las 11:09

@MenthoL I'm glad I could help you :) - Robert

@Parado niceunderstanding - Amit Singh

@AmitSingh Really Thanks! - Robert

Por favor intente esto:

select database_id into #temp1 from sys.database_mirroring WHERE WHERE mirroring_state_desc in ('SUSPENDED','DISCONNECTED','PENDING_FAILOVER'))

SELECT name from sys.databases where database_id in #temp1

Where #temp1 is your temporary table

Respondido el 13 de Septiembre de 13 a las 11:09

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