ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Export Table to CSV file with Column Header (Name) using bcp utility in SQL Server<p>Hi <a class="username" rel="maideen">maideen,</a></p> <p>Check this query.</p> <p><strong><span style="text-decoration: underline;">Database</span></strong></p> <p>For this example I have used of Northwind database that you can download using the link given below.</p> <p><a href="https://www.aspsnippets.com/Articles/Install-Microsoft-Northwind-and-Pubs-Sample-databases-in-SQL-Server-Management-Studio.aspx" target="_blank">Download Northwind Database</a></p> <p><strong><span style="text-decoration: underline;">SQL</span></strong></p> <pre class="brush: sql">DECLARE @Servername varchar(100) SET @Servername = @@servername DECLARE @columnHeader VARCHAR(8000) SELECT @columnHeader = &#39;&#39;&#39;CustomerID&#39;&#39;,&#39;&#39;ContactName&#39;&#39;,&#39;&#39;City&#39;&#39;,&#39;&#39;Country&#39;&#39;&#39; declare @sql varchar(8000) select @sql = &#39;bcp &#34;SELECT &#39;+ @columnHeader +&#39; UNION ALL SELECT TOP 5 CustomerID,ContactName,City,Country FROM Northwind..Customers&#34; queryout D:\Export_FILE.csv -c -t, -T -S&#39; + @servername exec master..xp_cmdshell @sql</pre> <p><strong><span style="text-decoration: underline;">Screenshot</span></strong></p> <p><span style="text-decoration: underline;">Query output and Exported CSV</span></p> <p><img src="https://i.imgur.com/5nd7vQz.jpg" alt="" width="403" height="505" /></p>https://www.aspforums.net:443/Threads/179424/Export-Table-to-CSV-file-with-Column-Header-Name-using-bcp-utility-in-SQL-Server/https://www.aspforums.net:443/Threads/179424/Export-Table-to-CSV-file-with-Column-Header-Name-using-bcp-utility-in-SQL-Server/Thu, 25 Jul 2019 11:54:19 GMT