How to: Log Long-Running Queries (ODBC)
New: 14 April 2006
A complete sample is available that shows the creation of a long-running query log file. The complete sample code is in the file LogLongRunningQuery.cpp, which you can download from the SQL Server Downloads page on MSDN. This sample was developed using Microsoft Visual C++ 2005 and was developed for ODBC version 3.0 or later.
|When possible, use Windows Authentication. If Windows Authentication is not available, prompt users to enter their credentials at run time. Avoid storing credentials in a file. If you must persist credentials, you should encrypt them with the Win32 crypto API.|
To log long-running queries using ODBC Administrator
In Control Panel, double-click Administrative Tools and then double-click Data Sources (ODBC). (Alternatively, you can run odbcad32.exe from the command prompt.)
Click the User DSN, System DSN, or File DSN tab.
Click the data source for which to log long-running queries.
In the Microsoft SQL Server Configure DSN Wizard, navigate to the page with Save long-running queries to the log file.
Select Save long-running queries to the log file. In the box, place the name of the file where the long-running queries should be logged. Optionally, click Browse to browse the file system for the query log.
Set a query time-out interval, in milliseconds, in the Long query time (milliseconds) box.
To log long-running queries data programmatically
Call SQLSetConnectAttr with SQL_COPT_SS_PERF_QUERY_LOG and the full path and file name of the long-running query log file. For example:
Call SQLSetConnectAttr with SQL_COPT_SS_PERF_QUERY_INTERVAL and set to the time-out interval, in milliseconds.
Call SQLSetConnectAttr with SQL_COPT_SS_PERF_QUERY and SQL_PERF_START to start logging long-running queries.
Call SQLSetConnectAttr with SQL_COPT_SS_PERF_QUERY and SQL_PERF_STOP to stop logging long-running queries.