我想从SQL Server查询输出中删除列标题.我做了搜索,但没有找到任何解决方案.我有一个查询,例如.
select cc.DepartmentID , cc.Name from HumanResources.Department cc
当我运行此查询时,我得到这样的输出.
ID Name 12 Document Control 1 Engineering 16 Executive 14 Facilities and Maintenance 10 Finance 9 Human Resources
我想从SQL Server的输出中删除ID和名称(列标题).
我将通过脚本运行此查询以生成csv文件.
编辑:
当我通过脚本运行查询时,我将csv文件作为输出,它看起来像这样.
#TYPE System.Data.DataRow ID Name
更新:
我正在使用PowerShell脚本.
$Database = "temp" $Server = "localhost" $AttachmentPath = "output.csv" # Connect to SQL and query data, extract data to SQL Adapter $SqlQuery = "select cc.DepartmentID , cc.Name from HumanResources.Department cc" $SqlConnection = New-Object System.Data.SqlClient.SqlConnection $SqlConnection.ConnectionString = "Data Source=$Server;Initial Catalog=$Database;Integrated Security = True" $SqlCmd = New-Object System.Data.SqlClient.SqlCommand $SqlCmd.CommandText = $SqlQuery $SqlCmd.Connection = $SqlConnection $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter $SqlAdapter.SelectCommand = $SqlCmd $DataSet = New-Object System.Data.DataSet $nRecs = $SqlAdapter.Fill($DataSet) $nRecs | Out-Null #Populate Hash Table $objTable = $DataSet.Tables[0] #Export Hash Table to CSV File $objTable | Export-CSV $AttachmentPath
我想从输出中删除列标题.
在SSMS下的工具/选项/查询结果/ SQL Server /结果文本中,有一个"在结果集中包含列标题"的复选框.类似于网格的结果.
如果您通过powershell使用sqlcmd,则可以使用/ h-1禁用标头.
此设置对应于环境变量SQLCMDHEADERS.
技巧和窍门
使用值-1指定不应打印标题.如果提供-1,则参数和设置之间不能有空格,即-h-1.否则,SQLCMD将其解释为单独的选项并失败.
示例(从[TechNet]修改)1:
sqlcmd -q /h-1 "SELECT * FROM AdventureWorks2012.Person.Person"
也将合作 -h-1