Powershell SQL查询不返回两个查询的任何响应

Powershell SQL查询不返回两个查询的任何响应,powershell,powershell-2.0,powershell-3.0,Powershell,Powershell 2.0,Powershell 3.0,我正在执行一个powershell脚本,用于查询数据库并从数据库表中提取值,该脚本将查询两个不同的数据库表。但是我发现第二个查询没有返回任何结果 这是我的脚本 $query1="select no,b.store,b.Name, b.ID,a.location from (SELECT *,(ABS(CHECKSUM(NEWID())) % 91 + 1)as ID from table_A ) a left join table_B b on a.ID = b.ID where a.f

我正在执行一个powershell脚本,用于查询数据库并从数据库表中提取值,该脚本将查询两个不同的数据库表。但是我发现第二个查询没有返回任何结果

这是我的脚本

$query1="select  no,b.store,b.Name, b.ID,a.location from 
(SELECT *,(ABS(CHECKSUM(NEWID())) % 91 + 1)as ID
from table_A ) a 
left join table_B b
on a.ID = b.ID 
where a.firstname is not null "
$com=$connection.CreateCommand()
$com.CommandText=$uery1
$r1 = $com.ExecuteReader()
add-content D:\test_$currentDate.log "The reader : $r1"
while ($r1.read())
{
    for ($i = 0; $i -lt $r1.FieldCount; $i++)
      {
         #assign value to the respective variable 
         # working fine for this section
         # value assign correctly and database return result
      }
          Add-Content D:\test_$currentDate.log  "The no before generate the email : $no"
          $query2="SELECT test_table ('"+$no+"') " 
          # $no is the value return from database for the first query
          $com2=$connection.CreateCommand()
          $com2.CommandText=$query2
          $result=$command.ExecuteReader()
          Add-Content D:\test__$currentDate.log  "The query2 : $query2"

          Add-Content D:\test_$currentDate.log  "The com2 : $com2"
          add-content D:\test_$currentDate.log "result : $result" 
       while ($result.read())
            {   
                Add-Content D:\test_$currentDate.log  "This is a test"
               # assign value from database 
           }

}


我发现
这是一个测试
在我的日志文件中没有更新,我怀疑
$result
变量为空。我尝试从SQL控制台执行查询,它能够返回两个查询的结果,示例输出在这里

The reader : System.Data.SqlClient.SqlDataReader  
The no before generate the email : XXXXX
The query2 : SELECT test_table ('"+$no+"') 
The com2 : System.Data.SqlClient.SqlCommand
result : System.Data.Common.DataRecordInternal 



两个查询都指向同一个数据库,
$query2
是一个标量值函数,我想知道从数据库返回的
System.Data.SqlClient.SqlDataReader
System.Data.Common.DataRecordInternal
之间有什么不同

您的代码示例看起来无法写入您提供的输出-行
添加内容D:\test\uuuuu$currentDate.log“query2:$query”
引用了代码中未定义的变量
$query
,因此我认为您的输出不能包含
query2:selecttest\u表(“+$no+”)
。你能确认代码样本和/或输出是正确的吗?我更新了代码,我必须在代码中隐藏一些机密细节。我找到了问题的答案。触发器标量值函数将需要其他方法1)您的代码容易出现SQL注入问题/攻击。2) 执行query1时有一个输入错误,这可能导致query2根本没有执行
$com.CommandText=$uery1
缺少一个
q
您能确认您确实尝试过执行示例代码吗?除了@alroc的评论外,我还看到了至少一个其他的不一致性,这意味着您的代码无法生成您发布的确切输出。