問題描述
我的數據庫配置為使用登錄名 sa
的 SQL 服務器身份驗證.現在我想知道用戶的Windows 登錄用戶名是什么.SA 將在那里為每個人服務.我能夠獲得計算機IP地址和計算機名,但我迫切需要用戶的Windows登錄用戶名.我的網絡是使用活動目錄設置的.
My database is configured to use SQL server authentication with login name sa
. Now I would like to know what is the user's Windows login user name. SA will be there for everybody. I was able to get the computer IP address and Computer name, but I desperately need the user's Windows login user name. My network is setup using active directory btw.
set @UserComputerIP = CONVERT(varchar(20),CONNECTIONPROPERTY('client_net_address'))
set @UserComputerName = CONVERT(varchar(20),HOST_NAME())
任何幫助將不勝感激.(Ps. 不,我無法在 SQL Server 中切換到 Windows 身份驗證)
Any help would be appreciated. (Ps. No i cannot switch to Windows Authentication in SQL Server)
推薦答案
如果您不使用 Windows 身份驗證,我不確定您是否可以獲得域用戶名.
I'm not sure if you can obtain the domain user name if you're not using Windows Authentication.
我所知道的最詳細的信息是系統表/視圖,如果您使用 SQL 登錄名連接,這些信息不會顯示 NT 用戶名.即使服務器處于混合身份驗證模式.
The most detailed info that I know of are the system tables/views, and those don't show the NT user name if you connected using a SQL login. Even if the server is in mixed authentication mode.
在 SQL Server 2008 之前的版本上:
On pre-SQL Server 2008:
select nt_username from master.sys.sysprocesses where spid = @@spid
從 SQL Server 2008 開始:
From SQL Server 2008 on:
select nt_user_name from sys.dm_exec_sessions where session_id = @@spid
如果您使用 SQL 身份驗證連接,兩者都會顯示一個空列.
Both will show an empty column if you connected using SQL authentication.
這篇關于從sql server獲取windows用戶登錄名的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!