New answers tagged scripting
As you were hinted in comments above, your RBAR approach might be very inefficient. Consider the suggestions there. Also, I am not going into details about the different approaches of UPSERT, as it is a very broad topic, especially when one wants to do concurrency-safe. PostgreSQL 9.5 helps a lot in this regard. So, to your actual question: it is not ...
Logins (on the server) do not automatically correspond to users (in the database). For instance, "sa" does not have its own user in the database, but uses "dbo". This might get you started. SELECT s.session_id, sp.[type], sp.type_desc, sp.[name] AS loginName, dp.[name] AS userName, sp.is_disabled FROM sys.dm_exec_sessions AS s LEFT JOIN ...
All you need to do is JOIN to sysusers SELECT p.spid, p.dbid, p.uid, u.name FROM sys.sysprocesses p JOIN sys.sysusers u ON p.uid = u.uid
Top 50 recent answers are included