I have following function which accepts the CSV & delimeter & splits it
ALTER FUNCTION [dbo].[FN_Split] (@String varchar(max), @Delimiter char(1))
First, make sure you did run the create script in the correct database.
Second, as @astander started mentioning, you are using the function results incorrectly.
Your function returns a table, not a value. You'll need to execute the function as part of your sql statements, not during your build of ad hoc queries. For example, this code:
set @sql=@sql+'UNION select items from'+ dbo.FN_Split(@p_SourceText,
@p_Delimeter)+' where orderId ='+@i+')'
would become:
set @sql = @sql+'UNION select items from dbo.FN_Split(' + @p_SourceText +', ' +
@p_Delimeter + ') where orderId =' + @i + ')'
Make similar changes everywhere you are currently referencing the function.