[Show all top banners]

the_hareeb
Replies to this thread:

More by the_hareeb
What people are reading
Subscribers
:: Subscribe
Back to: Kurakani General Refresh page to view new replies
 how to find SPID of client program in SQL-Server
[VIEWED 3017 TIMES]
SAVE! for ease of future access.
Posted on 09-29-10 4:07 PM     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

I need to track some client program's query for index recommendations from Database Tuning advisor. For that I am using SQL server Profiler and exporting the table to Database Tuning Advisor. 

How do I get the SPID of my client program so that I can filter that client program in profiler.

thanks for help.



 
Posted on 09-29-10 5:16 PM     [Snapshot: 12]     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

You might want to run the query below in your client's db
SELECT @@SPID AS 'ID', SYSTEM_USER AS 'Login Name', USER AS 'User Name'

Hope that is what you are looking for...

 
Posted on 09-29-10 5:22 PM     [Snapshot: 18]     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

AlwaysHappy,
Your query is not correct. @@SPID gives you the current SPID of the user. So You will give only one spid always.
Here is the complete query. 

IF
 EXISTS (SELECT * 
           FROM   tempdb.sys.objects 
           WHERE  object_id = Object_id('Tempdb.dbo.#cLIENTsPID')) 
  DROP TABLE #clientspid 

CREATE TABLE #clientspid ( 
  spid        VARCHAR(10), 
  [status]    VARCHAR(20), 
  [login]     VARCHAR(100), 
  [hostname]  VARCHAR(100), 
  blkby       VARCHAR(10), 
  dbname      VARCHAR(100), 
  command     VARCHAR(100), 
  cputime     INT, 
  diskio      INT, 
  lastbatch   VARCHAR(100), 
  programname VARCHAR(100), 
  spid1       INT, 
  requestid   INT) 

INSERT INTO #clientspid 
EXEC Sp_who2 

SELECT spid, 
       hostname, 
       ProgramName
FROM   #clientspid 
WHERE  hostname NOT IN ('  .',@@SERVERNAME)
Last edited: 29-Sep-10 05:26 PM

 
Posted on 09-29-10 11:16 PM     [Snapshot: 68]     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

thanks guys.. will try this tomorrow.

virus,a quesitron for you..
IF EXISTS (SELECT * 
           FROM   tempdb.sys.objects 
           WHERE  object_id = Object_id('Tempdb.dbo.#cLIENTsPID')) 
  DROP TABLE #clientspid 

what does this do? Since we have '#', this means the table #cLIENTsPID will be dropped automatically once the session is over right? so why do we need to drop it. also what does this if exists section do.

if i understand it correctly, your script just creates a table from sp_who2. Please help me understand. Your help is appreciated. Sorry I am new to DBA.

thanks

 
Posted on 09-30-10 10:39 AM     [Snapshot: 96]     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

hareeb, If exists with condition makes sure that temp table is not created already. This is the best practice while creating any temp table. and Yes temp table is a session label variable. It will be destroyed when your session close. I dropped the table table because what if you need to run that command again and again (like with 10 minute interval). Write a extra few extra lines for code doesn't harm at all :). Well, you will soon understand the importance of if Exist when you write a big queries for production dbs and best of luck for that
 
Posted on 09-30-10 1:27 PM     [Snapshot: 108]     Reply [Subscribe]
Login in to Rate this Post:     0       ?    
 

thanks for the clarification virus. I am new to this, do you suggest any books, tutorials? I am currently looking at index tuning, fill factors etc.  
 


Please Log in! to be able to reply! If you don't have a login, please register here.

YOU CAN ALSO



IN ORDER TO POST!




Within last 7 days
Recommended Popular Threads Controvertial Threads
TPS Re-registration case still pending ..
जाडो, बा र म……
NOTE: The opinions here represent the opinions of the individual posters, and not of Sajha.com. It is not possible for sajha.com to monitor all the postings, since sajha.com merely seeks to provide a cyber location for discussing ideas and concerns related to Nepal and the Nepalis. Please send an email to admin@sajha.com using a valid email address if you want any posting to be considered for deletion. Your request will be handled on a one to one basis. Sajha.com is a service please don't abuse it. - Thanks.

Sajha.com Privacy Policy

Like us in Facebook!

↑ Back to Top
free counters