Powershell,调用SqlCmd:如何从列中获取所有数据

Powershell,调用SqlCmd:如何从列中获取所有数据,powershell,sql-server-2016,invoke-sqlcmd,Powershell,Sql Server 2016,Invoke Sqlcmd,我有一个表,其中有一列包含JSON数据。它可能相当大。我想运行一个查询,使用PowerShell从一行中选择JSON数据。该命令如下所示: Invoke-Sqlcmd -ServerInstance xxxxxx -Database xxxxxx -Username xxxxxx -Password xxxxxx -Query 'select [data] from jsontable where versionid=1922' -MaxCharLength 700000 |

我有一个表,其中有一列包含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
该命令起作用,但结果只是整个命令的一小部分。e、 g.在输出文件中,我看到:

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数据根据我上面的评论,Invoke Sqlcmd将返回System.data.DataRow,Out文件将序列化(并截断)该文件,而不是[data]列中的原始字符串

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

PS>调用Sqlcmd-ServerInstance.“\sqlexpress”-查询“选择“{…我的json…}”作为数据”|输出文件“c:\temp\temp.txt”
将此内容写入temp.txt:


data 
---- 
{...my json...}
为了证明返回值是DataRow:

PS>$result=Invoke Sqlcmd-ServerInstance“\sqlexpress”-query“选择“{…我的json…}”作为数据”
PS>写入主机$result.GetType().FullName
System.Data.DataRow
如果要从结果集中的[data]列写入值,则需要从Invoke Sqlcmd的结果中提取该值:

PS>$result=Invoke Sqlcmd-ServerInstance“\sqlexpress”-query“选择“{…我的json…}”作为数据”
PS>$value=$result[“数据”]
PS>$value |输出文件“c:\temp\temp.txt”
现在输出文件包含:

{...my json...}
请注意,如果结果集中有多行,则它将是一个Object[]数组

PS>$result=Invoke Sqlcmd-ServerInstance“\sqlexpress”-query“选择“{…我的json…}”作为数据联合选择“{…我的其他json…}”作为数据”
PS>写入主机$result.GetType().FullName
系统对象[]
您需要指定要写出的行(例如
[0]
):

PS>$result=Invoke Sqlcmd-ServerInstance“\sqlexpress”-query“选择“{…我的json…}”作为数据联合选择“{…我的其他json…}”作为数据”
PS>$value=$result[0][“数据”]
PS>$value |输出文件“c:\temp\temp.txt”

希望这有帮助…

在脚本末尾尝试一下->选择对象日期|格式列表,这让我得到了更多,但仍然不是全部。我认为您正在将数据行或数据表传输到输出文件中,并将其序列化到您的文件中。尝试类似“$result=Invoke SqlCmd…;$result.Rows[0][“data”].Value | out file.\my.json”这样的操作,它应该只写数据。我明天到计算机前会检查一下。正如@mclayton所说,在将命令作为变量获取后,请尝试[Format List-InputObject$result]try
-MaxCharLength 700000 | Select-Expand Data | Out File.\my.json
非常有趣和有用!有趣的是,我发现结果比预期的要长,也比SQLServer报告的要长。当我把最大长度提高到4米时,我得到了全部。