ProgramName Details

Last post 06-13-2008, 3:17 AM by Phil Factor. 1 replies.
Sort Posts: Previous Next
  •  06-11-2008, 1:27 PM Post number 59489

    ProgramName Details

    SP_WHO2 and several other tools will tell you the ProgramName of a spid.  However the result is very vague.  For example, if an Office application has opened the connection then the ProgramName simply says "Microsoft Office 2003".  What I am after is the actual file name that opened the connection.  We have a large number of Access applications and I need to know which one is opening which connections.  In reality I would like to know the name of the query that is reading the database, but that is probably not possible.

  •  06-13-2008, 3:17 AM Post number 59604 in reply to post number 59489

    Re: ProgramName Details


    --You can easily see the last statement sent from a client to an instance
    --of Microsoft SQL Server by looking at DBCC inputbuffer, but I guess that
    --it is the actual Access query that you want to know about.

    --However, this might help to see what is going on in a database

    CREATE PROCEDURE spMySpids
    @Database SYSNAME --the name of the database
    AS
    --spMySpids 'NorthWind'
    DECLARE @DynamicSQL VARCHAR(MAX)

    SELECT @DynamicSQL='Declare @MySpids table
    (
    MySpid_ID int identity(1,1),
    spid int,
    EventType nvarchar(30),--Event type. This could be RPC
    --Event or Language Event.
    --The output will be No Event when no
    --last event was detected.
    Parameters int,  --0 = Text 1- n = Parameters
    EventInfo nvarchar(4000) --For an EventType of RPC,
    --EventInfo contains only the procedure name.
    --For an EventType of Language, only the first 4000 characters
    --of the event are displayed.
    )
    '

    SELECT @DynamicSQL=@DynamicSQL+'
    insert into @MySpids (EventType, Parameters, EventInfo)
    execute (''dbcc inputbuffer ('
    +CONVERT(VARCHAR(5),spid)+')'')
    update @MySpids set spid='
        
    +CONVERT(VARCHAR(5),spid)
         +
    ' where MySpid_ID=@@identity
    '
    FROM MASTER.dbo.sysprocesses WHERE dbid=DB_ID (@database)

    SELECT @DynamicSQL=@DynamicSQL+'
    Select * from @MySpids'

    EXECUTE (@DynamicSQL)

View as RSS news feed in XML