You can call extended stored procedures from a function .
Here are some examples:
- xp_cmdshell
- xp_regwrite
- xp_logevent
If you had the correct permissions, theoretically you could call an extended stored procedure from your function and store information such as APP_NAME () and ORIGINAL_LOGIN () in a simple file or in a registry key.
Another option is to create an extended stored procedure from scratch .
If all this has too many problems, I would follow an early recommendation of SQL Profiler or server side tracing .
An example of using the extended stored procedure is given below. This uses xp_logevent to log each instance of a function call in the Windows application log.
One caveat to this method is that if a function is applied to a column in a SELECT query, it will be called for each row returned. This means that it is possible to quickly fill out a journal.
The code:
USE [master] GO GRANT EXECUTE ON xp_logevent TO PUBLIC GO USE [Sandbox] GO CREATE FUNCTION ufx_Function() RETURNS INT AS BEGIN DECLARE @msg VARCHAR(4000), @login SYSNAME, @app SYSNAME SET @login = ORIGINAL_LOGIN() SET @app = APP_NAME() SET @msg = 'The function ufx_Function was executed by ' + @login + ' using the application ' + @app EXEC master.dbo.xp_logevent 60000, @msg, warning RETURN 1 END GO SELECT dbo.ufx_Function()
source share