DROP TABLE TABLE1
GO
CREATE TABLE TABLE1
( ID INT NULL,
PID INT NULL
)
GO
INSERT INTO TABLE1 VALUES(1,1)
INSERT INTO TABLE1 VALUES(1,2)
INSERT INTO TABLE1 VALUES(1,3)
INSERT INTO TABLE1 VALUES(2,4)
INSERT INTO TABLE1 VALUES(2,5)
INSERT INTO TABLE1 VALUES(2,6)
INSERT INTO TABLE1 VALUES(3,7)
INSERT INTO TABLE1 VALUES(3,8)
GO
DROP FUNCTION FMERG
GO
CREATE FUNCTION FMERG(@ID INT)
RETURNS VARCHAR(8000)
AS
BEGIN
DECLARE @STR VARCHAR(8000)
SET @STR=''
SELECT @STR=@STR+','+CAST(PID AS VARCHAR) FROM TABLE1 WHERE ID=@ID
SET @STR=RIGHT(@STR,LEN(@STR)-1)
RETURN(@STR)
END
GO
SELECT DISTINCT ID,DBO.FMERG(ID) FROM TABLE1