SQL Server Management Studio - history of commands run - sql-server

I have previously altered a table, added a column and modify a stored procedure.
I do not remember exactly which table and which stored procedure,
is there some log in sql management studio about what changes were done in a particular DB?
I have tried what is described here How to see query history in SQL Server Management Studio but i have not found the changes made to the DB

There's a few ways that you can get this information. Firstly you could try the standard reports --> Schema Changes History.
The information for this comes from:
SELECT cat.name AS Category
, b.name AS EventCaptured
, c.name AS ColumnCaptured
FROM fn_trace_geteventinfo(1) AS a
INNER JOIN sys.trace_events AS b
ON a.eventid = b.trace_event_id
INNER JOIN sys.trace_columns AS c
ON a.columnid = c.trace_column_id
INNER JOIN sys.trace_categories AS cat
ON b.category_id = cat.category_id
ORDER BY Category, EventCaptured, ColumnCaptured
Alternatively, query sys.traces to find the location of the default trace and feed this into fn_trace_gettable as per below.
SELECT *
FROM fn_trace_gettable
('C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\LOG\log.trc', default)

You can use Security\Server Audit Specification and enable DATABASE_OBJECT_CHANGE_GROUP audit on your database.
Use following reference in order to use SQL Server server audit.
CREATE SERVER AUDIT SPECIFICATION (Transact-SQL)
Create a Server Audit and Server Audit Specification

You can use the modify_date column in sys.objects table
SELECT *
FROM SYS.OBJECTS
WHERE Modify_Date BETWEEN <date_of_modification> AND <date_of_modification> + 1
and then you can try to narrow it down.
You can be even more specific and run the query for just tables and stored procedures.
SELECT *
FROM SYS.objects
WHERE TYPE IN ('IT', 'S', 'U', 'P', 'PC', 'X')
AND modify_date BETWEEN '10-Jun-2014' AND '11-Jun-2014'

I develop SSMSBoost add-in for SSMS and we have "Executed Query history" there. All actions are logged with timestamp, connection information and execution result. This will certainly only work, if you do all changes from SSMS with SSMSBoost installed. If someone will perform changes from other machine you will not see them, until he uses SSMSBoost as well and you share execution history.

Related

Audit Procedures used on SQL Server DB

I've inherited a database recently which contains thousands of stored procedures and functions, however most of them are deprecated and no longer in use.
I've started adding a piece of code to the stored procedures one at time to notify me if they run, but this process is really quite manual.
Is there any way to start an audit, and see which stored procedures run in the next month or two without adding a piece of code to each stored procedure manually?
Thanks,
Eric
I believe you need to be on SQL Server 2005 SP2 or higher. In prior versions of SQL Server, the OBJECT_NAME function only accepts a parameter for object_id.
Hopefully this should work for you:
SELECT DB_NAME(dest.[dbid]) AS 'databaseName'
, OBJECT_NAME(dest.objectid) AS 'procName'
, MAX(deqs.last_execution_time) AS 'last_execution'
FROM sys.dm_exec_query_stats AS deqs
CROSS APPLY sys.dm_exec_sql_text(deqs.sql_handle) AS dest
WHERE dest.[TEXT] LIKE '%yourTableName%' -- replace
And dest.[dbid] = DB_ID() -- exclude ad-hocs
GROUP BY DB_NAME(dest.[dbid])
, OBJECT_NAME(dest.objectid)
ORDER BY databaseName
, procName
OPTION (MaxDop 1);

SQL Server 2008 SSIS Export Wizard - find out how long it took

We ran a SQL Server 2008 Export wizard overnight to copy 100+ tables from one server to another. The process worked fine (if a little slow over our network)
The report produced at the end does not show a start and end time for the operation for some unknown reason
I know the names of all the tables created (they are brand new on the target server) - is there any SQL I can run against sys.tables or a similar table that can show the last write time against the table?
The create_date value on sys.tables seems to imply that the export wizard creates empty copies of each table before starting the data insert rather than doing each one in turn
You can get an estimation by looking at the index usage statistics
SELECT Object_Name(object_id) As object
, Max(last_user_update) As last_update
FROM sys.dm_db_index_usage_stats
WHERE database_id = DB_ID()
GROUP
BY Object_Name(object_id)
ORDER
BY object

Query to return internal details about stored function in SQL Server database

