Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Problem 0f @@SPID

Posted on 2001-06-12
4
Medium Priority
?
399 Views
Last Modified: 2012-06-27

I understand that this @@SPID will return the server process ID of the current user process. So, I use this spid to select the username from the NT in ?master..sysprocesses?  at column ?nt_username?. The below sp is my code;

CREATE PROCEDURE proc_ NTUserName
AS
BEGIN
DECLARE @vchEmployee varchar(12)
     DECLARE @NTName nchar(128)
     DECLARE @SlashPosition INT

     SELECT @NTName = nt_username -- Fetch NT user name for this process ID
     FROM master..sysprocesses
     WHERE spid = @@SPID

SET @SlashPosition = CHARINDEX('\', @NTName) -- Trim the NT domain from front of user name if it is present

     IF (@SlashPosition <> 0)
     BEGIN
          SET @NTName = RIGHT(@NTName, 128 - @SlashPosition)
     END

SET @vchEmployee = RTRIM(LEFT(CAST(@NTName AS varchar(256)), 12)) -- Convert to data type used in audit table
     
     SELECT vchEmployee
END


When I test this sp in SQL Server Query Analyzer it is works fine but when I use recordset in Access to retrieve the username it is return nothing (empty). And my code in access is as below

Dim strsql As String
Dim rs As Recordset

    strsql = " proc_NTUserName  "
    Set rs = clsDatabase.OpenRecordSetODBC(strsql)
    If Not rs.EOF Then
        MsgBox rs(0)
    End If
    Set rs = Nothing

So, why from access it cannot return value????

     
0
Comment
Question by:jetyun
  • 2
4 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 6180761
If the user has used the Integrated Security, then simply issue this:

select system_user
or
select nt_domain, nt_user from sysprocesses where spid = @@spid

Now in your precise case, you might modify your code like this:
CREATE PROCEDURE proc_ NTUserName
AS
SET NOCOUNT ON
BEGIN
 ...
END

The problem is your first SELECT @Variable, which will generate an empty recordset, which will not be generated with the SET NOCOUNT ON option.

Cheers
0
 

Expert Comment

by:Kelvsat
ID: 6185964
dear angelIII,
  I tried this already it is still not work....
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 300 total points
ID: 6186861
<jetyun> or <Kelvsat>

as i mentionned: If the user has used the Integrated Security ...
If you use UserID= and Password= parameters in your connection string, the value(s) nt_user and nt_domain ARE empty!
Your connection string needs to have "Trusted_Connection=true" or "Integrated Security=SSPI" in it in order that these values are filled.

Please post your connection string so we can compare.

CHeers
0
 

Author Comment

by:jetyun
ID: 6371470
i stop doing that already......thanx for ur comment.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

877 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question