How to know currently logged in username in Oracle SQL? - sql

How to know currently logged in username in Oracle SQL, if we are dealing with multiple users within the same DB, it is little bit confusing to understand!

By using this query we can get currently logged in user
select user from dual;

There are many different ways.
Option 1
Use the V$SESSION view.
SELECT USERNAME FROM V$SESSION;
Option 2
This one gives all the detailed information because there are times that you have to locate locked sessions.
select
substr(a.spid,1,9) pid,
substr(b.sid,1,5) sid,
substr(b.serial#,1,5) ser#,
substr(b.machine,1,6) box,
substr(b.username,1,10) username,
-- b.server,
substr(b.osuser,1,8) os_user,
substr(b.program,1,30) program
from v$session b, v$process a
where
b.paddr = a.addr
and type='USER'
order by os_user,username;

Related

How to display different interactive grid queries, depending on user privileges

I use Master-Detail page. I want to display whole table only for user who is an administrator. For the user who isn't the administrator, I want to display sql query which will be restrict MD view.
I tried to create another one master detail on same page which is visible only for users without Administrator role. And first MD is not visible for them.(I used Server-side Condition) Is there exist some other way to display different query, depending on user role.
I hope I explained the problem clearly. Thanks in advance
Have a look at the package APEX_ACL, you can use the related views in your where clause.
Example:admins see all rows, other users only see the row for KING
SELECT *
FROM emp
WHERE
-- user is admin ?
( EXISTS ( SELECT 1
FROM apex_appl_acl_user_roles
WHERE application_id = :APP_ID AND
user_name = :APP_USER AND
role_static_id = 'ADMINISTRATOR'
) ) OR
-- user is no admin
( ename = 'KING' );

Select a user by their username and then select data from another table using their UID

Sorry if that title is a bit convoluted... I'm spoiled by an ORM usually and my raw SQL skills are really poor, apparently.
I'm writing an application that links to a vBulletin forum. Users authenticate with their forum username, and the query for that is simple (selecting by username from the users table). The next half of it is more complex. There's also a subscriptions table that has a timestamp in it, but the primary key for these is a user id, not a username.
This is what I've worked out so far:
SELECT
forum.user.userid,
forum.user.usergroupid,
forum.user.password,
forum.user.salt,
forum.user.pmunread,
forum.subscriptionlog.expirydate
FROM
forum.user
JOIN forum.subscriptionlog
WHERE
forum.user.username LIKE 'SomeUSER'
Unfortunately this returns the entirety of the subscriptionlog table, which makes sense because there's no username field in it. Is it possible to grab the subscriptionlog row using the userid I get from forum.user.userid, or does this need to be split into two queries?
Thanks!
The issue is that you are blindly joining the two tables. You need to specify what column they are related by.
I think you want something like:
SELECT * FROM user u
INNER JOIN subscriptionlog sl ON u.id = sl.userid
WHERE u.username LIKE 'SomeUSER'
select * from user u JOIN subscriptions s ON u.id = s.id where u.username = 'someuser'
The bit in bold is what you want to add, it combines the 2 tables into one that you return results from.
try this
SELECT
forum.user.userid,
forum.user.usergroupid,
forum.user.password,
forum.user.salt,
forum.user.pmunread,
forum.subscriptionlog.expirydate
FROM
forum.user
INNER JOIN forum.subscriptionlog
ON forum.subscriptionlog.userid = forum.user.userid
WHERE
forum.user.username LIKE 'SomeUSER'

How can I select users based on whether or not there are multiple rows for that user?

I recently had an interview question that was worded something like this:
Select all users from the user table where the user works for multiple businesses and has a salary over 90000 at at least one of the businesses.
Where each row represented a user. Some users had multiple rows (for when they worked for multiple businesses) and each had a businessId.
I'm still pretty new to SQL, I do a lot of basic select and update statements when writing application code, but I'm not sure how to go about doing this. Should I be using count to find out if there are multiple rows for one user? I'm not sure how to structure it with the where clause to check the salary.
Group by the user (for instance the name). Then you can use the having clause to do aggregate operations in the group - meaning for every user
select username
from users
group by username
having count(distinct businessId) > 1
and max(salary) > 90000

SQL query output using group by

I have this view in an Oracle db and I'm trying to generate a report with an SQL query but it's not working for last few days now so decide to get expert advice.
View does runs well. Basically what view does is it records all users who log into any database for auditing purpose.
View columns:
dbname
username
server
program
logon_time
Questions:
I am trying to generate SQL to group by all username and program who log in to dbname=x on date=26-Dec-2012
count distinct username and program where user logged on 26-Dec-2012
Basically trying to get... if dbname=x get 50000 login via username=abc then abc was connected from which server and what program?
Tried the following query
select dbname, username, server, program, logon_time from audit
where username = abc and
dbname in (select dbname from audit
where dbname='x' and logon_time = to_date('26-DEC-2012','DD-MON-YYY'))
group by username, program
This seems very straight-forward, except for issues with reformatting the date+time information into just date information.
Query 1:
SELECT username, program
FROM Audit
WHERE dbname = 'x'
AND TO_CHAR(logon_time, 'YYYY-MM-DD') = '2012-12-26'
GROUP BY username, program;
This should give you a list of the distinct username, program combinations that logged into database 'x' on 26th December 2012.
Query 2:
You want the COUNT of the rows from Query 1 except for the criterion about database name, so:
SELECT COUNT(*)
FROM (SELECT username, program
FROM Audit
WHERE TO_CHAR(logon_time, 'YYYY-MM-DD') = '2012-12-26'
GROUP BY username, program
)
This should give you a count of the distinct user name, program combinations that connected to any database on 26th December 2012.
You could try something like this:
SELECT dbname, username, server, program, trunc(logon_time), count(*) FROM audit
WHERE username = 'abc'
AND dbname = 'x'
AND logon_time >= to_date('26-DEC-2012','DD-MON-YYY')
AND logon_time < to_date('27-DEC-2012','DD-MON-YYY')
GROUP BY dbname, username, server, program, trunc(logon_time)
Edit: Answer revised to fix grouping mistake Conrad pointed out.
Still not 100% sure what is required, but this should be much closer.

How to show values from two different queries?

I have one database that contains all of user information including name. Then there is a second database that contains notes from the users and it contains the #id but not the name. The query i am doing to retrieve user notes doesn't have name so all its doing is showing the notes, then right under it i am doing another query to retrieve the name from the first database using the common #id. But it won't show.
Is there a way I can do this query in one? Please help. Thanks.
Use:
SELECT u.name,
n.*
FROM DB2.NOTES n
LEFT JOIN DB1.USERS u ON n.id = u.id
ORDER BY u.name
Assuming the connection credentials has access to both databases, you prefix the database name in front of the table name and separate with a period.
The LEFT JOIN will show both users, and notes without users associated. Here's a good primer on JOINs.
You might need to show your code, but you can write queries against two databases (or schemas) on the same host, just qualify the table names with the database name, e.g.
SELECT db1.user.id, db1.user.name, db2.userinfo.notes
FROM db1.user
INNER JOIN db2.userinfo ON(db1.user.id=db2.userinfo.id)
The credentials you are connecting with must have access to both databases for this to work of course.