Technical Article

Create CSV from column (SQL2K only)

,

This is a bummed version of many of the "csv creating" scripts I've seen.  No need to use a temp table or loop; the string can be built with one select statement (Select @String =  isnull(@String + ',','') + Column From Table)

if object_ID('sp_ReturnCSV') is null drop procsp_ReturnCSV  

create PROCEDURE sp_ReturnCSV  
@ColumnName varchar(25), @TableName varchar(8000)  OUTPUT 

AS  


BEGIN 

   DECLARE @newSQL nvarchar(4000)
Set @NewSQL = 
'declare @SQL varchar(8000)
Select @SQL =  isnull(@SQL + '','','''') + ' + @ColumnName + ' From ' + @TableName + ' (nolock)

select @SQL'

exec( @NewSQL)


   

END



exec sp_ReturnCSV @ColumnName = 'au_id', @TableName = 'pubs.dbo.authors'

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating