Configure MS SQL Server to log query and its execution time - sql

I had a program using SQLconnector to connect to MS SQL 2012, I want know SQL query execution time. I am aware there is a SQL Server Profiler, but I can't configure it correctly to capture any query execution time. I am also aware that I can add timer within program but i can not change code easily.
All advice welcome. thanks

AFAIK SQLConnector is for MySQL, so I don't understand how you can work with SQLServer 2012.
If you need profiling in SQLServer - there is nothing better than SQL Server Profiler.
I can't configure it correctly to capture any query execution time
It must be easy:
Event Selecion - you need SQL:Stmt Completed (uncheck everything else). You need to select duration.
Column Filters - create filter by Login Name (make special login if you need it), TextData, Spid (if you can figure out how to obtain it)

This is not supported by default but you can do this using several techniques depending on what are your needs.
Do you need to capture every statement including SELECT? If yes then I suggest you still use SQL Server Profiler or SQL Server Traces
If you only need to capture DML statements (excluding SELECT) you can try setting up triggers on tables you want to audit.
There are also DDL triggers that can help you catch DDL statements (as far as I know there is no way to capture all DDL statements using DDL triggers).
So, there are many options here but it all depends on what are your needs.

Related

View SQL Blocks/Locks?

I was wondering if there is there a tool or a way to view which statements cause a block/lock and which objects would be affected? I know about sp_who and sp_who2, but those only work while the system is running.
For example, I know running this:
UPDATE myTable SET col1 = 'something'
Will put a lock on "myTable", but there are more complex scenarios (like nested procs and triggers) that would be more difficult to identify.
I was hoping for tool like the "Actual Execution Plan" built-into SSMS, but other tools, queries will suffice
Thanks all
You can refer this blog and follow the different ways to identify the blocks:
sp_who2 System Stored Procedure
sys.dm_exec_requests DMV
Sys.dm_os_waiting_tasks
SQL Server Management Studio Activity Monitor
SQL Server Management Studio Reports
SQL Server Profiler
Also check: Different techniques to identify blocking in SQL Server
You can use SQL Activity Monitor (right click on the SQL instance in SSMS) to get a list the number of current locks etc and you can use the 'Task State' column of the 'Processes' panel in there to see currently running statements which will tell you if things are blocking.
These all focus on activities which are currently executing though, so I'm not sure this will be what you want.

Hiding statements from SQL Server Profiler (or other observers)

In SQL Server 2008 R2, I would like to execute a statement that I want to be invisible to the SQL Profiler or other means of observing user queries. Is there a way to control what is displayed by SQL profiler?
I would like to execute something like:
SELECT 'MyPassword' INTO #passwordTable
I don't want to show 'MyPassword' through SQL Server Profiler or other means. Any ideas?
Essentially, no, you can't. You used to be able to do this by adding a comment like this into the batch or statement:
-- sp_password
But this no longer works. Why aren't you hashing your password?
Well, you have to be a server administrator to run the SQL Profiler, so even if you could prevent it from seeing the command, the user could just go grab the password table anyway. Ideally you would be storing hashes of the passwords rather than the passwords, making any viewing from the profiler useless.
If you really want to try and keep the profiler from seeing the statements, you could try a third party tool like this: http://www.dbdefence.com/support/dbdefence-documentation/
I have no idea if it works though, or how reputable that company is.
Denis, Aaron is correct, there is nothing like an "invisible statement", you can't tweak SQL Profiler to NOT show statements: once aboard, one can see all statements running in the DB.
You need to obfuscate this sensible data before submitting it to the DB. There are some obfuscated methods available (one-way hash, symmetric algoritms, home-made methods), you need to choose the more suitable method to your needs and implement it. Unfortunatelly, there is no free-lunch to your case...
I have seen a product called DBDefence.
It hides SQL statements from the profiler completely. I do not know how do they do it.
I use free version because I have small database.
In earlier versions of SQL Server it was possible to add a comment --sp_password
but not in SQL Server 2008 and above.
I don't see the point, really. If one is able to view a query with SQL profiler, surely he could access the database to view the actual data.
The key is to not store sensitive data (like passwords) in clear text.
Preventing people to use SQL profiler will come down to applying the proper security configuration on your SQL Server.

SQL Server activity monitor show all queries

I'm using SQL Server 2005, and I'd like to know if there is any possibility to watch every query sent to SQL server?
SQL Server Management Studio has a built-in tool called the Activity Monitor which gives me nearly what I am searching for in the "Recent Expensive Queries Pane"
This pane shows information about the most expensive queries that have been run on the instance over the last 30 seconds.
I need exactly this for all recent queries, not just the expensive ones...
Can anyone help me?
Name a tool, or give me a hint what to search for...
To watch every query sent to SQL server using SQL profiler, you can use one of SQL Profiler predefined templates. It's TSQL_Duration.
It's designed to track completed stored procedures and TSQL code.
The trace will show you exactly the batch executed.
You can expand the events tracked and columns shown.You can find more information here: How To: Use SQL Profiler
The same feature is also available in ApexSQL Comply, an auditing tool that uses SQL traces to capture the events, then stores them in a central repository database, and provides built-in and custom reports where you can easily filter the transactions, and export them as Excel, Word, or PDF files.
Disclaimer: I work for ApexSQL as a Support Engineer
You're looking for SQL Profiler, it's usually installed along with SSMS.

Get SQL statement after parameters added

Is there a way (in MySQL) to get the pure SQL statement after parameters have been added? I'm having issues with one of my statements once I start using parameters, and I want to see what's being executed. Of course it has to do with dates.
Using MySQL .NET Connector. I have access to MySQL server, and I use SQLYog to administrate.
Thanks!
-Steve
You can use the query logs option to have all queries issued to the server logged.
See http://dev.mysql.com/doc/refman/5.1/en/server-logs.html for information about the log types and how to enable and configure them.

How can I monitor the executed sql statements on a SQL Server 2005

In a project of mine the SQL statements that are executed against a SQL Server are failing for some unknown reason. Some of the code is already used in production so debugging it is not an easy task. Therefore I need a way to see in the database itself what SQL statements are used, as the statements are generated at runtime by the project and could be flawed when certain conditions are met.
I therefore considered the possibility to monitor the incoming statements and check myself if I see any flaws.
The database is running on a SQL Server 2005, and I use SQL server management studio express as primary tool to manipulate the database. So my question is, what is the best way to do this?
Seeing how you use the Management Studio Express, I will assume you don't have access to the MSSQL 2005 client tools. If you do, install those, because it includes the SQL profiler which does exactly what you want (and more!). For more info about that one, see msdn.
I found this a while ago, because I was thinking about the exact same thing. I have access to the client tools myself, so I don't really need to yet, but that access is not unlimited (it's through my current job). If you try it out, let me know if it works ;-)
Best way is to fire up profiler, start a trace, save the trace and then rerun the statements