How can I call a function that returns a table and accepts varchar?

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

ALTER FUNCTION [dbo].[GetListFromCSVString] ( @csvString varchar(500) ) RETURNS @ValueList TABLE ( ListValue varchar(50) ) AS begin -- body End 
+6
source share
1 answer
 select ListValue from dbo.GetListFromCSVString('1,2,3') 

Result:

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

If the parameter of your function is a field in another table, you should use cross apply to get a list of values ​​for each row in the original 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 
+7
source

Source: https://habr.com/ru/post/896189/


All Articles