Search SupportSQL
Sunday
10May2009

Shrink All Database Log Files at Once

Use this batch to quickly shrink all database log files, freeing up any space at the end of the file. To shrink even more, first complete a full database backup, then run this:

Click to read more ...

Sunday
26Apr2009

“Security management” series – audit all granted protects – all databases

This installment is part of a “Security Management” series of articles for SQL Server.

We have covered getting all security principles and their role memberships, and the role/group memberships of those, to the top of the membership tree. This procedure collects all the security granted to principles/roles/groups for all databases on the SQL Server instance.

Click to read more ...

Sunday
26Apr2009

get row counts for all tables in a database

This proc returns the total row counts for each table in a database.

I use this to track and analyze table growth rates by record count across all databases by using this as the input query to an insert statement that adds all the row counts and the current date/time. Then trend lines can be plotted to gauge growth rates and enable capacity forecasting.

Click to read more ...

Thursday
16Apr2009

Query to Make a Horizontal Table Row Vertical

—Contributed by William Cassidy, Collectors Universe, Inc. (a unique and awesome developer) – Thanks William!

—Run these queries to generate a series of SQL statements.
—Tweak the source table and WHERE clause if needed.
—Afterward, copy the generated SQL statements into Management Studio and run them.  They take about 25 seconds.
—Then, you’ll have a table #temp that contains the values (cast as varchars) of all columns in the table.

Click to read more ...

Sunday
12Apr2009

“Homegrown Compliance” Series – default trace Data Acquisition and sample view

This is a part in a series of “HOMEGROWN COMPLIANCE” articles dealing with various aspects of meeting compliance related goals in the current general corporate compliance environment (ISO, SOX, ISA, CPI, etc.)

This installment provides the stored procedure and description how to collect the data from the default trace that runs, you guessed it, by default in SQL Server 2005. Also included is a view to join to event descriptions for key fields. What is nice is that additional traces, hindering performance, are not needed for this approach as the default trace is usually already consuming resources. This method provides a way to utilize that trace data.

Click to read more ...

Sunday
29Mar2009

“Security Management” Series – Audit All security memberships – Principles Membership Tree

This installment is part of the “Security Management” series of articles for SQL Server.

Find all group memberships, and their group memberships, all the way to the highest group/role/principle membership. This will be correlated and joined to another procedure (in an article to be published soon) together returning all granted and inherited protects on the SQL Server instance for every principle and every role/group.

Used in several areas e.g. compliance, general security auditing, baseline comparisons, security inquiries, deployment cleanups, etc.

Find all principles with memberships to groups/roles and memberships of those groups/roles all the way to the top principle:

Just a few example uses:

  • Find all sysadmin members
  • Find all db_datawriter memberships
  • Find all memberships for a principle/group/role, etc.
  • Audit all principle memberships against the approved baseline.

Click to read more ...

Saturday
21Mar2009

Blocking info – waiter and blocker quick info

This is a resource light query that can be run repetitively to identify blocking or used at the time of blocking to collect/report the pertinent information quickly, and then possibly email or log that for review. It can be very useful to collect and analyze blocking data through key trend charts, i.e. application/traffic growth compared to blocking trend, maintenance effects on application blocking, new deployment monitoring and cross server blocking.

Click to read more ...

Monday
16Mar2009

moot modify scripting in SSMS: the never-modifying-modify-script

…a little gotcha for setting settings in SSMS, if you turn on the option to script an IF NOT EXISTS clause then this is what the modify stored procedure script comes out as below (just the first part pasted). Ouch! This will never modify anything and still return: Command(s) completed successfully.

Click to read more ...

Sunday
15Mar2009

BCP All Tables Into A Directory

Uses xp_cmdshell (note: it is not a recommended practice to enable these procs) but is a useful tool to bcp all tables in a database (@dbname) into a directory (@filepath i.e. ‘c:\’ ).

Click to read more ...

Monday
23Feb2009

Fetch - the good Active Directory task execution doogie

Sunday
22Feb2009

Database Stats - Quick Facts

Very useful in identifying all kinds of valuable database statistics and information, e.g. non-standard settings, large files, incorrect recovery modes, database files located on system drive, etc.

Click to read more ...

Sunday
22Feb2009

When you have to kill, kill all and kill quick... KILLING SPIDS FOR SINGLE USER ACCESS

Problem:

Say goodbye to the frustrations over getting quick, exclusive access to a database during the restore or repair of that database while end users keep gaining access without cease. This sp will get the database into single-user mode fast after diligently 86ing all spids.  The issue is that you can kill and kill spids but other users can still get into the db until the ‘set single user mode’ is executed. A looping kill statement with a poised ‘set single_user’ statement works nice. For heavy traffic environments, increase the max loop limit and shorten the wait time value.

Basic logic:

1. kill all users in the db

2. verify all are killed

3. re-kill if needed (up to 10 retries a few seconds apart)

4. immediately set the database in single user mode for the current query window (where restores/repairs can be done)

5. provide the statement to set database back to multi-user mode if needed.

(note - this will only execute specifically for users in the ACL list (see the code below) and will not work on system databases)

Click to read more ...

Sunday
08Feb2009

“SQL Agent Toolbox” Series – Job Steps Missed

This is an installment in the “SQL Agent Toolbox” series of articles for SQL Server.

Question: How do I know if a job for some reason did not fire as was called for by the schedule?

Reporting failed jobs leaves out an important notification that is rarely caught by most task management systems: Missed jobs! This proc will return jobs that didn’t make it to run as was expected by the scheduling system behind SQL Server Agent.

Click to read more ...

Sunday
01Feb2009

“SQL Agent Toolbox” Series – Job Steps Failed

This is an installment in the “SQL Agent Toolbox” series of articles for SQL Server.

This proc returns all the failed job steps from SQL Server Agent history tables. This is useful to see how many times a job has failed regardless of notifications and the current run status of the job.

Click to read more ...