Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2015 November SQL Server: Script to find the last Executed Query using SPID

SQL Server: Script to find the last Executed Query using SPID

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

In this post, I am sharing different methods to find last executed query by SPID of SQL Server.

When DBA monitoring the server performance or serving any request on the performance issue, the DBA may be required to find last executed query of a particular client.

You can find SPID using below commands:

1
2
SELECT @@SPID --returns current sessions ID
SP_WHO --list of all sessions with SPID

Find last executed query using DBCC INPUTBUFFER:
You can use DBCC INPUTBUFFER to get the last SQL statement issued by a client by passing the SPID as parameter.

1
DBCC INPUTBUFFER (SPID)

Find last executed query using SYS.SYSPROCESSES and SYS.DM_EXEC_SQL_TEXT:

1
2
3
DECLARE @sqlhandle VARBINARY(64)
SELECT @sqlhandle = sql_handle from sys.sysprocesses where spid = 987
SELECT text FROM sys.dm_exec_sql_text(@sqlhandle)
Nov 19, 2015Anvesh Patel
SQL Server: Script to find the size of Database Files and Log Filesdbrnd.com published 125 Posts: This Gratitude Post for Readers
Comments: 3
  1. Adrian
    December 15, 2015 at 1:48 pm

    Please let me now if you’re looking for a writer for your blog.
    You have some really good articles and I feel I wold
    be a good asset. If you ever want to take some of the
    load off, I’d absolutely love to write some material for yoiur blog in exchange for a link bacck tto
    mine. Please send me an emaill if interested. Regards!

    • Anvesh Patel
      Anvesh Patel
      December 16, 2015 at 10:51 am

      Thanks for attention but at this time, I am not looking for other writer.

  2. imlay
    January 11, 2016 at 1:22 pm

    My brother suggested I might like this web site.
    He used to be totally right. This put up truly made my day.
    You cann’t imagine simply how much time I had spent for this
    blog ! Thanks!

Anvesh Patel
Anvesh Patel

Database Engineer

November 19, 2015 SQL Server, SQL Server DBA ScriptAnvesh Patel, database, database research and development, dbrnd, INPUTBUFFER, SQL Query, SQL Server, SQL Server Administrator, SQL Server Monitoring, SQL Server Performance Tunning, SQL Server Tips and Tricks, TSQL
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....