• KGNH - Monday, July 2, 2018 10:51 PM

    I have to write a procedure where I need to capture some data from application and insert into some tables. Here I also have to get the UserName who is accessing application and done the changes. I have written a procedure to capture the changes. But the user details should come from the application. Here in my table I am having a field UserName to capture the data.

    So While writing the procedure what should I give for Username.
    For inserting the UserName any thing that should be done from the database side or it will be handled only at the application level.

    Currently all the users accessing application are using windows credentials and connecting to database with the same.

    Thanks.

    For Application with shared SQL login

    CREATE PROCEDURE foo.bar
      @SomeValue int
    , @UserName nvarchar(256)
    AS
    BEGIN
    INSERT INTO SomeTable(UserName, SomeValue)
    VALUES (@UserName, @SomeValue));
    END;
    GO

    For Application with passthrough Windows Credentials
    CREATE PROCEDURE foo.bar
      @SomeValue int
    AS
    BEGIN
    INSERT INTO SomeTable(UserName, SomeValue)
    VALUES (SUSER_NAME(), @SomeValue));
    END;
    GO