我已经找到了关于这个主题的答案,我已经在另一个论坛上发帖了,但这似乎是所有知识的字体。我试图使用PowerShell从SQL Server 2000数据库中提取数据。 Powershell脚本调用存储过程,然后将数据和export-csv提取出来,以及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 for datetime),因为它是一个庞大的存储过程。
任何帮助都会非常有用。
答案 0 :(得分:1)
这不是Powershell问题或临时表问题。这是因为当使用默认的tostring方法(不包括毫秒)调用export-csv时,datetime列将转换为字符串。如果你想要毫秒,那么在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>
编辑于10/17/2012 看起来你仍然遇到麻烦。所以这是一个完整的脚本,我测试输出毫秒。您需要将变量部分更改为您的环境。我希望这会有所帮助:
#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()
#另一个带解释的代码示例。在10/23/2012添加
#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")