我需要從Powershell獲取csv文件中的列名稱。從proc獲取Powershell中的列標題如果返回0結果
Powershell調用SQL過程,如果過程返回0行,那麼powershell腳本甚至不會將列名寫入csv文件。
我的代碼如下。
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server=localhost;Database=LIQ_TradeBustTethysTrader5895;Integrated Security=True"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = "Execute dbo.Tethys_ArrowGrass_Price_report @loginID = 'ALL', @tableName = 'order_msgs', @firmname = 'ALL', @AssetClass = 'ALL',@OutputBustRecords=0, @ManualOrder=0, @TimeFrom='0:0', @TimeTo='23:59', @ExcludeSimulatedTrades=0, @SingleTable =1"
$SqlCmd.Connection = $SqlConnection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)
$SqlConnection.Close()
$DataSet.Tables[0] |out-null
$DataSet.Tables[0] | Export-Csv -path "E:\Users\neerajd\Documents\tethystrader-public-dev-general\src\SQL\Misc\out.csv" -NoTypeInformation
它返回0行還是返回* nothing *?有一個區別。在Management Studio中執行時的結果是什麼樣的 - 您是否看到列標題? –
當我從Management Studio中運行它時,proc會顯示列標題。同時,powershell在cmd窗口中僅顯示「0」。 –
通過簡化開始調試。而不是運行一個sproc,做一個'select foo from bar where ...'並檢查它是否返回需要的東西。如果可行,那麼問題在於存在問題。如果沒有,問題在於別的。 – vonPryz