Tuesday, February 28, 2012

comma delimited lists in sql server

once upon a time I did this with cursors (cursors, cursors I say!)

  this is easier:
ALTER PROCEDURE [dbo].[getCommaDelim]
AS
BEGIN
      -- SET NOCOUNT ON added to prevent extra result sets from
      -- interfering with SELECT statements.
      SET NOCOUNT ON;
    declare  @list varchar(1000)
set @list = ''
   
      SELECT @list =@list + name + ',' from customers
select @list
END

if the @list is not set thusly:
set @list = ''
then the result will be null

No comments:

Post a Comment