Vienas iš variantų - user defined fuctions: http://www.developer.com/net/csharp/article.php/3399881/Implement-User-defined-Functions-in-SQL-Server-2005-with-Managed-Code.htm