How to resolve Case sensitive issue while using sub query in oracle - oracle

select * from ur_username u
join ur_username_person up
on up.username_id=u.username_id
join ur_person p on up.person_id=p.person_id
join ur_system s on u.system_id=s.system_id
WHERE U.USERNAME IN (select username from ur_username where system_id=349 and status='DISABLED') AND U.STATUS='ACTIVE'
in the above code in where clause all the username from the sub query will be stored and I want the all username irrespective of case should be stored in the where clause i tried using (UPPER) but only getting upper case user names alone.
Please give any suggestions here.
For example in the sub query I am getting "ccku" as result but when it comes to where clause I want store both "ccku" and "CCKU" .
Please help me with the above issue

When you tried upper, did you do this in your where clause?
WHERE
UPPER(U.USERNAME) IN
UPPER(select username from ur_username where system_id=349 and status='DISABLED')

This isn't going to be super efficient but you might be able to check the subquery twice. once using upper() and once using lower().
WHERE (upper(U.USERNAME) IN (select username from ur_username where system_id=349 and status='DISABLED')
or lower(U.USERNAME) IN (select username from ur_username where system_id=349 and status='DISABLED'))
AND U.STATUS='ACTIVE'

It is because you have settings of SEC_CASE_SENSITIVE_LOGON parameter as 'TRUE',
setting this parameter to 'FALSE' maybe solves your issue.
SQL> show parameter sec_case_sensitive_logon
SQL> alter system set sec_case_sensitive_logon=false scope=both;

Related

Fetch sql query with Machine Name

I want to fetch sql query with machine name where sql query has been run and machine name can belong's to any user. Please guide how is it possible to get that by joing tables like DBA_hist_sql or any other table.
I can suggest such variant
select
s.sql_id,
s.sql_text,
d.machine
from
v$sql s,
dba_hist_active_sess_history d
where
d.sql_id = s.sql_id
Maybe there is better variant or more related to your question. I hope it wil be helpful for you.
I let you links on documentation of these views.
DBA_HIST_ACTIVE_SESS_HISTORY
V$SQL
You can join DBA_HIST_ACTIVE_SESS_HISTORY and DBA_HIST_SQLTEXT , as long as the sql has been captured in the workload repository.
In the DBA_HIST_ACTIVE_SESS_HISTORY you have the field MACHINE, where you got the value of SYS_CONTEXT('userenv','host') .
You can join both views by sql_id.
However, the query will not be registered on the workload repository if it's not meaningful. You can modify this behaviour by changing settings of AWR using DBMS_WORKLOAD_REPOSITORY.MODIFY_SNAPSHOT_SETTINGS
An example
select
distinct s.sql_id,
s.sql_text,
d.machine ,
u.username ,
d.program
from
gv$sql s inner join dba_hist_active_sess_history d
on ( d.sql_id = s.sql_id and S.INST_ID = D.INSTANCE_NUMBER )
inner join dba_users u on ( D.USER_ID = U.USER_ID )
where
u.username = '&1'
S.SQL_ID = '&2'
order by D.SAMPLE_TIME desc
You can apply the filter by username or sql_id, or both. keep in mind that the field USERNAME will show you the Oracle user who executed the query, not the operating system user behind that connection.

cascading Input Control sql query return error: "ORA-01427: single-row subquery returns more than one row"

looking for solution on my sql query error.I'm trying to create second cascading Input Control in JaspersoftServer. The first Input Control works fine, however when I try to create a second cascade IC it returns with the error. I have 3 tables (user, client, user_client), many to many, so 1 linked table (user_client) between them.The 1st Input Control (client) - works well, end user will select the client, the client can have many users, so cascade is the key. Also, as the output, I would like to get not the user_id, but user's firstname and the lastname as one column field. And here is where i'm stuck. I'm pretty sure it is simple syntaxis error, but spent a good couple of hours to figure out what is wrong with it. Is anyone can have a look at it please and indicate where is the problem in my query ?! So far I've done:
select distinct
u.user_id,(
SELECT CONCAT(first_name, surname) AS user_name from tbl_user ),
c.client_id
FROM tbl_user u
left join tbl_user_client uc
on uc.user_id = u.user_id
left join tbl_client c
on c.client_id = uc.client_id
where c.client_id = uc.client_id
order by c.client_id
Thank you in advance.
P.S. JasperServer + Oracle 11g
You're doing an uncorrelated subquery to get the first/last name from the user table. There is no relationship between that subquery:
SELECT CONCAT(first_name, surname) AS user_name from tbl_user
... and the user ID in the main query, so the subquery will attempt to return every first/last name for all users, for every row your joins find.
You don't need to do a subquery at all as you already have the tbl_user information available:
select u.user_id,
CONCAT(u.first_name, u.surname) AS user_name
c.client_id
FROM tbl_user u
left join tbl_user_client uc
on uc.user_id = u.user_id
left join tbl_client c
on c.client_id = uc.client_id
where c.client_id = uc.client_id
order by c.client_id
If you want to put a space between the first and last name you'll either need nested concat() calls, since that function only takes two arguments:
select u.user_id,
CONCAT(u.first_name, CONCAT(' ', u.surname)) AS user_name
...
... or perhaps more readably use the concatenation operator instead:
select u.user_id,
u.first_name ||' '|| u.surname AS user_name
...
If the first control has selected a client and this query is supposed to find the users related to that client, you're joining the tables the wrong way round, aren't you? And you aren't filtering on the selected client - but no idea how that's actually implemented in Jasper. Maybe you do want the entire list and will filter it on the Jasper side.

