sanjaytiwari31 (2/4/2013)
Can anybody tell me how to optimize this function or where is the problem.
Yes I can but you're not going to like it. The code cannot be optimized no matter how much you play with it because of the nested cursors. It would be a far better thing sack the code entirely and rewrite it from the ground up without the cursors.
Because of the length of the code, I've not done a serious deep dive on it but I agree with what has already been said... it looks like it could be resolved by a single relatively simple query across 4 tables. If no one in your shop can handle the conversion, your shop should get some hired help. 😛
--Jeff Moden
Change is inevitable... Change for the better is not.