. . "I have a query which is similar to one above and when i use this in stored procedure it runs indefinitely without giving any output." . .