What is the easiest way to create a SQL Query to export data to a flat file with column names? It can be tab or comma delimited (comma preferred). I would like something I can run from SQL Agent to keep it simple. Currently this is what I have, but it doesn't include column names.
SET NOCOUNT ON DECLARE @ADEXPORT VARCHAR(1000) SET @ADEXPORT = 'bcp "select fn as givenName, ln as sn, (fn + '' '' + ln) as displayname, id as cn, REPLACE(CONVERT(CHAR(10),STU.BD,10),''-'','''') as password, id as samAccountName, id as userPrincipalName, ''True'' as userCannotChangePassword, ''True'' as passwordNeverExpires, ''False'' as accountDisabled from DST13000GUSD.dbo.STU WHERE (SC < 91) and (SC <> 1) and (TG = '''') and (DEL = 0) ORDER BY LN" QUERYOUT "D:\ActiveDirectoryExport\ActiveDirectoryExport.txt" -T -c -S' + @@SERVERNAME EXEC master..xp_CmdShell @ADEXPORT