output with no headers csv

  • I have a sql script which runs fine, but I need the output to show  headers in comma delimited trying to avoid sql cmd if possible would rather use tsql

  • Please post the script if you want an accurate answer.  Otherwise, my suggestion would be to read the following article.

    If you actually have SQL Server 2017 or better instead of just 2016, search for the STRING_AGG function.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • If this needs to be scheduled just use a powershell job step in SQL Server Agent.

    Something like:

    $SQLparams = @{
    'ServerInstance' = '.';
    'Database' = 'YourDB';
    'ErrorAction' = 'Stop';
    'Query' = 'SELECT * FROM YourQuery' }

    $Exportparams = @{
    'Path' = 'C:\YourDir\YourFile.csv' }

    Invoke-Sqlcmd @SQLparams |
    Export-CSV @Exportparams -NoTypeInformation
  • SELECT 'col1,col2,col3,col4' as line
    UNION ALL
    SELECT CONCAT('''', col1,''',''', col2,''',''', col3,''',''', col4,'''')
    FROM myTable

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply