Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Problem 0f @@SPID

Posted on 2001-06-12
4
Medium Priority
?
396 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

670 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