SQL Запрос не возвращает ответ на два запроса - PullRequest
0 голосов
/ 05 февраля 2020

Я делаю скрипт 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 
           }

}



Я обнаружил, This is a test - это не обновить в моем файле журнала, который я подозреваю, переменная $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, которые возвращаются из базы данных.

...