In my PowerShell script, I will query the database using the select statement and it will run every one hour. It will took a quite longer period for completing all the process each time the scheduler executed, so when the next scheduler executed, it might query the old data where its still processing by the previous scheduler. May I know whether any method to prevent this? From what I read (this link) there is a method to achieve it.
Even I changed my query+PowerShell into the following, I found it still not able to achieve the things that I want
$queryAuth2="select * from [StagingDB].[dbo].[UnixAccResetPassword] WITH (HOLDLOCK,READPAST)"
#write-host "The command is : "$queryAuth
$command2=$connection.CreateCommand()
$command2.CommandText=$queryAuth2
$reader2 = $command2.ExecuteReader()
#write-host "The reader is : "$reader
#start-sleep -seconds 180
while ($reader2.read())
{
# write-host "In reader 1"
for ($i = 0; $i -lt $reader2.FieldCount; $i++)
{
# write-host "In reader 2"
$pfno=$reader2.GetValue($i)
write-host "pfno :"$pfno
}
}
The thing I want to achieve is when the select statement query the 5 records(item1,item2,item3,item4,item5
) from table, it will locked until those records finished processed and remove away from table.