I found 2 problems with the tsf_user function;
- If I use the function as DBA (with a direct login to the database) the function gives a guid.
- The function can confuse the SQL-Server query optimizer with the result that the function gets executed multiple times.
As a workaround I replaced the function by a view, which checks if the CONTEXT_INFO value isn’t a GUID, and an additional check whether the user returned is actually present in our user table.
I did not investigate what it would do with the performance when I made the function schema bound, but the view is much faster… and I think that making the function schema bound might give the same result. ( It would not bind to anything, but SQL-Server might understand that there is no need to evaluate the function more then once ).