Sunday, June 19, 2011

dbo.NameWithCommaa

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go


ALTER FUNCTION [dbo].[NameWithCommaa] (
/*
FUNCTION ListToTable
Usage: select dbo.TableToString('2,13') order by entry desc
PURPOSE: Takes a comma-delimited list as a parameter and returns the values of that list into a table variable.
*/
@mylist varchar(8000)
)
Returns varchar(1000)
as
BEGIN
DECLARE @ListTable TABLE (
seqid int not null,
entry varchar(255) not null)
DECLARE
@this varchar(255),
@rest varchar(8000),
@pos int,
@seqid int

SET @this = ' '
SET @seqid = 1
SET @rest = @mylist
SET @pos = PATINDEX('%,%', @rest)
WHILE (@pos > 0)
BEGIN
set @this=substring(@rest,1,@pos-1)
set @rest=substring(@rest,@pos+1,len(@rest)-@pos)
INSERT INTO @ListTable (seqid,entry) VALUES (@seqid,RTRIM(LTRIM(@this)))
SET @pos= PATINDEX('%,%', @rest)
SET @seqid=@seqid+1
END
set @this=@rest
INSERT INTO @ListTable (seqid,entry) VALUES (@seqid,@this)
-- RETURN
-----------------------------

declare @pages varchar(200)
SELECT @pages = COALESCE(@pages + ', ', '') + convert( varchar(50),Name)FROM
(
select Name from tbl_Rights,

(select entry from listtotable(@mylist)) newtable
where tbl_Rights.ID=cast (newtable.entry as bigint)

) as new

return @pages
END

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More