March 27, 2020 at 5:04 pm
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
March 27, 2020 at 8:22 pm
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
Change is inevitable... Change for the better is not.
March 29, 2020 at 4:40 pm
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
March 29, 2020 at 5:56 pm
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