Skip to content
Advertisement

Looping in SQL with TempTable

I am new to SQL Server, can you please help me to write query for my scenario?

I have this Table1 :

Input to my stored procedure is a PolicyNumber e.g 1234 and the code should return 5 rows.

I need to cover these cases:

  1. If the first payment is not cancelled, I need return that row.

  2. If the first payment is cancelled, look for a next non-cancelled payment. If a next payment is found that was not cancelled, I need to return that row.

My question is: I think I need to loop all the data for that policy till I get a non-cancelled payment, please let me know how do I that.

Advertisement

Answer

I think you just need to take NULLs into account:

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement