I have a script that runs a pretty basic PDO transaction and it doe work (selects records and performs the merge based on those results)
So it successfully inserts/updates but I'm trying to get a count of accurate records and it doesn't work, it just returns 1 in powershell.
Starting with an empty database table and running this script, It inserts 1300 records successfully but $count
prints '1'.
I'm simply trying to get record counts, success messages and errors for the purpose of an email when the script runs. Am I missing something?
while($arr = odbc_fetch_array($result)){
$stmt = $PDO->prepare("
MERGE INTO ORDER_STATUS as S
USING (VALUES(
CAST(:ORDER_ID as INT),
CAST(:ORDER_STATUS as VARCHAR(45)),
CAST(:is_active as DECIMAL(1,0)),
CAST(:DATE_UPDATED as DATE)
)
)
AS O(order_id, order_status, is_active, date_updated)
ON O.order_id = S.order_id
WHEN MATCHED THEN UPDATE SET order_status = O.order_status, is_active = O.is_active, date_updated = O.date_updated
WHEN NOT MATCHED THEN INSERT(order_id,order_status,is_active,date_updated) VALUES(O.order_id, O.order_status, O.is_active, O.date_updated)
");
$PDO->beginTransaction();
$i = 0;
while($db2row = odbc_fetch_array($result)) {
if(++$i % 1000 == 0) {
$PDO->commit();
$PDO->beginTransaction();
}
$stmt->execute($db2row);
}
$PDO->commit();
$count = $stmt->rowCount();
print_r($count);
}