2
I work with a system that has a very limited report. The reports are basically restricted to one sql query.
I need to present in a report a query that, in my query, is being informed by the user a multi-selection parameter, I will explain better:
The user informs about which units want to view the report, imagine that he selects units 1 and 2, then in the clause IN
will stay:
Where CODIGO_UNIDADE In (1,2)
So far everything perfect, the problem is that I need to present in the report which units the user informed in the parameters, and, the only way I have to do this is by declaring variable in SQL and capturing the parameter that the user informed:
DECLARE @UNIDADE VARCHAR (100)
Set @UNIDADE = (
Select NOME from UNIDADES
Where CODIGO_UNIDADE In (:UNIDADE_INFORMADA_PELO_USUÁRIO)
)
Select @Unidade As 'Unidade Informada'
As you may already be wondering, this cannot be done, because as the subquery returns more than one value an error is returned:
Mensagem 512, Nível 16, Estado 1, Linha 3
A subconsulta retornou mais de 1 valor. Isso não é permitido quando a subconsulta segue um =, !=, <, <= , >, >= ou quando ela é usada como uma expressão.
What is the easiest way to do this? Considering the limitations (passing the value to a variable) I have in my report?
Good afternoon friend, thank you for your reply! This solution did not meet because it was bringing a record for each unit.
– Otávio Augusto
Hello! I don’t think so. The script returns only 1 line, regardless of the number of records returned, since we are concatenating the return. I just tested it again and it’s correct. Can you take a look again? I edited the answer by removing @ from the concatenation UNIT. Maybe this caused some kind of confusion. I hope I helped. D
– rammonzito