gpt4 book ai didi

sql-server - Powershell Invoke-Sqlcmd to DataTable rowcount 在其 1 条记录时不正确

转载 作者:行者123 更新时间:2023-12-02 23:03:10 25 4
gpt4 key购买 nike

$sql=("select top 1 * FROM CollectionProfile")

$CollectionProfile = New-Object System.Data.DataTable

$CollectionProfile = Invoke-Sqlcmd -ServerInstance $Instance -Database $db -Query $sql -ErrorAction Stop

$CollectionProfile.Rows.Count

返回:0

但如果我将 TOP 计数更改为 2 -

$sql=("select top 2 * FROM CollectionProfile")

返回:2

让我发疯,是的,我在“内部网络”上找不到一个引用。我一定是做错了什么,但是什么?

最佳答案

当您将查询与 TOP 1 一起使用时,Invoke-SqlCmd 返回一个 DataRow。当您将查询与 TOP 2 一起使用时,Invoke-SqlCmd 返回一个 DataRows 数组。 Invoke-SqlCmd 不返回 DataTable。您可以更改代码以强制返回一个数组(参见此处:force array),然后检查其上的计数:

$sql = ("select top 1 * FROM CollectionProfile")

$CollectionProfile = @(Invoke-Sqlcmd -ServerInstance $Instance -Database $db -Query $sql -ErrorAction Stop)

$CollectionProfile.Count #Returns 0 with Null, 1 with TOP 1, and 2 with TOP 2

关于sql-server - Powershell Invoke-Sqlcmd to DataTable rowcount 在其 1 条记录时不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37030834/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com