Get login name in SQL SERVER - sql

I have an orphan user in sql server and I want to get the login name not the user name because they are not the same, I didn't find a solution that correspond to my needs.

You should be able to get it using :
SELECT suser_name()
Docs Link : https://learn.microsoft.com/en-us/sql/t-sql/functions/suser-name-transact-sql

I believe that this is what you need:
SELECT ORIGINAL_LOGIN()

execute as login = 'LCF\jmp'
select distinct name
from sys.login_token
where principal_id > 0
and type = 'WINDOWS GROUP';

Your user is Windows user, so it can login to server due to its membership in some Windows groups.
To find out these groups run the query:
execute as login = 'LCF\jmp'
select distinct name
from sys.login_token
where principal_id > 0
and type = 'WINDOWS GROUP';
And mind that it's not "orphaned", it need not to have it's sid mapped to server to be able to connect to server, Windows groups sids are enough.

On MSSQL 2014 use
select SYSTEM_USER

Related

SQL Server: How to check if a WORKLAD GROUP and a RESOURCE POOL exists

In my SQL script, I want to drop a "WORKLOAD GROUP" and a "RESOURCE POOL", but it might be that they do not exist. How can I check?
I'm using SQL Server 2016.
Thanks in advance!
Frank
I've got the solution from another forum:
IF EXISTS (SELECT * FROM sys.resource_governor_resource_pools WHERE name = N'MyPool')
DROP RESOURCE POOL MyPool
GO
IF EXISTS (SELECT * FROM sys.resource_governor_workload_groups WHERE name = N'MyGroup') DROP WORKLOAD GROUP MyGroup GO

Oracle APEX - Set Page to Read Only based on App User

I am currently trying to make a page read only if the user logged in is an administrator.
My login table has an attribute "login_type" which is either "U" for regular user and "A" for admin. I am attempting to use a "EXISTS" condition type on my page with the following SQL query:
SELECT *
FROM login
WHERE
(
(SELECT login_type FROM login WHERE upper(login_username) = upper(:APP_USER)) = 'A'
)
In theory, this should ensure that anyone logged in, where their user login_type is 'A' should have read only fields on the page. This isn't the case, and I'm struggling to use the read only conditions all together.
Any help?
APEX 5
I set:
Read Only Type condition for page: Rows returned
SQL Query:
SELECT 1
FROM login
WHERE upper(login_username) = upper(:APP_USER)
AND login_type = 'A'
It's worked

Removing domain from sql query results

Say I use the command [MYSQL]:
Select Username, UserType from USERS_DATABASE;
And i get the next results:
Username: UserType:
someDomain/user1 User
someDomain/user2 User
diffDomain/user3 User
Is there any way i can remove the domain through an SQL query or I need to parse it and then remove?
The domains can be different (Length, Characters and so on)
Thanks.
In SQL Server
Select substring(Username,charindex('/',Username)+1,len(Username)) as Username,
UserType
from USERS_DATABASE;
In MySQL
Select substring(Username,locate('/',Username)+1,length(Username)) as Username,
UserType
from USERS_DATABASE;
Assuming T-SQL you can use this:-
SELECT SUBSTRING(Username, CHARINDEX('/', Username) + 1, LEN(Username)), UserType
FROM USERS_DATABASE;

How to list active connections on PostgreSQL?

Is there a command in PostgreSQL to select active connections to a given database?
psql states that I can't drop one of my databases because there are active connections to it, so I would like to see what the connections are (and from which machines)
Oh, I just found that command on PostgreSQL forum:
SELECT * FROM pg_stat_activity;
Following will give you active connections/ queries in postgres DB-
SELECT
pid
,datname
,usename
,application_name
,client_hostname
,client_port
,backend_start
,query_start
,query
,state
FROM pg_stat_activity
WHERE state = 'active';
You may use 'idle' instead of active to get already executed connections/queries.
SELECT * FROM pg_stat_activity WHERE datname = 'dbname' and state = 'active';
Since pg_stat_activity contains connection statistics of all databases having any state, either idle or active, database name and connection state should be included in the query to get the desired output.
You can check connection details in Postgres using pg_stat_activity. You can apply filter to satisfy your condition. Below are queries.
References: https://orahow.com/check-active-connections-in-postgresql/
SELECT * FROM pg_stat_activity WHERE state = 'active';
select * from pg_stat_activity where state = 'active' and datname = 'REPLACE_DB_NAME_HERE';
If you would like to use PgAdmin (for me it is more than convenient), you could do these simple steps. Glad if this is helps
SELECT * FROM pg_stat_activity;

How to find the users list in oracle 11g db?

How to find out the users list, which is all created in the oracle 11g database. Is there any command to find out the users list which we can execute from the Command line interface!
I am not sure what you understand by "execute from the Command line interface", but you're probably looking after the following select statement:
select * from dba_users;
or
select username from dba_users;
select * from all_users
This will work for sure
The command select username from all_users; requires less privileges
You can try the following: (This may be duplicate of the answers posted but I have added description)
Display all users that can be seen by the current user:
SELECT * FROM all_users;
Display all users in the Database:
SELECT * FROM dba_users;
Display the information of the current user:
SELECT * FROM user_users;
Lastly, this will display all users that can be seen by current users based on creation date:
SELECT * FROM all_users
ORDER BY created;
You can think of a mysql database as a schema/user in Oracle. If you have the privileges, you can query the DBA_USERS view to see the list of schema.
I tried this query and it works for me.
SELECT username FROM dba_users
ORDER BY username;
If you want to get the list of all that users which are created by end-user, then you can try this:
SELECT username FROM dba_users where Default_TableSpace not in ('SYSAUX', 'SYSTEM', 'USERS')
ORDER BY username;