Select multiple columns from a subquery

I searched a lot, but I have no chance that several columns will be returned in the subquery at once. The following code works, but it sucks:

SELECT (SELECT Column1 FROM dbo.fnGetItemPath(ib.Id)) AS Col1, (SELECT Column2 FROM dbo.fnGetItemPath(ib.Id)) AS Col2, (SELECT Column3 FROM dbo.fnGetItemPath(ib.Id)) AS Col3 FROM ItemBase ib 

I really have no idea how to pass ib.Id functions and get columns Column1, Column2, Column3 without calling the fnGetItemPath function 3 times.

Thank you in advance

0
source share
2 answers

You can move ti to the "FROM" part and use external apply (or cross apply).

check the syntax yourself, but it should look something like this:

 SELECT Column1, Column2, Column3 FROM ItemBase ib Outer Apply dbo.fnGetItemPath(ib.Id) 
+4
source

Does this work?

 select (select column1, column2, column3 from dbo.fnGetItemPath(ib.Id)) from ItemBase ib 

or do you need something else?

0
source

All Articles