Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
ThePrisoner
Starting Member
18 Posts |
Posted - 2007-03-28 : 09:00:07
|
| helloI need a UDF similar to the following/****** Object: UserDefinedFunction [dbo].[fn_MVParam] Script Date: 03/28/2007 14:46:16 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOALTER FUNCTION [dbo].[fn_MVParam](@RepParam nvarchar(4000), @Delim char(1)= ',')RETURNS @VALUES TABLE (Parame nvarchar(4000))AS BEGIN DECLARE @chrind INT DECLARE @Piece nvarchar(4000) SELECT @chrind = 1 WHILE @chrind > 0 BEGIN SELECT @chrind = CHARINDEX(@Delim,@RepParam) IF @chrind > 0 SELECT @Piece = LEFT(@RepParam,@chrind - 1) ELSE SELECT @Piece = @RepParam INSERT @VALUES(Parame) VALUES(@Piece) SELECT @RepParam = RIGHT(@RepParam,LEN(@RepParam) - @chrind) IF LEN(@RepParam) = 0 BREAK END RETURNENDbut I would need it to use in an ORDER BY clause, better explained in this topichttp://www.sqlteam.com/forums/topic.asp?TOPIC_ID=6055 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-03-28 : 09:12:37
|
| There are many CSV split functions here. Even more efficient.Add another table variable. Store parts in there. When loop is done, insert from table variable to result table variable, SORTED.Peter LarssonHelsingborg, Sweden |
 |
|
|
ThePrisoner
Starting Member
18 Posts |
Posted - 2007-03-28 : 10:05:14
|
| yeah I figured it out by ading an identity columncreate FUNCTION [dbo].[fn_MVParamSorted](@RepParam nvarchar(4000), @Delim char(1)= ',')RETURNS @VALUES TABLE (SortID int identity,Parame nvarchar(4000))AS BEGIN DECLARE @chrind INT DECLARE @Piece nvarchar(4000) SELECT @chrind = 1 WHILE @chrind > 0 BEGIN SELECT @chrind = CHARINDEX(@Delim,@RepParam) IF @chrind > 0 SELECT @Piece = LEFT(@RepParam,@chrind - 1) ELSE SELECT @Piece = @RepParam INSERT @VALUES(Parame) VALUES(@Piece) SELECT @RepParam = RIGHT(@RepParam,LEN(@RepParam) - @chrind) IF LEN(@RepParam) = 0 BREAK END RETURNENDthen in my queryinner join dbo.fn_MVParamSorted(@stringtoorderby,',')on foo.product = fn_MVParamSorted.parameGROUP BY fn_MVParamSorted.parame,fn_MVParamSorted.sortidorder by fn_MVParamSorted.sortid |
 |
|
|
|
|
|
|
|