Changed proc to func so can be called in query