try/catch
-
ID col1 col2 col3 col4 rescol
121 0 1 2 6 YES
122 0 1 2 6 YES
123 0 0 9 0 NULLAbove are the Sql rows and if my function finds null for the ID=123 as 9 of col3 then i need to locate for before id's rescol and if that also find as null then i need to fetch for the above row of 121, like this till i am not getting res col as null i need to fix a loop how can i do this? In try catch block i am handling the above mentioned functionality as if i get error my catch block will call its above row's rescol value. but how many catch block i will put here because the rows are dynamic and unknown how many catch i will get executed.
-
ID col1 col2 col3 col4 rescol
121 0 1 2 6 YES
122 0 1 2 6 YES
123 0 0 9 0 NULLAbove are the Sql rows and if my function finds null for the ID=123 as 9 of col3 then i need to locate for before id's rescol and if that also find as null then i need to fetch for the above row of 121, like this till i am not getting res col as null i need to fix a loop how can i do this? In try catch block i am handling the above mentioned functionality as if i get error my catch block will call its above row's rescol value. but how many catch block i will put here because the rows are dynamic and unknown how many catch i will get executed.
If I understand your question correctly, you can try something like this:
SELECT TOP 1 rescol
FROM Table1
WHERE rescol IS NOT NULL
AND col3 = 9
AND [ID] BETWEEN 100 AND 200 -- Replace with appropriate values here
ORDER BY [ID] DESCAnd remember that
try catch
is only for handling exceptions, it is a very worst programming practice to use it for implementing logic within your application.