Some users want to know if there is a way to monitor events on SQL server without using SQL Profiler. Yes, there is: the engine support behind SQL Profiler is the feature called SQL Trace which is introduced in SQL 2005. SQL Trace provides a set of stored procedures to create traces on an instance of the SQL Server Database Engine. These system stored procedures can be used from within user's own applications to create traces manually, and allows to write custom applications specific to their needs.
The following sample code shows how to create customized SQL trace to monitor events to user's interest
-- sys.traces shows the existing sql traces on the serverSELECT * FROM sys.tracesgo--create a new trace, make sure the @tracefile must NOT exist on the disk yetDECLARE @tracefile NVARCHAR(500) SET @tracefile=N'c:\temp\newtraceFile'DECLARE @trace_id INTDECLARE @maxsize BIGINTSET @maxsize =1EXEC sp_trace_create @trace_id OUTPUT,2,@tracefile ,@maxsizego--- add the events of insterest to be traced, and add the result columns of interest-- Note: look up in sys.traces to find the @trace_id, here assuming this is the first trace in the server, therefor @trace_id=1DECLARE @trace_id INT = 1, @on BIT = 1, @current_num INTSET @current_num =1WHILE(@current_num <65)BEGIN--add events to be traced, id 14 is the login event, you add other events per your own requirements, the event id can be found @ BOL http://msdn.microsoft.com/en-us/library/ms186265.aspxEXEC sp_trace_setevent @trace_id,14, @current_num,@onSET @current_num=@current_num+1ENDgo--turn on the trace: status=1-- use sys.traces to find the @trace_id, here assuming this is the first trace in the server, so @trace_id=1DECLARE @trace_id INTSET @trace_id=1EXEC sp_trace_setstatus @trace_id,1--pivot the traced eventSELECT LoginName,DatabaseName,* FROM ::FN_TRACE_GETTABLE(N'c:\temp\newtraceFile.trc',DEFAULT)go-- stop trace. Please manually delete the trace file on the disk-- use sys.traces to find the @trace_id, here assuming this is the first trace in the server, so @trace_id=1DECLARE @trace_id INTSET @trace_id=1EXEC sp_trace_setstatus @trace_id,0EXEC sp_trace_setstatus @trace_id,2go