2017-03-17 12 views
1

我需要運行一系列查詢和結果導出到一個平面文件,但這個文件是一種特殊的:CSV文件

  1. 列分隔符必須是一個|
  2. 我需要放置「圍繞字符或varchar列BU如果任何字符或varchar列爲NULL爲特定的行,我不能放置」。如果該值是一個空字符串,那麼是的,我需要放置它們。

我發現了一個解決方法,在T-SQL中構建整個記錄,但是我需要爲每個新構建的查詢都做這個工作。

任何想法

+0

一個選項是使用BCP:[如何使用BCP提取管道分隔數據](http://dba.stackexchange.com/q/111378/43889) – SqlZim

回答

1

有不同的選擇在這裏:

  • 您可以使用BCP工具來提取數據
  • 您可以使用集成服務,如果你有可能做到這一點,每天一次並在SQL代理中安排包。
  • 您可以在管理Studio中使用導出嚮導,然後選擇您需要的分隔符:

右鍵單擊數據庫,然後單擊導出數據。 一直走在數據源,並在數據的目的地把你的文件,分隔的格式和(「)作爲文本限定符:

enter image description here

然後選擇要解壓縮的數據表(或查詢)並指定「垂直欄」作爲列分隔符 enter image description here

下一步是運行任務(這裏還提供了選項來保存SSIS包以再次運行它只要你想)

希望這可以幫助你。

1

可以使用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 
0

下面是一個使用PowerShell的一個解決方案,因爲你的標籤,在你的問題。

  • 使用.Net庫執行SQL(即與Invoke-SqlCmd相反)。這樣可以避免路徑更改(導入的SQLPS模塊的副作用),並且意味着您可以通過在數據中查找DbNull來查找空值。
  • 刪除那些不是請求數據的屬性,而是刪除DataRow對象的屬性。
  • 對於我們結果中的每個屬性,檢查它是否是DBNull。如果是,請用標記字符串替換它(該字符串包含一個GUID,因此很可能偶然出現在源數據中)。
  • 使用管道分隔符將數據轉換爲CSV
  • 用空白(即不帶引號)替換用引號括起來的所有出現的標記。
  • 結果寫入文件

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