【问题标题】:Powershell, Invoke-SqlCmd: How to get all the data from a columnPowershell,Invoke-SqlCmd:如何从列中获取所有数据
【发布时间】:2019-08-22 19:29:59
【问题描述】:

我有一个表,其中有一列包含 JSON 数据。它可以相当大。我想使用 PowerShell 运行查询以从单行中选择 JSON 数据。该命令如下所示:

Invoke-Sqlcmd -ServerInstance xxxxxx 
  -Database xxxxxx -Username xxxxxx -Password xxxxxx 
  -Query 'select [data] from jsontable where versionid=1922' 
  -MaxCharLength 700000 | Out-File .\my.json

命令有效,但结果只是整体的一小部分。例如在我看到的输出文件中:

data                                                                                                                                                                                                                              
----                                                                                                                                                                                                                              
{"$type":"System.Collections.Generic.List`1[[TBSM.Vision.FpFtp.Common.Domain.Data.FeedDerivedAttributeRuleData, TBSM.Vision.FpFtp.Common.Domain.Data]], mscorlib","$values":[{"$type":"TBSM.Vision.FpFtp.Common.Domain.Data.Fee...

但数据实际上开始了:

{"$type":"System.Collections.Generic.List`1[[TBSM.Vision.FpFtp.Common.Domain.Data.FeedDerivedAttributeRuleData, TBSM.Vision.FpFtp.Common.Domain.Data]], mscorlib","$values":[{"$type":"TBSM.Vision.FpFtp.Common.Domain.Data.FeedDerivedAttributeRuleData, TBSM.Vision.FpFtp.Common.Domain.Data","FeedCode":"All","id":"a513ede8-d520-77b1-65f8-6377a24fdd83","mappingRules":{"$type":"System.Collections.Generic.List`1[[TBSM.Vision.FpFtp.Common.Domain.Data.DerivedAttributeRuleDataCollection, TBSM.Vision.FpFtp.Common.Domain.Data]], mscorlib","$values":[{"$type":"TBSM.Vision.FpFtp.Common.Domain.Data.DerivedAttributeRuleDataCollection, TBSM.Vision.FpFtp.Common.Domain.Data","Rule Sequence":27,"classification":"Default","id":"3ad4c21c-7e69-e1ce-473f-d477767054ec","mappingRules":{"$type":"System.Collections.Generic.List`1[[TBSM.Vision.FpFtp.Common.Domain.Data.RuleData, TBSM.Vision.FpFtp.Common.Domain.Data]], mscorlib","$values":[

然后从那里继续。

如您所见,我指定了 MaxCharLength。如何停止截断并获取所有 JSON 数据

【问题讨论】:

  • 在脚本末尾试试这个-> Select-Object date |格式列表
  • 这让我得到了更多,但仍然不是全部。
  • 我认为您正在将 DataRow 或 DataTable 输送到 Out-File 中,并且它会将 that 序列化到您的文件中。尝试类似“$result = Invoke-SqlCmd ...; $result.Rows[0][“数据”].Value | out-file .\my.json”,它应该只写你的数据。明天我到电脑前去看看。
  • 正如@mclayton 所说,在将命令作为变量获取后尝试 [Format-List -InputObject $result]
  • 试试-MaxCharLength 700000 | Select -Expand Data | Out-File .\my.json

标签: powershell sql-server-2016 invoke-sqlcmd


【解决方案1】:

根据我上面的评论,Invoke-Sqlcmd 正在返回 System.Data.DataRow,而 Out-File 正在将其序列化(并截断)到文件,而不是 [data] 列中的原始字符串。

作为一个简单的复制,这个脚本

PS> Invoke-Sqlcmd -ServerInstance ".\sqlexpress" -query "SELECT '{...my json...}' AS data" | out-file "c:\temp\temp.txt"

将此内容写入 temp.txt:


data 
---- 
{...my json...}

只是为了证明返回值是一个DataRow:

PS> $result = Invoke-Sqlcmd -ServerInstance ".\sqlexpress" -query "SELECT '{...my json...}' AS data"
PS> write-host $result.GetType().FullName
System.Data.DataRow

如果您想从结果集中的 [data] 列中写入值,您需要从 Invoke-Sqlcmd 的结果中提取该值:

PS> $result = Invoke-Sqlcmd -ServerInstance ".\sqlexpress" -query "SELECT '{...my json...}' AS data"
PS> $value = $result["data"]
PS> $value | out-file "c:\temp\temp.txt"

现在输出文件包含:

{...my json...}

请注意,如果结果集中有不止一行,它将是一个 Object[] 数组。

PS> $result = Invoke-Sqlcmd -ServerInstance ".\sqlexpress" -query "SELECT '{...my json...}' AS data UNION SELECT '{...my other json...}' AS data"
PS> write-host $result.GetType().FullName
System.Object[]

你需要指定写出哪一行(例如[0]):

PS> $result = Invoke-Sqlcmd -ServerInstance ".\sqlexpress" -query "SELECT '{...my json...}' AS data UNION SELECT '{...my other json...}' AS data"
PS> $value = $result[0]["data"]
PS> $value | out-file "c:\temp\temp.txt"

希望这会有所帮助...

【讨论】:

  • 非常有趣和有用!有趣的是,我发现结果比预期的要长,而且比 SQL Server 报告的要长。当我将 maxlength 提高到 4M 时,我得到了一切。
猜你喜欢
  • 2019-06-21
  • 2017-12-21
  • 2016-01-15
  • 2015-07-06
  • 1970-01-01
  • 1970-01-01
  • 2011-06-13
  • 2017-09-30
  • 1970-01-01
相关资源
最近更新 更多