Logging ALL Queries on a SQL Server 2008 Express Database? - sql

Is there a way to tell SQL Server 2008 Express to log every query (including each and every SELECT Query!) into a file?
It's a Development machine, so the negative side effects of logging Select-Queries are not an issue.
Before someone suggests using the SQL Profiler: This is not available in Express (does anyone know if it's available in the Web Edition?) and i'm looking for a way to log queries even when I am away.

SQL Server Profiler:
File → New Trace
The "General" Tab is displayed.
Here you can choose "Save to file:" so its logged to a file.
View the "Event Selection" Tab
Select the items you want to log.
TSQL → SQL:BatchStarting will get you sql selects
Stored Procedures → RPC:Completed will get you Stored Procedures.
More information from Microsoft: SQL Server 2008 Books Online - Using SQL Server Profiler
Update - SQL Express Edition:
A comment was made that MS SQL Server Profiler is not available for the express edition.
There does appear to be a free alternative: Profiler for Microsoft SQL Server 2005 Express Edition

There is one more way to get information about queries that has been executed on MS SQL Server Express described here.
Briefly, it runs smart query to system tables and gets info(text, time executed) about queries(or cached query plans if needed). Thus you can get info about executed queries without profiler in MSSQL 2008 Express edition.
SELECT deqs.last_execution_time AS [Time], dest.TEXT AS [Query]
FROM sys.dm_exec_query_stats AS deqs
CROSS APPLY sys.dm_exec_sql_text(deqs.sql_handle) AS dest
ORDER BY deqs.last_execution_time DESC

…Late answer but I hope it would be useful to other readers here…
Using SQL Server Express with advanced auditing requirements such as this is not really optimal unless it’s only in development environment.
You can use traces (www.broes.nl/2011/10/profiling-on-sql-server-express/) to get the data you need but you’d have to parse these yourself.
There are third party tools that can do this but their cost will be quite high. Log explorer from ApexSQL can log everything but select and Idera’s compliance manager will log select statements as well but it’s cost is a lot higher.

You can log changes. SQL Server 2008 will make this especially easy with Change Data Capture. But SQL Server isn't very good at logging SELECTs.
It is theoretically possible with the profiler, but it will kill your performance. You might "get away with it" on your desktop, but I think you'll notice your machine acting slow enough to cause problems. And it definitely won't work after any kind of deployment.
One important point a couple others have missed already: unless they changed something for 2008 I didn't hear about, you can't trigger a SELECT.

Just for the record, I'm including the hints to use DataWizard's SQL Performance Profiler as a separate answer since it's really the opposite to the answer pointing at SQL Server Profiler.
There is a free trial for 14 days, but even if you need to buy it, it's only $20 for 3 servers (at the moment of writing, 2012-06-28). This seems more than fair to me considering the thousands everybody using SQL Server Express edition has saved.
I've only used the trial so far and it offers exactly what the OP was looking for: a way to trace all queries coming in to a specific database. It also offers to export a trace to an XML file. The paid version offers some more features but I haven't tried them yet.
Disclaimer: I'm just another developer messing with DBs from time to time and I'm in no way affiliated with DataWizard. I just so happened to like their tool and wanted to let people know it existed as it's helped me out with profiling my SQL Server Express installation.

I would either use triggers or use a third party software such as Red Gate to check out your SQL log files.

Seems that you can create traces using T-SQL
http://support.microsoft.com/kb/283790/
That might help.

The SQL query below can show simple query logs:
SELECT last_execution_time, text
FROM sys.dm_exec_query_stats stats
CROSS APPLY sys.dm_exec_sql_text(stats.sql_handle)
ORDER BY last_execution_time
This is how it looks like below:

Related

How can I see which tables are changed in SQL Server?

I have a database which I didn't make and now I have to work on that database. I have to insert some information, but some information must be saved in not one table but several tables. I
can use the program which have made the database and insert information with that. While I am doing that, I want to see that which tables are updated. I heard that SQL Server Management Studio has a tool or something which make us see changes.
Do you know something like that? If you don't, how can I see changes on the database's tables? If you don't understand my question, please ask what I mean. Thanks
Edit : Yes absolutely Sql Profiler is what I want but I am using SQL Server 2008 R2 Express and in Express edition, Sql Profiler tool does not exist in Tools menu option. Now I am looking for how to add it.
Edit 2 : Thank you all especially #SchmitzIT for his pictured answer. I upgraded my SQL Server Management Studio from 2008 R2 express edition to 2012 Web Developer Edition. SQL Profiller Trace definitely works.
I agree with #Lmu92. SQL Server Profiler is what you want.
From SQL Server Management Studio, click on the "Tools" menu option, and then select to use "SQL SErver Profiler" to launch the tool. The profier will allow you to see statements executed against the database in real time, along with statistics on these statements (time spent handling the request, as well as stats on the impact of a statement on the server itself).
The statistics can be a real help when you're troubleshooting performance, as it can help you identify long running queries, or queries that have a significant impact on your disk system.
On a busy database, you might end up seeing a lot of information zip by, so the key to figuring out what's happening behind the scenes is to ensure that you implement proper filtering on the events.
To do so, after you connect Profiler to your server, in the "Trace properties" screen, click the "Events Selection" tab:
You probably are good to uncheck the boxes in front of the "Audit" columns, as they are not relevant for your specific issue. However, the important bit on this screen is the "Column filters" button:
This is where you will be able to implement filters that only show you the data you want to see. You can, for instance, add a filter to the "ApplicationName", to ensure you only see events generated by an application with the name you specify. Simply click on the "+" sign next to "Like", and you will be able to fill in an application name in the textbox.
You can choose to add additional filters if you want (like "NTUsername" to filter by AD username, or "LoginName" for an SQL Server user.
Once you are satisfied with the results, click "OK", and you will hopefully start seeing some results. Then you can simply use the app to perform the task you want while the profiler trace runs, and stop it once you are done.
You can then scroll through the collected data to see what exactly it has been doing to your database. Results can also be stored as a table for easy querying.
Hope this helps.
Although you describe in your question what you want, you don't explain why you want it. This would be helpful to properly answer your question.
ExpressProfiler is a free profiler that might meet your needs.
If you're looking to track DDL changes to your database, rather than all queries made against it, you might find SQL Lighthouse useful, once it is released in Beta shortly.
Disclosure: I work for Red Gate.
Do you know something like that? If you don't, how can I see changes
on the database's tables? If you don't understand my question, please
ask what I mean. Thanks
Red gate makes a lot of great tools. You can always make a database comparison. That will require that you make a backup, then doing whatever you want to do, and the compare the two databases, there is a free trial, works perfect to compare which tables has changed (deleted/inserted and modified)
http://www.red-gate.com/products/sql-development/sql-data-compare/
what you might maybe be looking for is "SQL Server Profiler". It's a SQL monitor where you can see all the SQL queries running real time.
http://technet.microsoft.com/en-us/library/aa173918(v=sql.80).aspx
Hope that helps you.
As far as I know SQL Profiler tool is not included in the Express edition of SQL Server (only Developer edition contains it). Maybe you can purchase it separately.
If you want you can try this https://sites.google.com/site/sqlprofiler/ free tool for profiling. Maybe it has limitations, but it you can check it.
There are also some profiling tools for integration with Visual studio, and also the ReSharper has InteliTrace (if you are using the tool at all).
Cheers.

New to SQL servers, wanting to schedule table / data moves every X hours

I have looked at a few stackoverflow forum posts but nothing fits (or atleast I dont think so) what I need help with.
I'm looking for general advise, my company has 'tasked' me to look at moving some data from tables stored in our parent companies databases into a database of our own that has all the information we need in one place.
For instance if we want information that related to one thing, we may have to pull data from several different databases. I think I can get my head around the moving of the data and create a sql query to do it, however we're currently using SQL express as our SQL db (the company is more than happy to buy/create a SQL server but as far as we can see SQL express does what we need it too (feel free to correct me)).
I need to look at scheduling the data move for every hour or few hours to keep the data 'up to date' for when reports are generated using the data.
I have looked at a few programs but the as the queries and the database is on a server 2008 r2 system some of the 'programs' don't like it as they were last updated pre 2010 etc. I have also installed SQL management suite 2012 due to SQL server agent but I cant even get that worked (Service is enabled and I have restarted the DB just still nothing within suite).
I'm not looking (however happy to take the help) for a 'Do this and that and that' type reply but more than happy to accept that amount of help but if you guys / gals can point me in the right direction.
Summary:
-Combining data already on databases from our parent company into a table / DB of our own making
-Currently using SQL Express but willing to upgrade to something else that does the job
-Schedule the data moves for every X hours (Windows scheduling?)
-automating the entire thing so don't have to manually do the moves.
Help on any of the points above would be greatly appreciated and I would 'love you long time' for the help.
JB
There are a bunch of limitations for SQL Express. One of them is that SQL Agent is not supported. SSIS like SQL Agent is not supported.
http://msdn.microsoft.com/en-us/library/cc645993.aspx
Do not fret, you can always schedule a job with Windows Scheduler.
http://windows.microsoft.com/en-US/windows/schedule-task#1TC=windows-7
As for moving the data, it is up to you to select a solution.
1 - Write a PowerShell application to perform the Extract, Translate, and Load (ETL).
http://technet.microsoft.com/en-us/library/cc281945(v=sql.105).aspx
2 - Use the SQLCMD to perform logic like calling stored procedures.
http://technet.microsoft.com/en-us/library/ms162773.aspx
3 - Use BCP to dump and load data.
http://craftydba.com/?p=1245
http://craftydba.com/?p=1255
http://craftydba.com/?p=1584
http://craftydba.com/?p=1690
It is funny how youngsters think they need to spend a-lot of $ to create a solution for a business.
However, Microsoft does supply you with a-lot of free tools.
You just have to put them together for a solution.
PS: I remember about 10 years ago I created a custom ETL solution using VBSCRIPT. Unlike power shell, it is installed on old and new programs.
Good luck!
You can create a console application which executes that particular stored procedure which handles your logic. ( http://dotnet.dzone.com/articles/basics-stored-procedures-net )
Of course using SSIS is much easier but it's not available in SQL Server Express Edition.
I think you should have a look at Integartion Services, which is not available for Express Edition. Have a look at this article to get started with SSIS.

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.

Is there a way to track all the queries that has been executed by the server?

I want to be able to see all the queries that has been executed on the server last 2 days etc.
see the script, date of execution, sender etc.
is there any way?
I am using SQL X 2005.
I don't believe it's possible without SQL Server Profiler running.
Yes you can use SQL-Trace to log each command submitted to the server. It's the same mechanism used by the profiler, but you do not have to have the profiler or any other tool to use it.
There are two modes in which SQL Trace can run - in-memory buffer and disk file. The former is only used by profiler, is not documented and should not be used. Use the disk-file mode. The file can later on be opened on the same or different machine and even loaded into a table for analysis.
To learn more go to this page: http://msdn.microsoft.com/en-us/library/ms191511.aspx and search for section titled "To perform monitoring tasks with SQL Trace by using Transact-SQL stored procedures"
Here is a free, open-source Profiler tool that might help.
Profiler for Microsoft SQL Server 2005/2008 Express Edition

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