开发者

How would I call a function which returns a table and accepts a varchar?

开发者 https://www.devze.com 2023-03-31 21:30 出处:网络
I have a function I need to use, I\'m passing in a var char and I insert records into a table called using @ValueList. However I\'m not sure how to call / use this function ?

I have a function I need to use, I'm passing in a var char and I insert records into a table called using @ValueList. However I'm not sure how to call / use this function ?

开发者_如何学C
ALTER FUNCTION [dbo].[GetListFromCSVString]
(            
      @csvString varchar(500)
)
RETURNS @ValueList TABLE 
(
      ListValue varchar(50)
)
AS
begin
      -- body

End


select ListValue
from dbo.GetListFromCSVString('1,2,3')

Result:

ListValue
----------
1
2
3

If the parameter to your function is a field in another table you have to use cross apply get the list of values for each row in the source table.

-- Table to test on
declare @T table
(
  ID int identity primary key, 
  SomeColumn varchar(500)
)

-- Sample data
insert into @T values('1,2,3')
insert into @T values('a,b,c')

-- Use the function
select ST.ID,
       GL.ListValue
from @T as ST
  cross apply dbo.GetListFromCSVString(ST.SomeColumn) as GL

Result:

ID          ListValue
----------- ----------
1           1
1           2
1           3
2           a
2           b
2           c
0

精彩评论

暂无评论...
验证码 换一张
取 消