DBA_HIST_ACTIVE_SESS_HISTORY get sql by user and object schema

Hi I am learning ASH and AWR tables but any ideas as to how i can get list of sql, objects and schema owner accessed by a give user in last 30 days ? Basically get all SQL text, and then search within this SQL to see if a given object (table, package, function, view etc ) is accessed for a given schema and by which user ? Any ideas suggestion on where and how to start ?
You could join the following views -
DBA_HIST_ACTIVE_SESS_HISTORY
DBA_USERS
DBA_HIST_SQLTEXT
To filter the history for last 30 days, use sample_time of DBA_HIST_ACTIVE_SESS_HISTORY view.
Something like -
SELECT
h.sample_time,
u.username,
h.program,
h.module,
s.sql_text
FROM
DBA_HIST_ACTIVE_SESS_HISTORY h,
DBA_USERS u,
DBA_HIST_SQLTEXT s
WHERE sample_time >= SYSDATE - 30
AND h.user_id=u.user_id
AND h.sql_id = s.sql_iD
ORDER BY h.sample_time
/
The very best and simplest way to fetch related data using below query.
SELECT H.SAMPLE_TIME,
U.USERNAME,
H.PROGRAM,
H.MODULE,
S.SQL_TEXT,
H.SQL_ID,
H.TOP_LEVEL_SQL_ID,
H.BLOCKING_SESSION_STATUS
FROM DBA_HIST_ACTIVE_SESS_HISTORY H, DBA_USERS U, DBA_HIST_SQLTEXT S
WHERE H.SAMPLE_TIME >= SYSDATE - 30
AND H.SQL_ID = S.SQL_ID
--AND H.PROGRAM IN ('Toad.exe', 'SQL Developer')
--AND U.USERNAME ='YOUR_USERNAME'
ORDER BY H.SAMPLE_TIME DESC
In the above code you can also fetch data based on your requirements as below.
1. Custom user data: Just modify YOUR_USERNAME with your real username.
2. Program: Program name can be anything like SQL Developer or JDBC Thin client to identify from which client the queries are getting triggered, but optional.
Hope it will help and answer to your question. Thanks :)

When i change NLS ora gives "ORA-00979: not a GROUP BY expression" error

My Oracle NLS parameters is:
NLS_SORT = TURKISH
NLS_COMP= BINARY
COLUMNA1=VARCHAR2
query:
SELECT COLUMNA1,
(SELECT TABLEB.COLUMB1 FROM TABLEB WHERE TABLEB.COLUMNB2 = TABLEA.COLUMNA1)
FROM TABLEA GROUP BY TABLEA.COLUMNA1
This query worked and return data.
But when i change
NLS_SORT = TURKISH_AI
NLS_COMP= LINGUISTIC
This query points "= TABLEA.COLUMNA1" and gives "ORA-00979: not a GROUP BY expression" error?
In my project, i have a lot of query like this, and i couldnt change all.
And if my query wrong, why run before?
Thanks in advance!

Oracle inner query

My query
select kc.prod_id, kc.prod_actv_ts
from kit_cmpnt kc ,kit_cmpnt_stock kcs, prod p
where kc.cmpnt_cd='016'
and kcs.kit_cmpnt_nbr= kc.kit_cmpnt_nbr
and kcs.stock_id=1
and kcs.prod_id=kc.prod_id
and kcs.prod_actv_ts=kc.prod_actv_ts
and p.prod_id= kc.prod_id
and p.prod_actv_ts= kc.prod_actv_ts
and p.prod_inactv_ts is null;
I want to get a distinct combination of kc.prod_id, kc.prod_actv_ts
like distinct(kc.prod_id, kc.prod_actv_ts)
But what i am getting is a combination of repeated prod_id and prod_actv_ts
please help
I'd restructure the query as follows :
select kc.prod_id, kc.prod_actv_ts
from kit_cmpnt kc
where kc.cmpnt_cd='016'
and exists
(select 1 from kit_cmpnt_stock kcs
where kcs.stock_id=1
and kcs.kit_cmpnt_nbr= kc.kit_cmpnt_nbr
and kcs.prod_id=kc.prod_id
and kcs.prod_actv_ts=kc.prod_actv_ts)
and exists
(select 1 from prod p
where p.prod_id= kc.prod_id
and p.prod_actv_ts= kc.prod_actv_ts
and p.prod_inactv_ts is null);
General principle is that you shouldn't have something in the FROM clause unless you are taking something from it. If you aren't taking anything from it, it is a filter and should be in the WHERE clause as a subquery.
Try using select * to find the reason for the duplicates.
use distinct to filter unique combination...
eg..
select distinct(a,b,c) from table
where
some condition

Resources