I have a script that pulls data from a external source (five9) every 5 minutes and loads it into a SQL server, shown below. The script seems to work for the most part, but whenever I compare the results for the prior day to the results from the external source, I'm always a few hundred to a thousand rows short in SQL (out of 50,000 to 100,000 total rows). The scripts run without any errors in PowerShell.
I've got a second identical script with different start/end times to pull the last 24 hours running every night, and while that did reduce the missing rows a little the issues has persisted.
Duplicate values are restricted by a unique key value that is concatenated from other values in each row. I have tried turning off the is unique setting in SQL and deduping manually but still had missing rows.
I have verified that my unique string actually is unique by dumping the results from the external source into an excel file and concatenating there, then checking for duplicates. This does not turn up any results.
I have been able to identify which rows precisely are missing from SQL, and unfortunately nothing about that data is really jumping out at me as problematic.
Any help or insight anyone could offer would be greatly appreciated. Been pulling my hair out over this for over a month now lol.
$reportid = Start-Five9Report -FolderName folder -ReportName report -EndDateTime (Get-Date).AddMinutes(-1) -StartDateTime (Get-Date).AddMinutes(-6)
$reportid
$reportresult = Get-Five9ReportResult -Identifier $reportid
$timeZoneOffset =
@{
PST = 0
MST = 1
CST = 2
EST = 3
}
$localTimeZone = 'EST'
foreach($reportresult in $reportresult)
{
$VARDATE = $reportresult.Date
$VARTIME = $reportresult.TIME
$VARDATETIME = "$($VARDATE)T$VARTIME"
$REPORTDATETIME = ([datetime]$VARDATETIME).AddHours($timeZoneOffset[$localTimeZone]).ToString('s')
$DATE = $REPORTDATETIME.Substring(0,10)
$HOUR = "$($REPORTDATETIME.Substring(11,3))00:00"
$MONTH = $REPORTDATETIME.Substring(5,2)
$TIME = $REPORTDATETIME.Substring(11,8)
$YEAR = $REPORTDATETIME.Substring(0,4)
${REPORT RESULTS} = $reportresult.("REPORT RESULTs")
$insertquery="
Insert into [dbo].[table]
(
[VALUE],
//many other values
)
values
(
'$($DATE -replace "'", "''")',
'$($HOUR -replace "'", "''")',
'$($MONTH -replace "'", "''")',
'$($TIME -replace "'", "''")',
'$($YEAR -replace "'", "''")',
'$($VALUE -replace "'", "''")',
'$(${SPACED VALUE} -replace "'", "''")',
//many other values
'$("${VALUE}${VALUE}${VALUE}${VALUE}${VALUE}${VALUE}${VALUE}${VALUE}")' //Combined from several fields to serve as unique key
)
GO
"
Invoke-Sqlcmd -ServerInstance 'server' -query $insertquery -Username user -Password ("password") -database database
};```