How to execute Table valued function

SqlSql ServerSql Server-2008Tsql

Sql Problem Overview


I have following function which returns Table .

create Function FN(@Str varchar(30))
  returns
  @Names table(name varchar(25))
  as 
  begin 
  
      while (charindex(',', @str) > 0)
      begin
      insert into @Names values(substring(@str, 1, charindex(',', @str) - 1))
     set  @str = substring(@str, charindex(',', @str) + 1, 100)  
      end
      insert into @Names values(@str)  
      
      return
  end

Could any one please explain me how to run this function.

Sql Solutions


Solution 1 - Sql

A TVF (table-valued function) is supposed to be SELECTed FROM. Try this:

select * from FN('myFunc')

Solution 2 - Sql

You can execute it just as you select a table using SELECT clause. In addition you can provide parameters within parentheses.

Try with below syntax:

SELECT * FROM yourFunctionName(parameter1, parameter2)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionShineView Question on Stackoverflow
Solution 1 - SqlPaul CreaseyView Answer on Stackoverflow
Solution 2 - SqlShihamView Answer on Stackoverflow