Solved

Problem 0f @@SPID

Posted on 2001-06-12
4
382 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]
Comment Utility
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
Comment Utility
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
Comment Utility
<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
Comment Utility
i stop doing that already......thanx for ur comment.
0

Featured Post

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

Suggested Solutions

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now