You are here

Monthly archive

SQL Server – Get Size of Database Files

This script gets the size of each database file, not including backups. Essentially, you’ll see .MDF, .NDF, and .LDF files.

SELECT DISTINCT bus.database_name
, (buf.file_size / (1024*1024)) AS [File Size MB]
, buf.physical_name
FROM msdb..backupfile buf
INNER JOIN msdb..backupset bus
ON buf.backup_set_id = bus.backup_set_id
ORDER BY bus.database_name

TSQL – Get Number Of Cores & MaxDOP In Single Query

This is a handy script to show the number of cores and MaxDOP in a single output, along with a built-in script to change the value. The built-in script shows up as a query result so you can copy/paste it.

SELECT cpu_count AS [Cores] 
        , (SELECT value_in_use FROM sys.configurations WHERE name = 'max degree of parallelism') AS [MaxDOP]
        , '/*Next Line For SQLCMD Mode*/
sp_configure ''max degree of parallelism'', ''VALUE''; 
        ' AS [Configure Script]
FROM [sys].[dm_os_sys_info]

TSQL – Orphaned User Script

Finding Orphaned Users

USE [db]
EXEC sp_change_users_login 'Report'

Fixing Orphaned Users

USE [db]
EXEC sp_change_users_login 'auto_fix', 'USERNAME'

Estimating the Size of an Index

If you’re creating a new index in SQL Server, it is good practice to make sure you have enough space on your drive to do so. You’ll not only need the space to hold it, but double the space as SQL Server builds it on temporary disk space. ( So if your index is going to be 1 GB, then you will need up to 2 GB to make it. By the way, when rebuilding, you’ll need triple the space, as SQL Server also retains the old index while it creates a new one.

Finding Duplicate Records In Database

If there is more than 1 column with that value it will show up like:
Value                             3
Value2                            2
Value99                           4

SQL Server – Find Stored Procedures User Can Access

Returns a report that has information about user permissions for an object, or statement permissions, in the current database.

To find stored procedures that “username” can access:

exec sp_helprotect @username = 'username'

To see all objects in database and who has access:

exec sp_helprotect

For complete capabilities see

SQL Server – Email Results of Stored Procedure Conditionally

Within a Stored Procedure, you may want the results of a particular query emailed to you, but only if there is a result. In other words, I don't want to be notified when the result set size is 0.

SQL Server Profiler New Templates Failing

Source of info:

Upon trying to create a trace template in SQL Server Profiler, I was able to name the trace but there were no events to choose from in the “Events Selection” tab, even when the “Show All Events” box was checked.  If I clicked the “Column Filters” button the entire Profiler application would close itself without displaying any error message… just… gone.

TSQL – Simple Loop Through Result Set Using Cursor

This is the basic query text to use whenever you need to loop through a result set in SQL Server using TSQL. In this example we are getting every database name and printing it out.

TSQL – Loop Through Result Set WITHOUT Cursors

There is a time and a place for everything, cursors included. Here is one way to avoid using them.

To NOT use cursors, you must create a temp table with a "flag" column to indicate whether or not it has been processed yet. Then, rather than updating the cursor, you update the flag column. Here's an example:


This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
1 + 1 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.

Theme by Danetsoft and Danang Probo Sayekti inspired by Maksimer