BelgoCanadian
BelgoCanadian

Reputation: 934

Find out who is executing the stored procedure from within said procedure

Am I able to find out who is executing the stored procedure from within that procedure?

CREATE PROCEDURE
AS 
BEGIN
    DECLARE @executor 
    SET @executor = {query to find out}
END

Upvotes: 2

Views: 3021

Answers (1)

Hadi
Hadi

Reputation: 37313

To achieve this you can use functions like

  • SUSER_NAME() Function (it returns the login identification name of the user) MSDN article
  • CURRENT_USER() (it returns the name of the current user) MSDN article
  • USER_NAME() (it returns a database user name from a specified identification number) MSDN article
  • ORIGINAL_LOGIN() (it Returns the name of the login that connected to the instance of SQL Server.) MSDN article

Example:

CREATE PROCEDURE
AS 
BEGIN
    DECLARE @executor 
    SELECT @executor = SUSERNAME()
    ...
END

You can read more in this very useful articles:

Upvotes: 3

Related Questions