I have been given access to a SQL Server database that is currently used by 3rd party app. As such, I don't have any documentation on how that application stores the data or how it retrieves it.
I can figure a few things out based on the names of various tables and the parameters that the user-defined functions takes and returns, but I'm still getting errors at every other turn.
I was thinking that it would be really helpful if I could see what the stored functions were doing with the parameters given to return the output. Right now all I've been able to figure out is how to query for the input parameters and the output columns.
Is there any built-in information_schema table that will expose what the function is doing between input and output?
If you can execute a query against your database somehow, and if you have the necessary permissions to read the system catalog views, then you could run this query to get the name, the definition (SQL code) and a few more bits of information about your functions:
SELECT
obj.name ,
obj.type ,
obj.type_desc ,
obj.create_date ,
obj.modify_date ,
m.definition ,
m.is_schema_bound
FROM
sys.objects obj
INNER JOIN
sys.sql_modules m ON obj.object_id = m.object_id
WHERE
obj.type IN ('AF', 'FN', 'FS', 'FT', 'IF', 'TF')
Provided you have appropriate permissions, you can simply script out all Stored Procedures and Functions:
Right-click on your database in SSMS (SQL Server Management Studio), select Tasks –> Generate Scripts, ensure your database is highlighted and click next. Ensure the options to script out Stored Procedures and Functions are selected.
You can install SSMS (client Tools) without requiring a SQL Server license.
Another way is sp_helptext which will show you the source of the passed SP or UDF;
sp_helptext fnBlaDeBla

Backup Stored Procedures

What is the query to backup the stored procedure of a database in SQL Server 2000?
In SQL Server 2000, you can use this query to list out the complete text of stored procedures, they can span multiple rows.
SELECT
o.name,o.id,o.xtype, c.colid, c.text
FROM dbo.sysobjects o
INNER JOIN dbo.syscomments c ON o.id = c.id
WHERE o.xtype = 'p'
ORDER BY o.Name,c.colid
I would be easier to use Enterprise Manager to script all the procedures though. In Enterprise Manager, right click on the database to you want to capture all the procedures from. An options list will pop-up, select "All Tasks" then "Generate SQL Script...". A dialogue box will appear, click on "show all", you can then refine the list of objects to script, by using the check boxes. Select the objects on the left side and click on the "Add>>" to move them to the script list. You can set formatting and other options, then click OK when done.
In SQl Server 2005+ you can use this query to list the complete text of all stored procedures, views and functions:
SELECT
LEFT(o.name, 100) AS Object_Name,o.type_desc,m.definition
FROM sys.sql_modules m
INNER JOIN sys.objects o ON m.object_id=o.object_id
you can take this output and save it if you like.
However, it is easier to use SQL Server management Studio to script out all procedures.

How can I get a list of all of the user databases via t-sql?

I want to get a list of all of the user databases from an mssql server instance. What's the best way to do this?
I know I can select from sys.databases, but I don't see any way to filter out system databases besides hardcoding a list of names to exclude.
I need the script to work on 2000/2005 and 2008.
If the approach I listed above is the only way to go, what are list of names I should exclude? I don't know if 2005 or 2008 added any new system databases off the top of my head.
Was looking in to this again today and decided to profile what Management Studio was doing to populate the Object Explorer details.
Turns out the solution Microsoft have implemented is pretty simplistic and boils down to the following:
SELECT *
FROM master.sys.databases
WHERE Cast(CASE WHEN name IN ('master', 'model', 'msdb', 'tempdb') THEN 1 ELSE is_distributor END As bit) = 0
Please note that this was performed using SSMS 2008R2 (10.50.4033.0).
The first query will return a table with data regarding all of the databases on the instance:
Select *
From sys.databases
From this table you'll notice you can narrow down the scope of data you're looking for by using the WHERE clause. For example, the following queries will essentially return the same result table (the one you're most likely looking for):
Select *
From sys.databases
Where database_id > 5
Select *
From sys.databases
Where len(owner_sid)>1
These queries will work in SQL Server 2008 and 2012.
On SQL Server 2008 R2 Express, looks like I cannot reliably use any of the above methods. INFORMATION_SCHEMA.SCHEMATA only shows me information in the current database, db_id (database_id) #5 is my first user database, and owner_sid on two of my user databases on one of my mirrored databases (running on SQL Server 2008 R2 Standard) shows owner_sid = 1 for my two most recently created databases. (PablolnNZ's comment above is correct: I did not set an explicit owner for those two databases so it still shows as having an owner of 'sa'.)
The only reliable means I was able to use was the following:
SELECT name FROM sys.databases
WHERE name NOT IN ('master', 'model', 'tempdb', 'msdb', 'Resource')
This works in 2005, not 100% sure about the other versions but I think it will fly. It's a bit of a hack but might get you what you need:
select * from sys.databases where len(owner_sid)>1
As nasty as it sounds to hardcode things. The names and number of system databases has been fairly consistent for several versions of SQL. However, if that is too unpleasant you could semi-hardcode them into a table and then plug that into your query.
Not sure if you can offhand. One note -- on 2k you'll have to use master.dbo.sysdatabases and not master.sys.databases (which doesn't exist in 2k).
Starting with SQL Server 2008 you have access to a view called sys.databases which when joined with sys.server_principals can eliminate the databases owned by sa, which you can (most often) safely discern are the "system databases". Thus, allowing you to filter these items out.
select
d.name
,d.database_id
from
sys.databases d
join
sys.server_principals p
on p.sid = d.owner_sid
where
p.name <> 'sa';

Resources