2

我一直在寻找有关此主题的答案,并已在另一个论坛上发帖,但这似乎是所有知识的字体。我正在尝试使用 PowerShell 从 SQL Server 2000 数据库中提取数据。Powershell 脚本调用一个存储过程,然后提取数据并将其导出到 CSV 文件中。问题是输出数据中的日期时间字段已经丢失了毫秒。当 PowerShell 将数据库中的数据提取到临时表中时,就会发生这种情况。这是两段代码。

PowerShell 脚本

#VARIABLES
$SqlQuery = "SAP_proc"
#Connection Strings
$Server = "server"
$Database = "db_dab"
#Output Files
$OutputPath = "c:\Sapout.csv"
#END OF VARIABLES

#SQL Connection
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server=$Server;Database=$Database;Integrated Security=True"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = "SAP_proc"
$SqlCmd.Connection = $SqlConnection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$DataSet = New-Object System.Data.DataSet
$DataOut = $SqlAdapter.Fill($DataSet)
#$DataOut = $SqlAdapter.FillSchema($DataSet,[System.Data.SchemaType]::Source)
#Data Manipulation
$DataOut | Out-Null
#Export Data to Hash Table
$DataTable = $DataSet.Tables[0] 
#Export Has table to CSV
$DataTable | Export-CSV -Delimiter "," -Encoding Unicode -Path $OutputPath -NoTypeInformation
$SqlConnection.Close()

存储过程

ALTER proc [dbo].[SAP_proc]
as
DECLARE @return_value int
DECLARE @starttime datetime
DECLARE @endtime datetime

SET @starttime = DATEADD (dd, -30, CURRENT_TIMESTAMP)
SET @endtime = CURRENT_TIMESTAMP

EXEC    @return_value = [dbo].[sp_agent]
        @starttime
        ,@endtime 

SELECT  'Return Value' = @return_value

因为其他存储过程SP_agent是由软件创建的,所以我无法编辑它。此外,我不想在我的命令文本字符串中复制软件定义的存储过程(使用 SELECT 转换为日期时间的 varchar),因为它是一个庞然大物的存储过程。

任何帮助都会非常有用。

4

1 回答 1

1

这不是 Powershell 问题或临时表问题。这是因为当您使用不包括毫秒的默认 tostring 方法调用 export-csv 时,您的日期时间列将转换为字符串。如果你想要毫秒,那么在 tostring 方法调用中指定它:

$a = get-date
$a.ToString("d/M/yyyy hh:mm:ss.fff tt")

#To to something similar with your export-csv of a datatable you can create an expression:

$DataTable | select column1, column2, @{n=datecolumn;e={$_.datecolumn.ToString("d/M/yyyy hh:mm:ss.fff tt")}} | export-csv <rest of code>

编辑于 2012年 10 月 17 日看来你仍然有这个问题。所以这是一个完整的脚本,我已经测试了输出毫秒。您需要将变量部分更改为您的环境。我希望这有帮助:

#VARIABLES
$SqlQuery = "select 'test' as column1, 'milliseconds' as column2, getdate() as datecolumn"
#Connection Strings
$Server = "$env:computername\sql1"
$Database = "tempdb"
#Output Files
$OutputPath = "./millisec.csv"
#END OF VARIABLES

#SQL Connection
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server=$Server;Database=$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
$DataOut = $SqlAdapter.Fill($DataSet)
#$DataOut = $SqlAdapter.FillSchema($DataSet,[System.Data.SchemaType]::Source)
#Data Manipulation
$DataOut | Out-Null
#Export Data to Hash Table
$DataTable = $DataSet.Tables[0] 
#Export Has table to CSV
#$DataTable | Export-CSV -Delimiter "," -Encoding Unicode -Path $OutputPath -NoTypeInformation
$DataTable | select column1, column2, @{n='datecolumn';e={$_.datecolumn.ToString("M/d/yyyy hh:mm:ss.fff tt")}} | export-csv $OutputPath -NoTypeInformation -force

$SqlConnection.Close()

#另一个带有解释的代码示例。添加于 2012 年 10 月 23 日

#VARIABLES
$SqlQuery = "select getdate() as datecolumn"
#Connection Strings
$Server = "$env:computername\sql1"
$Database = "tempdb"


$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server=$Server;Database=$Database;Integrated Security=True"
$SqlConnection.Open() 
$SqlCmd = new-Object System.Data.SqlClient.SqlCommand($SqlQuery, $SqlConnection) 
$data = $SqlCmd.ExecuteScalar() 
$SqlConnection.Close()

#Notice NO millisecond
Write-Output $data

#See $data is an object of type System.DateTime
$data | gm

#There's a property called millisecond
#Millisecond          Property       int Millisecond {get;} 

#Although you don't "see" millisecond's on screen it's still there
$data.Millisecond


#Powershell uses a types and format rules to define how data is display on screen. You can see this by  looking at 
#C:\Windows\System32\WindowsPowerShell\v1.0\types.ps1xml and searching for "DataTime". This file along with format file define how data is displayed
#When you display datatime screen it's implicitly calling
#Searching for datetime in types.ps1xml you'll find this line:
#"{0} {1}" -f $this.ToLongDateString(), $this.ToLongTimeString()

#for our example

"{0} {1}" -f $data.ToLongDateString(), $data.ToLongTimeString()

#So millisecond is still there, but if you want millisecond in your output to CSV, screen or file you'll need to call a ToString method with a date format. Here's an example:
$data.ToString("M/d/yyyy hh:mm:ss.fff tt")
于 2012-10-12T21:17:53.610 回答