Skip to main content

SQL Server 2000, sp_who2 and fn_get_sql

A bit of old school SQL Server 2000 stuff that I had to delve in to, to resuscitate a dying database using sp_who2.

Running the command sp_who2 gives details of the current sessions, users and processes running in an instance of SQL Server. It's an undocumented version of the sp_who command (http://msdn.microsoft.com/en-us/library/ms174313.aspx) but sp_who2 gives a bit more info as to what certain processes are doing. From this, you can get a feel for what sessions and processes are hanging the database, or are being blocked, or just generally causing problems in the current running sessions of the instance of SQL Server.

Once the SPID (Server Process ID) is known that is causing issues, this can be used to get the sql_handle from sysprocesses. The sql_handle is a binary key that references queries in the procedure cache.

After getting the sql_handle, the function fn_get_sql can be used to reveal the SQL statement that seems to be causing issues.

DECLARE @Handle varbinary(255);
SELECT

@Handle = sql_handle 
FROM
sysprocesses 
WHERE
spid = SPID-from-sp_who2-to-go-here;
SELECT * FROM ::fn_get_sql(@Handle);

GO


For later versions of SQL Server (i.e. SQL Server 2005 and SQL Server 2008), use sys.dm_exec_requests and sys.dm_exec_sql_text to figure things out.

Comments

Popular posts from this blog

EF CodeFirst Database.SetInitializser Requires Connection to 'master' Database Error

One of the features of EF CodeFirst is the ability to automatically drop and recreate a database if the model changes or if the database does not exist, which is pretty useful when just doing development work. In a web app this is done in the Global.asax file within "protected void Application_Start()" as per this post from Scott Guthrie -  http://weblogs.asp.net/scottgu/archive/2010/07/16/code-first-development-with-entity-framework-4.aspx . An example would be: protected void Application_Start() { Database.SetInitializer<yourdbcontex>(new DropCreateDatabaseIfModelChanges<yourdbcontext>()); AreaRegistration.RegisterAllAreas(); RegisterGlobalFilters(GlobalFilters.Filters); RegisterRoutes(RouteTable.Routes); } However when using the Database.SetInitializer and trying to rebuild the database, the following exception may be encountered, especially if using SQL Server Express: "This operation requires a connection to the 'master'...

Enable .NET 8 Preview in Visual Studio

Download the SDK using Download .NET 8.0 (Linux, macOS, and Windows) (microsoft.com)  and install it. To enable projects to target the .NET 8 preview framework, the preview option in Visual Studio needs to be enabled, otherwise the option to target .NET 8 will not be available as shown below when setting up a new project (or trying to upgrade an existing one). To allow .NET 8 Preview to be used as a target framework for projects, the preview option needs to be enabled in Visual Studio. Open Visual Studio and select "Continue without code" In Visual Studio, select Tools then Options In Options, under Environment, select Preview Features and enable Use previews of the .NET SDK.

VWD Express 2010 Unit Tests with xUnit and MVC Templates

Microsoft's Visual Web Developer 2010 Express (VWD 2010 Express), by default does not come with a built in unit testing solution. However the ability is there to use unit tests with the Express version as can be seen when creating a new MVC project. Tech spec stuff: Microsoft Windows 7 Microsoft Visual Studio 2010 Express, Version 10.0.40219.1 SP1 Microsfot .NET Framework Version 4.0.30319 SP1 Getting started with xUnit xUnit is a "developer testing framework, built to support Test Driven Development (TDD)". There are other unit testing frameworks available (MSTest, nUnit, etc.). There are pro's to each and some better than others but xUnit is pretty straightforward to get up and running with the VWD 2010 Express version. Download and Install xUnit The xUnit project can be found at:  http://xunit.codeplex.com/ . Downloads are available from  http://xunit.codeplex.com/releases/view/62840 . Download the recommended version (currently 1.8 at time of writi...