Question

I have a Transact SQL function SimpleSplit, which splits a string according to the delimiter. I can use it as follows:

 DECLARE @DelimitedString NVARCHAR(128)
 SET @DelimitedString = 'Processor,RAM,SSD,Ethernet'
 SELECT * FROM [dbo].[SimpleSplit](@DelimitedString, ',')

This results in:

Processor
RAM
SSD
Ethernet

As expected.

I now have a Table called PROD_TABLE with a column Descr. I would like to apply this function to each value in column Descr. I attempted the following and it does not work:

SELECT p.[ID], p.[Descr]
FROM [Amazon].[dbo].[PROD_TABLE] p
OUTER APPLY [dbo].[SimpleSplit](p.Descr, '-') d

In the output I only see the ID and the Descr Columns i.e. no results from the SimpleSplit function. However, if I attempt

SELECT *
FROM [Amazon].[dbo].[PROD_TABLE] p
OUTER APPLY [dbo].[SimpleSplit](p.Descr, '-') d

I see the results of the SimpleSplit function in the last column. Why does this query apply the function, but the previous query does not?

Answer

Thanks to mr.Rebands answer below, I realized that I needed to name the results. Hence * worked, but to explicitly name the columns I needed to do something like:

SELECT p.[ID], p.[Descr], d.[Data]
FROM [Amazon].[dbo].[PROD_TABLE] p
OUTER APPLY [dbo].[SimpleSplit](p.[Descr], '-') d
Was it helpful?

Solution

Your function returns a table - what is the column name of the SimpleSplit result table? You will have to include that column name in your select statement.

OUTER APPLY is applied but the results are not selected.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top