SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


UDF to return a multi column table of values from an input string


UDF to return a multi column table of values from an input string

Author
Message
Andy Siantonas
Andy Siantonas
SSC Rookie
SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)

Group: General Forum Members
Points: 42 Visits: 81
Comments posted to this topic are about the item UDF to return a multi column table of values from an input string
Gargamel
Gargamel
SSC Veteran
SSC Veteran (295 reputation)SSC Veteran (295 reputation)SSC Veteran (295 reputation)SSC Veteran (295 reputation)SSC Veteran (295 reputation)SSC Veteran (295 reputation)SSC Veteran (295 reputation)SSC Veteran (295 reputation)

Group: General Forum Members
Points: 295 Visits: 106
Here is my version:

create procedure ListToMultiColumnTable (@List Varchar(max), @ColumnCount int)
as
Begin

declare @tempList Varchar(max), @tempListInner Varchar(max), @listToInsert nvarchar(max)
Declare @ColumnNumber int,@substringCount int

set @templist=replace(@List,',',''',''')+''','''
While @templist<>''''
Begin
Set @ColumnNumber = 1
set @substringCount=0
set @tempListInner=@tempList
set @listToInsert=''
while @ColumnNumber <= @ColumnCount
begin
declare @ci int
set @ci=charindex(',',@tempListInner)
if @listToInsert<>''
set @listToInsert=@listToInsert+', '
set @listToInsert=@listToInsert+substring(@templistInner,1,@ci-1) +
' col'+convert(nvarchar,@columnnumber)

set @substringCount=@substringCount+@ci
set @tempListInner=substring(@tempListInner,
@ci+1,len(@tempListInner))
Set @ColumnNumber = @ColumnNumber + 1
end

if left(@templist,1)<>''''
begin
execute ('select '''+@listToInsert+ ' into ##tab')
Set @ColumnNumber = 1
while @ColumnNumber <= @ColumnCount
begin
execute('alter table ##tab alter column col'+@ColumnNumber+' nvarchar(max)')
Set @ColumnNumber = @ColumnNumber + 1
end
end
else
execute ('insert into ##tab
select '+@listToInsert)
set @templist=substring(@templist,@substringCount+1,len(@templist))
End
select * from ##tab
drop table ##tab
Return
End
go

--output example
declare @List varchar(1000)
set @List = '1,Tea,£1.50,2,Coffee,£1.75,3,Hot Chocolate,£2.05'
exec ListToMultiColumnTable @list,3


Andy Siantonas
Andy Siantonas
SSC Rookie
SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)

Group: General Forum Members
Points: 42 Visits: 81
That solution has the advantage of allowing you to return as many columns as needed. But I don't think you can execute dynamic sql statements in a udf, which is why I didn't go down that route. I must admit I didn't like limiting the number of columns in the script, but it was all I could come up with in the time available.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search