SQL Agent Job - .csv file - needs headers

Unanswered SQL Agent Job - .csv file - needs headers

  • giovedì 12 aprile 2012 17:38
     
     

    Hello! I have a SQL Server agent job that runs a stored procedure and then appends the date to the filename. The file is .csv. I need the headers to be included in the .csv file but can't figure out how to do that. Can anyone help me? My job is below. Thanks!

    DECLARE @dir varchar(8000), @date_str varchar(8000),

    @FileName varchar(8000), @bcpCommand varchar(8000), @sql varchar(8000)

    SET @sql = 'EXEC dbname.dbo.Rpt_stored proc

    SET @dir = '\\pathtofile\'

    SET @date_str = REPLACE( CONVERT(char(8),GETDATE(),1), '/', '-' )

    SET @FileName = @dir + Filename_' + @date_str + '.txt'

    SET @bcpCommand = 'bcp "' + @sql + '" queryout "' + @FileName + '" -T -c -t"|"'

    EXEC master..xp_cmdshell @bcpCommand


    Linda

Tutte le risposte