我需要运行一系列查询和结果导出到一个平面文件,但这个文件是一种特殊的:CSV文件
- 列分隔符必须是一个|
- 我需要放置“围绕字符或varchar列BU如果任何字符或varchar列为NULL为特定的行,我不能放置”。如果该值是一个空字符串,那么是的,我需要放置它们。
我发现了一个解决方法,在T-SQL中构建整个记录,但是我需要为每个新构建的查询都做这个工作。
任何想法
我需要运行一系列查询和结果导出到一个平面文件,但这个文件是一种特殊的:CSV文件
我发现了一个解决方法,在T-SQL中构建整个记录,但是我需要为每个新构建的查询都做这个工作。
任何想法
可以使用SQLPS模块,然后选择您的表
Import-Module Sqlps -DisableNameChecking;
Invoke-Sqlcmd -ServerInstance "yourservername" -Database "yourdatabasename" -Query "select top 2 zonetable1, zonetable2 from dbo.yourtable" |
Export-Csv "c:\temp\result.csv" -NoTypeInformation
下面是一个使用PowerShell的一个解决方案,因为你的标签,在你的问题。
Invoke-SqlCmd
相反)。这样可以避免路径更改(导入的SQLPS模块的副作用),并且意味着您可以通过在数据中查找DbNull来查找空值。DataRow
对象的属性。。
clear-host
function Invoke-SQLQuery {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[string]$ServerInstance
,
[Parameter(Mandatory = $true)]
[string]$Database
,
[Parameter(Mandatory = $true)]
[string]$Query
,
[Parameter(Mandatory = $false)]
[int]$CommandTimeoutSeconds = 30 #30 is the SQL default
)
begin {
$connectionString = ("Server={0};Database={1};Integrated Security=True;" -f $DbInstance,$DbCatalog)
$connection = New-Object System.Data.SqlClient.SqlConnection
$connection.ConnectionString = $connectionString
$connection.Open()
}
process {
$command = $connection.CreateCommand()
$command.CommandTimeout = $CommandTimeoutSeconds
$command.CommandText = $query
$result = $command.ExecuteReader()
$table = new-object "System.Data.DataTable"
$table.Load($result)
$table | Convert-DataRowToPSCustomObject
}
end {
$connection.Close()
}
}
function Convert-DataRowToPSCustomObject {
[CmdletBinding()]
param (
[Parameter(ValueFromPipeline = $true)]
[System.Data.DataRow]$Row
)
process {
[PSObject]($Row | Select * -ExcludeProperty RowError, RowState, Table, ItemArray, HasErrors)
}
}
#this gives us a nice way to distinguish nulls from blanks in csv rows
function Convert-DbNullToMarker {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[PSObject]$Data
)
process {
$Data | Get-Member -MemberType NoteProperty | % Name | %{
if($Data.$_ -is [DbNull]) {
$Data.$_ = 'DbNullMarker{fdb653bf-0810-4893-a076-e3d935d9e6ba}'
}
}
$Data
}
}
#this gives us a nice way to distinguish nulls from blanks in csv rows
function Convert-DbNullMarkerToNull {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[string]$CsvRow
)
process {
$CsvRow -replace '"DbNullMarker{fdb653bf-0810-4893-a076-e3d935d9e6ba}"',''
}
}
Invoke-SqlQuery -ServerInstance "." -Database "master" -Query "select * from (values (null, 'hello'), ('hi', 'world')) x(a, b)" |
Convert-DbNullToMarker -Verbose |
ConvertTo-Csv -Delimiter '|' -NoTypeInformation |
Convert-DbNullMarkerToNull |
Out-File -FilePath (Join-Path $PSScriptRoot 'SqlCsvDemo.csv') -Encoding utf8 -Force
一个选项是使用BCP:[如何使用BCP提取管道分隔数据](http://dba.stackexchange.com/q/111378/43889) – SqlZim