File Sizes for all Databases

This is re-runnable script to get the database file sizes for each database in an instance. This is quick and dirty implementation which you may want to adapt for your own needs.

IF EXISTS(SELECT NULL FROM msdb..sysobjects where name = 'DBFileSizes' AND type = 'U')
	TRUNCATE TABLE dbo.DBFileSizes
ELSE
CREATE TABLE dbo.DBFileSizes(
	[DBname] nvarchar(128) NULL,
	[fileid] [smallint] NULL,
	[groupid] [smallint] NULL,
	[size] [int] NOT NULL,
	[maxsize] [int] NOT NULL,
	[growth] [int] NOT NULL,
	[status] [int] NULL,
	[perf] [int] NULL,
	[name] [sysname] NOT NULL,

Distinct List of Servers in Central Management Server

Here's a way to get a distinct list of the server's registered in your CMS (Central Management Server).

--dbo.sysmanagement_shared_registered_servers_internal
--unique list
SELECT DISTINCT s.[server_name] as [server]
FROM	[msdb].[dbo].[sysmanagement_shared_server_groups_internal] g
JOIN	[msdb].[dbo].[sysmanagement_shared_registered_servers_internal] s
	ON	g.[server_group_id] = s.[server_group_id]
WHERE	g.[NAME] LIKE '%'

Detect and Correct Orphaned Users

To detect orphaned users in a given database, simply run the following.

USE [MyDB]
GO
EXEC sp_change_users_login 'report'

If you want to check all databases on a given SQL Server at the same time, just wrap it in sp_msforeachdb.

EXEC sp_msforeachdb "exec [?]..sp_change_users_login 'report'"

Finally to resolve an orphaned user, run the following script to resync the SID of the user to map to the appropriate login. Note that a user does not need to map to a login of the same name just as the login 'sa' maps to the user 'dbo'.

USE [MyDB]
GO

Flipping a Bit Field

Here are several techniques for changing a bit field to its opposite value (flipping the bit). The simple SELECT demonstrates a BIT field and its opposite. You can adjust this easily to switch to an UPDATE statement.

DECLARE @BitTable TABLE ( BitField bit NOT NULL )
INSERT INTO @BitTable VALUES (0), (1)
SELECT	BitField,
	BitFlipped = ~BitField
	--BitFlipped = BitField ^ 1
	--BitFlipped = 1 - BitField
	--BitFlipped = CASE BitField WHEN 0 THEN 1 ELSE 0 END
	--BitFlipped = ABS(Bitfield - 1)
	--BitFlipped = (BitField + 1) % 2
FROM @BitTable
GO

Use Read Only Mode to Get a Final clean Database Backup

In order to get a final database backup, you need for the users to not be doing any more transactions against the database. The best way to guarantee this is to place the database in READ ONLY mode. But if there are existing connections, you probably won't be able to do this. First set the database to SINGLE USER mode and then change it right back to MULTI USER. This will drop the existing connections to the database.

USE [master]
GO
--kill existing connections and set to single user
ALTER DATABASE [MyDatabase] SET  SINGLE_USER WITH ROLLBACK IMMEDIATE
GO

Use an Email Friendly Name Alias for Scheduled SSRS Reports

If you want to display a Friendly Name for the sender of your SQL Server Reporting Services reports, use the following syntax.
First go to
Start --> All Programs --> Microsoft SQL Server 2008 --> Configuration Tools --> Reporting Services Configuration Manager
Once you've connected to the Report instance, select
Email Setting --> Sender Address and Enter
"FriendlyNameAlias" emailaddress@yourdomain.com

Quick Rowcount for all Tables

As long as you keep your statistics updated, you can use this technique to calculate a quick rowcount of all tables.

SELECT	o.name AS [TableName],
	i.rows AS [RowCount]
FROM	sysobjects o
JOIN	sysindexes i	ON o.id = i.id
WHERE	xtype='u'
	AND	indid < 2 --heap or clustered
ORDER BY [RowCount] DESC

Ensure Fields are Defined Identically across all Tables

When you have fields that are common across multiple tables, you probably want them to be defined identically. Are they? Are you sure? Use this script to check things like exact case spelling, NULLability and field length/precision.

WITH UniqueColumnDefinitions AS (
SELECT
	o.[name] AS [Table], 
	c.[name] AS [Column],
	t.[name] AS [Data Type],
	c.[prec] AS [Precision],
	c.[scale] AS [Scale], 
	c.[isnullable] AS [Nullable],
	[check_sum] = binary_checksum(c.[name], t.[name], c.[prec], c.[scale], c.[isnullable])
FROM	[dbo].[sysobjects] o 

Disable or Enable all Triggers on a Table

If you need to bring a copy of the database from production to another environment, you may want to disable all the triggers for all tables. Here is a simple syntax you can use to accomplish this.

First you could specify the specific table(s) for which you want to disable triggers.

ALTER TABLE [dbo].[MyTable] DISABLE TRIGGER ALL

The re-enable all triggers just use this syntax.
ALTER TABLE [dbo].[MyTable] ENABLE TRIGGER ALL

List all Disabled Triggers

If you have triggers running against your database tables then you probably want to make sure they are turned on or off at the right times. Here's a script to check the status of your triggers.

SELECT	db_name() AS [Database Name],
	T.[name] AS [TableName],
	TR.[Name] AS [TriggerName],
	[Status] = CASE WHEN OBJECTPROPERTY(TR.[id], 'ExecIsTriggerDisabled') = 1
		THEN 'Disabled' ELSE 'Enabled' END
FROM	sysobjects T
JOIN	sysobjects TR ON t.[ID] = TR.[parent_obj]
WHERE	T.[xtype] = 'U'
	AND TR.[xtype] = 'TR'

Copyright © 2010-2012 SQL Tidbits™