Rac*_*SQL 2 sql-server export excel sql-server-2014
这里有很多问题询问“如何从 SQL Server 导出到 Excel”,但是每个答案以及我知道的东西都给我带来了问题(这里是 64 位系统)。
我们有一个开发人员每月运行 6 次查询(从 2010 年、2011 年等更改年份)。然后,他将结果复制并粘贴到 Excel 电子表格中。
我在将这些数据从 SQL Server 2014 导出到 Excel 时遇到问题。
这是查询:
SELECT
R.numero,
R.ano,
R.data 'data abertura',
R.datahora_conclusao 'data conclusão',
R.datahora_emissao 'data emissão',
S.[status],
(ins.INSTITUTO +
' - ' + dir.DIRETORIA +
(CASE WHEN NOT nuc.NUCLEO IS NULL THEN ' - ' + nuc.NUCLEO ELSE '' END) +
(CASE WHEN NOT equ.EQUIPE IS NULL THEN ' - ' + equ.EQUIPE ELSE '' END)) AS UNIDADE,
N.codigo 'codigo natureza exame',
Case E.cod_estadopreservacao when 1 then 'Preservado' when 2 then 'Não Preservado' else 'Laborátorio' end 'Local Exame',
u.nome 'Perito Designado'
FROM tbReps R
LEFT Join tbExames E on E.cod_rep = R.cod_rep
LEFT JOIN tbUsuarios U ON U.cod_usuario = R.cod_perito_primeiro
LEFT JOIN tbUnidades AS uni WITH(NOLOCK) ON R.cod_unidade = uni.cod_unidade
LEFT JOIN lstUnidadesInstitutos AS ins WITH(NOLOCK) ON uni.cod_instituto = ins.cod_instituto
LEFT JOIN lstUnidadesDiretorias AS dir WITH(NOLOCK) ON uni.cod_instituto = dir.cod_instituto AND uni.cod_diretoria = dir.cod_diretoria
LEFT JOIN lstUnidadesNucleos AS nuc WITH(NOLOCK) ON uni.cod_diretoria = nuc.cod_diretoria AND uni.cod_nucleo = nuc.cod_nucleo
LEFT JOIN lstUnidadesEquipes AS equ WITH(NOLOCK) ON uni.cod_nucleo = equ.cod_nucleo and uni.cod_equipe = equ.cod_equipe
LEFT JOIN lstNaturezasExame N ON R.cod_naturezaexame = N.cod_naturezaexame
LEFT JOIN lstStatus S ON S.cod_status = R.cod_status
WHERE year(r.data) = 2016 and r.data <= '2016-04-30 23:59:59'
order by data
Run Code Online (Sandbox Code Playgroud)
我正在想办法将其直接导入到 Excel 文件中,以年份分隔。我认为创建 6 个表 (2010...2011...2012) 并使用该Task > Export
过程是个好主意,然后将它们删除。但我认为有一种更好(也更正确)的方法来做到这一点。
我应该使用哪个 OLE.DB 提供程序?有没有脚本可以做到这一点?一些我不知道的程序?
我在with (nolock)
任何地方都仅用于测试目的。
PowerShell 是要走的路:
# Original Author : Bill Fellows (http://billfellows.blogspot.com/2011/03/powershell-export-query-to-csv.html)
# Modified by : Kin Shah (http://dba.stackexchange.com/users/8783/kin)
# Added the -NoTypeInformation to remove "#TYPE System.Data.DataRow" from the header !
# http://www.vistax64.com/powershell/190352-executing-sql-queries-powershell.html
$server = "servername\INSTANCE1" #YourServer Name e.g. server1\instance1
$database = "your_db_name" #your database name
$query = "select some columns from table inner join other table on table.id = othertable.id"
# powershell raw/verbatim strings are ugly
# Update this with the actual path where you want data dumped
# change here to the path that you want
$extractFile = @"
C:\SQLtoExcel.csv
"@
# Use windows authentication !!
$connectionTemplate = "Data Source={0};Integrated Security=SSPI;Initial Catalog={1};"
$connectionString = [string]::Format($connectionTemplate, $server, $database)
$connection = New-Object System.Data.SqlClient.SqlConnection
$connection.ConnectionString = $connectionString
$command = New-Object System.Data.SqlClient.SqlCommand
$command.CommandText = $query
$command.Connection = $connection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $command
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)
$connection.Close()
# dump the data to a csv
# http://technet.microsoft.com/en-us/library/ee176825.aspx
# -NoTypeInformation will remove the first #TYPE System.Data.DataRow
$DataSet.Tables[0] | Export-Csv $extractFile -NoTypeInformation
Run Code Online (Sandbox Code Playgroud)
也可以看看:
Kevin Feasel通过 Powershell 实现多标签 Excel
归档时间: |
|
查看次数: |
6491 次 |
最近记录: |