简体   繁体   English

为什么当我有多个结果时,此powershell代码仅返回一个结果?

[英]Why is this powershell code only returning one result when I have multiple results?

I am running a query through powershell. 我正在通过Powershell运行查询。 I know I have multiple results from running the query through ssms. 我知道通过ssms运行查询有多个结果。 Why does the variable only have one result in powershell? 为什么变量在Powershell中只有一个结果?

I have used many methods to do this query and I finally got it working but can't get all the results of the query. 我使用了许多方法来执行此查询,但最终使它正常工作,但无法获得查询的所有结果。

[string] $Server= "mochqdb01";
[string] $Database = "MoCApp.Models.Item+ItemDBContext";
[string] $SQLQuery= $("Select smEmail from Items where DateRequested >= dateadd(day,datediff(day,1,GETDATE()),0)");

$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
$SqlCmd.CommandText = $SQLQuery
$SqlCmd.Connection = $SqlConnection
$dbname = $SqlCmd.ExecuteScalar()
$SqlConnection.Close()
Write-output "Database is " $dbname

Output: Database is Franziew@marketyyo.com 输出:数据库为Franziew@marketyyo.com

Should have multiple results. 应该有多个结果。 Should I save into an array? 我应该保存到数组中吗?

I actually want to save the results into this format. 我实际上想将结果保存为这种格式。

Send-ToEmail -email "js@marketyyo.com","mb@marketyyo.com";Is this possible? Send-ToEmail-电子邮件“ js@marketyyo.com”,“ mb@marketyyo.com”;这可能吗?

ExecuteScalar() returns the first column of the first row of the first result set. ExecuteScalar()返回第一个结果集的第一行的第一列。 You need ExecuteReader() . 您需要ExecuteReader() EG 例如

$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
$SqlCmd.CommandText = $SQLQuery
$SqlCmd.Connection = $SqlConnection
$rdr = $SqlCmd.ExecuteReader()
while ($rdr.Read())
{
   $smMail = $rdr[0]
   write-output "email is $smMail"
}
$rdr.Close()
$SqlConnection.Close()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Rails中,为什么我希望自己的finder方法只返回一个结果呢? - In rails, why is my finder method only returning one result when I want it to return all of them? 作为一个结果返回多个“ WITH CTE”查询的结果 - Returning the results of multiple 'WITH CTE' queries as one result 我可以有多个选择但只返回一个结果集 - Can i have multiple select but only return one result set 当我只想要一个时聚合查询返回多行 - Aggregate query returning multiple rows when I only want one 当只有一条记录与WHERE子句匹配时,JOIN返回多个结果? - JOIN is returning Multiple results when only One record matches the WHERE clause? 带有 LIKE 的 Oracle SQL CASE 为两种情况返回一个值,当我只想要结果时,如果它们具有任何条件 - Oracle SQL CASE with a LIKE returning a value for both scenarios when I only want a result if they have any of the criteria 如果我的存储过程 function 只返回一个结果,我该如何返回多个结果? - How do I return multiple results from my Stored Procedure function if it returns only one result? 为什么这个 Hive 代码返回 0 结果? - Why is this Hive code returning 0 results? 当我只期望一个值时,DB2 SQL 函数返回多个值 - DB2 SQL function returning multiple values when I am expecting only one 当我只想要一行时,SQL JOIN 返回多行 - SQL JOIN returning multiple rows when I only want one row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM