Showing posts with label mssql 2008. Show all posts
Showing posts with label mssql 2008. Show all posts

Thursday, January 3, 2013

Your Daily MSSQL Tips - Display Table Size and Disable Maintenance Plans for MSSQL 2005, MSSQL 2008 and MSSQL 2012

Yeeesh posts have been MSSQL-heavy. I've just been working on this stuff for quite a bit lately - by no means is this my favorite database. Anyway, you're not here for that. You're here because you need to fix something that's broken and Google says I have it. Here you go:

Find Database Table Size

Your database is growing and you need to know why. First step would be to find out which table is responsible for the growth - but how? Right click the database in question and run one of the following queries:

Displays the size of every table - exec sp_Msforeachtable 'exec sp_spaceused ''?'''
Displays a specific table - exec sp_spaceused TableName

Disable a Maintenance Plan

Let's say you want to temporarily disable a maintenance plan. You are trying to troubleshoot it, but you don't want to delete the entire thing and re-create it. You may have noticed SQL Management Studio's GUI has not straight forward way of doing this. First, you need to find the Job ID for the maintenance plan. This can be retrieved from the SQL Server Agent Job Properties window. Alternatively you can run the following query:

name ASC

Select the correct job ID and add it to this query,replacing @PutYourJobIDHere with the ID of your plan : 

SET @my_job_id = 'PutYourJobIDHere'
EXEC msdb.dbo.sp_update_job @job_id = @my_job_id , @enabled = 0

So a correct query would look like this:

SET @my_job_id = '8AE8F423-C40F-453B-9B7D-9F632FBAC2FC'
EXEC msdb.dbo.sp_update_job @job_id = @my_job_id , @enabled = 0

When you are ready to enable your plan again, use the same query, changing @enabled = 0 to @enabled = 1

Saturday, December 29, 2012

Remove Single User Mode From Your MSSQL Database

***This guide is valid for MSSQL 2000, MSSQL 2005, MSSQL 2008 and MSSQL 2012. 

Is your database stuck in single user mode? Irritating isn't it? Use the following command to restore it to normal service, where DBNAME is the name of the database:

exec sp_dboption 'DBNAME', 'single user', 'FALSE'

MSSQL .NET sysobjects Cheatsheet - Viewing and Creating Sysobjects

The following in formation should be valid for MSSQL 2005 and 2008. Sysobject creation is best handled using the GUI available at $WINDOWS\Microsoft.NET\Framework\$version_number\aspnet_regsql.exe. Under some circumstances, this may not be possible or preferable. In those circumstances, the following queries can assist you.

To view all installed sysobjects for the database, run the following query, replacing DBNAME with the name of the relevant database:

select * from sysobjects where xtype ='P' and name like 'aspnet_%'

Sysobject creation scripts are specific to the object. The script below will create aspnet_SchemaVersions:

CREATE PROCEDURE [dbo].aspnet_CheckSchemaVersion   
@Feature                   nvarchar(128),   
@CompatibleSchemaVersion   nvarchar(128)
BEGIN IF (EXISTS( SELECT  *               
FROM dbo.aspnet_SchemaVersions               
WHERE   Feature = LOWER( @Feature ) AND                       
CompatibleSchemaVersion = @CompatibleSchemaVersion ))       

Thursday, December 13, 2012

Simple T-SQL Script to Kill All Connections to a MSSQL Database

When restoring an MSSQL database, it is common to receive an error similar to this one: 

"The database could not be exclusively locked to perform the operation."
Msg 5030, Level 16, State 12, Line 1 

This error can occur in MSSQL 2005, 2008 or 2012.
There are a number of workarounds for this. Some will tell you to disable the website's application pool in IIS, but this doesnt help if someone remotely is connected using SQL Management Studio. You can go into Performance Monitor and manually kill each process, but often the processes jump up faster than you can disable them by hand. 
Where that is the case, perform the following procedure. Assuming your restore job task window is properly configured and just open, keep it open. You can right click on the database server and select New Query without closing the restore job task window, which wastes time when you must recreate it. Then, enter the following script into the query menu (be sure to update the variable @DB_NAME with the actual name of your database). This script will sever all connections immediately. You can then left click on your restore job task window and click OK to retry the restore. Assuming the restore task is correctly configured, it will complete without further issue. 

USE master

DECLARE @DBName varchar(50)
DECLARE @spidstr varchar(8000)
DECLARE @ConnKilled smallint
SET @ConnKilled=0
SET @spidstr = ''

Set @DBName = '@DB_NAME'
IF db_id(@DBName) < 4
PRINT 'Connections to system databases cannot be killed'
SELECT @spidstr=coalesce(@spidstr,',' )+'kill '+convert(varchar, spid)+ '; '
FROM master..sysprocesses WHERE dbid=db_id(@DBName)

IF LEN(@spidstr) > 0
SELECT @ConnKilled = COUNT(1)
FROM master..sysprocesses WHERE dbid=db_id(@DBName)

(hat tip to Adam Lacey)

How To Fix MSSQL Error 3219

You've done everything right with your MSSQL database restore. All of your versions match. All connections to the database have been killed. Your BAK file is valid, you have adequate permissions to both it and the proposed MDF and LDF file paths. And yet, when you attempt to restore using MSSQL 2005 or 2008, the task fails with the following:

The file or filegroup X cannot be selected for this operation. RESTORE DATABASE is terminating abnormally. (Microsoft SQL Server, Error: 3219)

Often the most infuriating problems are the one's with the easiest solution. To fix this, simply perform "Restore Database" instead of "Restore Files".

NSA Leak Bust Points to State Surveillance Deal with Printing Firms

Earlier this week a young government contractor named Reality Winner was accused by police of leaking an internal NSA document to news outle...