Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6560129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:25:12+00:00 2026-05-25T13:25:12+00:00

I am trying to pull the input buffer data ( DBCC INPUTBUFFER(@SPID) ) for

  • 0

I am trying to pull the input buffer data (DBCC INPUTBUFFER(@SPID)) for each record returned for a database when querying the SYSPROCESSES table. I am interested to hear if there is a better way to accomplish this, but would also appreciate correction to what I currently have working for learning purposes.

DECLARE @Max [int]
DECLARE @Min [int] = 1

SELECT @Max = COUNT(SPID)
FROM MASTER.DBO.SYSPROCESSES
WHERE DB_NAME(DBID) = 'Northwinds' AND DBID != 0

CREATE TABLE #Results (
    EventType [nvarchar](1024),
    Parameters [int],
    EventInfo [nvarchar](1024),
    SPID [int],
    STATUS [nvarchar](255),
    PROGRAM_NAME [nvarchar](1024),
    CMD [nvarchar](255),
    LOGINAME [nvarchar](255)
)

WHILE @Min <= @Max
BEGIN

    DECLARE @SPID [int]

    WITH SelectedRow AS (
        SELECT SPID, ROW_NUMBER() OVER (ORDER BY SPID) AS RowNumber
        FROM MASTER.DBO.SYSPROCESSES
        WHERE DB_NAME(DBID) = 'Northwinds' AND DBID != 0
    )

    SELECT @SPID = SPID
    FROM SelectedRow
    WHERE RowNumber = @Min

    DECLARE @InputBuffer TABLE (
        EventType [nvarchar](1024),
        Parameter [int],
        EventInfo [nvarchar](1024)
    )

    DECLARE @SysProcesses TABLE (
        SPID [int],
        STATUS [nvarchar](255),
        PROGRAM_NAME [nvarchar](1024),
        CMD [nvarchar](255),
        LOGINAME [nvarchar](255)
    )

    INSERT @InputBuffer 
        EXEC('DBCC INPUTBUFFER('+@SPID+')')

    INSERT @SysProcesses 
        SELECT SPID, STATUS, PROGRAM_NAME, CMD, LOGINAME
        FROM MASTER.DBO.SYSPROCESSES
        WHERE DB_NAME(DBID) = 'Northwinds' AND DBID != 0 AND SPID = @SPID

    INSERT INTO #TempResults(EventType, Parameters, EventInfo, SPID, STATUS, PROGRAM_NAME, CMD, LOGINAME)
        SELECT *
        FROM @InputBuffer, @SysProcesses

    SET @Min = (@Min + 1)
END

Now when I execute the following query:

SELECT SPID
FROM MASTER.DBO.SYSPROCESSES
WHERE DB_NAME(DBID) = 'Northwinds' AND DBID != 0

it returns 31 rows…

However when I SELECT * FROM #TempResults after executing the above loop I am returning mass amounts of duplicates in the temp table…to the sum of 10751.

Again this is mostly for learning, but would be a pretty handy function to have as well. I am looking for information on what I’m missing causing the duplicate records to be returned, as well as a possible better solution.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T13:25:13+00:00Added an answer on May 25, 2026 at 1:25 pm

    You are doing this:

    INSERT #Results
    SELECT * 
    FROM @InputBuffer, @SysProcesses
    

    This is effectively a cross join, so if either table has more than one row, you’re going to have COUNT(left side) * COUNT(right side) rows as a result. Maybe you should use SELECT DISTINCT column list instead or, as I suggested in my comment, stop looping through sysprocesses (deprecated) and retrieving info from DBCC INPUTBUFFER – even for educational purposes that is painful.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am trying to pull data from one table, compare columns to a variable,
Trying to pull fields 'info' and 'date' from Comment table. Two Tables: Php <?php
I am trying to pull up some data. Here is the setup: A [school]
I've been trying to pull a copy of my sqlite database from my android
With the code below I am trying to pull each url I extract using
I've generated some JSON and I'm trying to pull it into an object in
I'm trying to make a pull down menu post a form when the user
I'm trying to use SharpZipLib to pull specified files from a zip archive. All
I'm trying to write a query that will pull back the two most recent
I am trying to write a function that will pull the name of a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.