In the example you posted, it would be like creating a subtable within each row of the main table. It seems to me that this is not possible in SQL Server. The way you built the main code, the result of the subconsulta will have to return a single expression.
If there is no line number limit, create a function of type inline Valued-table. That is, the return of the function becomes a table. In the main SELECT command, trigger the function in the FROM clause, by merging with the table. If necessary, you can use APPLY, when for each row of the User table the function will be triggered once.
-- código #1
CREATE FUNCTION dbo.RetonarSubselect()
returns table as
return SELECT lista_de_colunas
from Produto
go
With JOIN, we can have:
-- código #2
SELECT U.lista_de_colunas, SS.lista_de_colunas
from Usuário as U
left join dbo.RetonarSubselect() as SS on U.coluna = SS.coluna;
With APPLY, we can have:
-- código #3
SELECT U.lista_de_colunas, SS.lista_de_colunas
from Usuário as U
cross apply dbo.RetonarSubselect(parâmetros) as SS;
where parameter is usually passed to the function, almost always column(s) of some table that is part of the FROM clause.
Do you say a user-defined Function of SQL itself? Not that I know, just as string itself.
– bfavaretto
With dynamic SQL it should be possible. If you put the function definition here it might help.
– bruno
You can return
TABLE
(in which case the query composing subselect is executed before the return).– bfavaretto
In that case I wouldn’t answer I’ll have to think of another solution to this.
– Al Unser Albuquerque
Aff! Just now I saw that the question is 2015! But here she appeared as recent...
– José Diz