SQL Server has CLR integration (albeit at last check, only with .NET 2.0). This means you can run some assembly marked as such from a function/stored-procedure/query. Here is the documentation:
MSDN SQL Server CLR description
You can write an assembly with an exposed method for SQL Server and run it directly from there, thus completing your loop in C#/VB/.NET language of your choosing. This may be a little heavy handed if none of the above answers have what you're looking for, but I'm pretty sure offers the most flexible approach where you control everything.