/dev/null

Elite is stupid. Back to the roots.

October 6, 2007

Comma separated list in SQL

06:42

Just a short example of how to write a stored function that returns a comma separated list of values (Microsoft Transact-SQL):


CREATE FUNCION get_authors (@pubId int)
RETURNS nvarchar(max)
WITH EXECUTE AS CALLER
AS
BEGIN
  DECLARE @authorList nvarchar(max);

  SELECT @authorList = @authorList + ', ' +
    authorFirstName + ' ' + authorLastName
    FROM pdb_authors a, pdb_publicationAuthors b
    WHERE a.authorId = b.authorId AND b.pubId = @pubId;

  RETURN(SUBSTRING(@authorList, 3, LEN(@authorList) - 2))
END

Powered by PHP, Memcached, Suhosin, MySQL and WordPress