Jes Sie

Jes Sie

  • 704
  • 1.2k
  • 265.1k

Looping Through Each Table Row in SQL Server

Dec 9 2021 3:41 AM

I have 2 tables (Header and Details). The primary key for the header is PolicyNo and is a foreign key to the Details table. Each table has a field name IsPaid (bit). Every time the customer will pay to the cashier, the IsPaid field of the Details table will be updated to true. If all rows of the Details table will all be true, then the Header table IsPaid field should also be updated to true. This is easy if all rows will be paid at once. But often, it is not paid at the same time. Below is a scenario to make things clear.

A policy was created with 10 persons insured. Only 7 persons insured were paid. So, the Header IsPaid field is not set to true. After several days, the remaining 3 persons paid their account. So now, all the IsPaid field of the details table was set to true.

Question: How can I loop through each row in the Details table and check whether the  IsPaid field is already true? if all rows were true, how can I update the IsPaid field of the Header table?

Thank you all for your help


Answers (7)