--table to list
CREATE TABLE t1(id int, v varchar(20))
GO
INSERT t1
SELECT 1, 'A'
UNION
SELECT 1, 'B'
UNION
SELECT 1, 'C'
UNION
SELECT 2, 'A'
UNION
SELECT 2, 'C'
UNION
SELECT 3, 'X'
UNION
SELECT 3, 'Y'
UNION
SELECT 3, 'Z'
GO
CREATE FUNCTION typeList(@id int)
RETURNS varchar(20)
AS
BEGIN
DECLARE @vl varchar(20)
SELECT @vl=''
SELECT @vl = @vl + v + ','
FROM t1
WHERE id = @id
IF ( LEN(@vl) > 0 )
SET @vl = LEFT(@vl, LEN(@vl) - 1 )
RETURN (@vl)
END
GO
CREATE VIEW t1_view
AS
SELECT id, dbo.typelist(id) as type_list
FROM t1
GROUP BY id
GO
SELECT * FROM t1_view
GO
DROP VIEW t1_view
DROP FUNCTION typelist
DROP TABLE t1
GO
CREATE TABLE t1(id int, v varchar(20))
GO
INSERT t1
SELECT 1, 'A'
UNION
SELECT 1, 'B'
UNION
SELECT 1, 'C'
UNION
SELECT 2, 'A'
UNION
SELECT 2, 'C'
UNION
SELECT 3, 'X'
UNION
SELECT 3, 'Y'
UNION
SELECT 3, 'Z'
GO
CREATE FUNCTION typeList(@id int)
RETURNS varchar(20)
AS
BEGIN
DECLARE @vl varchar(20)
SELECT @vl=''
SELECT @vl = @vl + v + ','
FROM t1
WHERE id = @id
IF ( LEN(@vl) > 0 )
SET @vl = LEFT(@vl, LEN(@vl) - 1 )
RETURN (@vl)
END
GO
CREATE VIEW t1_view
AS
SELECT id, dbo.typelist(id) as type_list
FROM t1
GROUP BY id
GO
SELECT * FROM t1_view
GO
DROP VIEW t1_view
DROP FUNCTION typelist
DROP TABLE t1
GO