Solved

Problem 0f @@SPID

Posted on 2001-06-12
4
386 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 142

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 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 100 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

773 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