sql-serverdelphiunidac

obtain the real identity of the connected user


dxStatusbar1.Panels1.Text := DataModule2.UniConnectDialog1.Connection.Username;

...gives me the username that has connected to sql server. However the connected user has a different name in the actual database.

Example: His login name for the sql server is 'John' and is user mapped to 'Northwind' database. However in 'Northwind' database he is called 'John Smith'. And this is the name (John Smith) I am trying to have displayed in dxStatusbar1.Panels1.Text after he connects.

How can I get that ?

edit : Tried Victoria suggestion :

UserName := DataModule2.UniConnection1.ExecSQL('SELECT :Result = CURRENT_USER', ['Result']);
 dxStatusbar1.Panels[1].Text := UserName; 

but get :

enter image description here


Solution

  • I couldn't find any UniDAC API way to get currently connected user name (not even for SDAC), so I would just issue a SQL command querying CURRENT_USER and grab the name from the result:

    SELECT CURRENT_USER;
    

    Or in the Unified SQL way with the USER function:

    SELECT {fn USER};
    

    Since you've mentioned stored procedure in your comment, it sounds to me like you probably want to get this information directly from a connection object without using query object. If that is so, you don't even need to have a stored procedure but execute directly command like this:

    var
      UserName: string;
    begin
      UserName := UniConnection1.ExecSQL('SELECT :Result = CURRENT_USER', ['Result']);
      ...
    end;
    

    Or in unified way:

    var
      UserName: string;
    begin
      UserName := UniConnection1.ExecSQL('SELECT :Result = {fn USER}', ['Result']);
      ...
    end;