Wednesday, January 15, 2014

Helpful scripts for SQL Server

There is nothing new or revolutionary about the following scripts but I am compiling them here for anyone that these may help.

SQL Server database marked 'suspect'

There are various reasons that the DBMS may mark your database as suspect . Some of these are

  • The database could have been corrupted
  • Not enough space for the DBMS to run a recovery operation.
  • OS locks on the files
  • Unexpected sql server shut down.

To resolve this copy and paste the following script into SSMS. Replace [MyDatabaseName] to the actual database that you are trying to recover.

use master
go
EXEC sp_resetstatus [MyDatabaseName];
ALTER DATABASE [MyDatabaseName] SET EMERGENCY
DBCC checkdb(MyDatabaseName)
ALTER DATABASE MyDatabaseName SET SINGLE_USER WITH ROLLBACK IMMEDIATE
DBCC CheckDB (MyDatabaseName, REPAIR_ALLOW_DATA_LOSS)
ALTER DATABASE MyDatabaseName SET MULTI_USER

Get a list of all indexes on all tables in your database

There are instances when you need a list of all the tables, with index description and keys in your database. The following script lets you do just that.

DECLARE @IndexInfoTemp  TABLE (index_name         varchar(250)
                              ,index_description  varchar(250)
                              ,index_keys         varchar(250)
                              )

DECLARE @IndexInfo  TABLE (table_name         sysname
                          ,index_name         varchar(250)
                          ,index_description  varchar(250)
                          ,index_keys         varchar(250)
                          )

DECLARE @Tables Table (RowID       int not null identity(1,1)
                      ,TableName   sysname 
                      )
DECLARE @MaxRow       int
DECLARE @CurrentRow   int
DECLARE @CurrentTable sysname

INSERT INTO @Tables
    SELECT
        DISTINCT t.name
        FROM sys.indexes i
            INNER JOIN sys.tables t ON i.object_id = t.object_id
        WHERE i.Name IS NOT NULL
SELECT @MaxRow=@@ROWCOUNT,@CurrentRow=1

WHILE @CurrentRow<=@MaxRow
BEGIN

    SELECT @CurrentTable=TableName FROM @Tables WHERE RowID=@CurrentRow

    INSERT INTO @IndexInfoTemp
    exec sp_helpindex @CurrentTable

    INSERT INTO @IndexInfo
            (table_name   , index_name , index_description , index_keys)
        SELECT
            @CurrentTable , index_name , index_description , index_keys
        FROM @IndexInfoTemp

    DELETE FROM @IndexInfoTemp

    SET @CurrentRow=@CurrentRow+1

END --WHILE
SELECT * from @IndexInfo

Kill all sleeping processes in SQL Server

There may be occasions where you may want to get a list and subsequently kill all sleeping processes. Here is a script that lets you do that.

DECLARE @v_spid INT
DECLARE c_Users CURSOR
   FAST_FORWARD FOR
   SELECT SPID
   FROM master..sysprocesses (NOLOCK)
   WHERE spid>50 
   AND status='sleeping
   AND DATEDIFF(mi,last_batch,GETDATE())>=60
   AND spid<>@@spid

OPEN c_Users
FETCH NEXT FROM c_Users INTO @v_spid
WHILE (@@FETCH_STATUS=0)
BEGIN
  PRINT 'Exterminating '+CONVERT(VARCHAR,@v_spid)+'...'
  EXEC('KILL '+@v_spid)
  FETCH NEXT FROM c_Users INTO @v_spid
END

CLOSE c_Users
DEALLOCATE c_Users

Monday, October 7, 2013

VS 2012 - Some of the properties associated with the solution could not be read

Recently, I upgraded one of the team projects to VS2012. Post upgrade, whenever I tried opening the solution, VS would complain with the message, "Some of the properties associated with the solution could not be read".

I opened up the solution file that did not have this issue (the one in the dev branch) and compared it to the recently upgraded solution. When I compared the solution files the value for the "SccNumberOfProjects" was different. There were supposed to be only 20 projects part of the solution but for some odd reason the value for this attribute was 21 in the upgraded version.

I changed the value of this attribute to 20 in the upgraded solution file (had to changes this in a couple of spots in the solution file) and the problem went away.

Hope this helps.

Friday, April 26, 2013

Unexpected Error "12037"

When downloading something from a secure site you may sometimes get the following error

Error 12037 - SSL certificate date that was received from the server is bad. The certificate is expired.

If this is the case, then check your clock settings. Most often then not this is because of a wrong time/zone on your machine.

Hope this helps.

Monday, December 17, 2012

Http 500 Internal server error

Scenario: I deployed an application as a virtual directory under the default site and my application threw a HTTP 500 error.

Resolution: The reason was that the default site and my virtual directory that was configured as an application had common keys (keys with the same name). I removed the key from the application in the virtual directory and the problem went away. One way to find any config issues in IIS7 is to open the configuration editor in IIS7 for the site you are troubleshooting. The configuration editor will throw specific exceptions if it cannot successfully open the config which is how I found the issue with duplicate keys.

Tuesday, September 27, 2011

Error -2147024893 while uninstalling ss2k5 reporting services

While recently trying to uninstall reporting services 2k5 from my machine I ran into this nasty error. Researching this I found that the default site was running. Stopped the default site, re-ran the uninstallation and everything is hunky dory again.

Tuesday, May 24, 2011

Enable Session State in a WCF service

The concept of session in a WCF service is widely different from a typical ASP.NET application where the session is initiated and maintained on the server. Sessions in WCF are per call and are created in the context of the channel that received the service request. I will keep this simple. If you need access to Session data in a WCF service there are 2 things that need to happen. In the web.config of your service application, the aspnetcompatibility needs to be enabled like so....
1.
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibiltyEnabled="true"/>

2. Add the following line to the class that implements your service interface like so...

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

The AspNetCompatibilityRequirementsMode enum has three possible values, Allowed NotAllowed and Required. Set the value of this enum according to your needs.

System.Web.HttpContext unavailable in a service class library

I encountered this recently. I have a service that implements an aggregate interface. Each interface is implemented in a seperate class library. I needed access to System.Web.HttpContext in one of my projects. When I tried to add a reference to the System.Web namespace I did not see this namespace listed in the list of available namespaces. Intrigued I checked my project properties and saw that the project was referencing .Net Client Profile of .Netfrx40. I changed this to the full .Netfrx40 and tried adding the reference again and VOILA I was able to add the namespace reference to my project and now have access to HttpContext.

Happy Coding!