Oracle Add procedure using pl/sql - oracle

The Procedure is like below!
I'm new to oracle. As i copy the sqlserver procedure to oracle and change some parts. It will be a great thxs help me solve this !
CREATE OR REPLACE PROCEDURE SP_GetAdminResource
(
AdminId IN NVARCHAR2,
p_ResultSet OUT sys_refcursor
)
AS
BEGIN
WITH T AS(
SELECT T1.ResId, T1.UpResId
FROM SYS_Resource T1
WHERE T1.IsActive = 1
AND T1.ResId IN (SELECT DISTINCT T2.ResId
FROM SYS_RoleResource T2
WHERE T2.RoleId IN
(SELECT T3.RoleId
FROM SYS_RoleAdministrator T3, SYS_Role T10
WHERE T10.RoleId = T3.RoleId
AND T10.IsActive = 1
AND T3.AdminId = AdminId))),
TT AS (SELECT *
FROM T
UNION ALL (SELECT T4.ResId, T4.UpResId
FROM SYS_Resource T4, T
WHERE T4.IsActive = 1
AND T4.ResId = T.UpResId)),
SELECT T5.ResId, T5.UpResId, T5.ResIcon,T5.ResName, T5.ResUrl,T5.OrderNum,T8.ActionCode
FROM SYS_Resource T5 INTO p_ResultSet
LEFT JOIN (SELECT T6.ResId, T6.ActionCode
FROM SYS_RoleResource T6
WHERE T6.RoleId IN
(SELECT T7.RoleId
FROM SYS_RoleAdministrator T7, SYS_Role T9
WHERE T9.RoleId = T7.RoleId
AND T9.IsActive = 1
AND T7.AdminId = AdminId)) T8 ON T5.ResId =
T8.ResId
WHERE T5.IsActive = 1
AND T5.ResId IN (SELECT DISTINCT TT.ResId FROM TT)
ORDER BY T5.OrderNum ASC, T5.ResName ASC;
END SP_GetAdminResource;

Check below the working version of your procedure. I have put my comments in between . Please go through it and work accordingly.
CREATE OR REPLACE PROCEDURE SP_GetAdminResource
(
AdminId IN NVARCHAR2,
p_ResultSet OUT sys_refcursor
)
AS
BEGIN
Open p_ResultSet for -- this is the way to use refcursor in your procedure
WITH T AS(
SELECT T1.ResId, T1.UpResId
FROM SYS_Resource T1
WHERE T1.IsActive = 1
AND T1.ResId IN (SELECT DISTINCT T2.ResId
FROM SYS_RoleResource T2
WHERE T2.RoleId IN
(SELECT T3.RoleId
FROM SYS_RoleAdministrator T3, SYS_Role T10
WHERE T10.RoleId = T3.RoleId
AND T10.IsActive = 1
AND T3.AdminId = t2.AdminId))), ----Check which AdminId need to be joined.I joined it with T2
TT AS (SELECT T.ResId, T.UpResId
FROM T
UNION ALL (SELECT T4.ResId, T4.UpResId
FROM SYS_Resource T4, T
WHERE T4.IsActive = 1
AND T4.ResId = T.UpResId) )
-- , ----Extra Need to remove it
SELECT T5.ResId,
T5.UpResId
-- T5.ResIcon, ---Uncomment these columns in your query, I just made tables to test without these columns.
-- T5.ResName,
-- T5.ResUrl,
-- T5.OrderNum,
-- T8.ActionCode
FROM SYS_Resource T5
-- INTO p_ResultSet -------------------------------No need to fetch into sysrefcursor.
LEFT JOIN (SELECT T6.ResId, T6.ActionCode
FROM SYS_RoleResource T6
WHERE T6.RoleId IN
(SELECT T7.RoleId
FROM SYS_RoleAdministrator T7, SYS_Role T9
WHERE T9.RoleId = T7.RoleId
AND T9.IsActive = 1
AND T7.AdminId = t6.AdminId)) T8 ON T5.ResId = ------Check which AdminId need to be joined.I joined it with T6
T8.ResId
WHERE T5.IsActive = 1
AND T5.ResId IN (SELECT DISTINCT TT.ResId FROM TT)
ORDER BY T5.OrderNum ASC, T5.ResName ASC ;
END SP_GetAdminResource;

Related

OR in Select Oracle

select * from table1 t1 where t1.column1 = 'someValue' and ((t1.column2 =1) OR (sysdate < select t1.DateColumn2 + t2.DateColumn2/1440
from
table2 t2 where t1.column3 = t2.column3));
if t1.column2 =1 evaluates to false, I want to check another condition if time t1.DateColumn2 + t2.DateColumn2 is < sysdate . Oracle is throwing syntax error near or condition. Cant sysdate be used directly like that? Not sure where I am going wrong. Thanks
If I am guessing your intention correctly, you want an exists clause
select *
from table1 t1
where t1.column1 = 'someValue'
and ( (t1.column2 =1)
OR exists( select 1
from table2 t2
where t2.column3 = t1.column3
and sysdate < t1.DateColumn2 + t2.DateColumn2/1440 ));
Or just join the two tables in the outer query assuming there is at most 1 row in t2 per t1 row (if there is exactly 1 row you should do an inner join rather than a left outer join)
select t1.*
from table1 t1
left outer join table2 t2
on( t1.column3 = t2.column3 )
where t1.column2 = 1
or sysdate < t1.DateColumn2 + t2.DateColumn2/1440;

Converting Oracle Query to Hive

How can I convert the below query in Oracle to Hive?
SELECT A.EMP_NO, A.LOGIN_TIMESTAMP FROM TABLE1 A, TABLE2 B
WHERE A.EMP_NO = 1234 AND B.EMP_CURR =
(SELECT MIN(EMP_CURR) FROM TABLE2 WHERE EMP_NO = A.EMP_NO AND
LOGIN_TIMESTAMP = A.LOGIN_TIMESTAMP AND EMP_STATUS_CODE <> 'P')
Use dense_rank() to get rows with minimum EMP_CURR:
SELECT A.EMP_NO, A.LOGIN_TIMESTAMP
FROM TABLE1 A
INNER JOIN (select B.*,
dense_rank() over(partition by B.EMP_NO, B.LOGIN_TIMESTAMP order by B.EMP_CURR) rn
from TABLE2 B where EMP_STATUS_CODE <> 'P'
) B
on B.EMP_NO = A.EMP_NO and B.LOGIN_TIMESTAMP = A.LOGIN_TIMESTAMP and B.rn=1
where B.rn=1 and A.EMP_NO = 1234;

Oracle delete from tableA where a duplicate row is in tableB

As the title says, I am looking for a way to remove all rows from TableA where there is a matching row in TableB.
the Tables A & B have about 30 columns in them so a WHERE A.col1 = B.col1 etc would be a little problematical. Ideally I was hoping for something like
DELETE FROM tableA WHERE IN TableB
(overly simplified by this type of thing)
IN clause can compare all columns returned from select
DELETE FROM tableA WHERE ( col1,col2,col3,.. ) IN ( select col1,col2,col3... FROM TableB );
The brute force way to establish if two records from each table are the same is to just compare every column:
DELETE
FROM tableA a
WHERE EXISTS (SELECT 1 FROM tableB b WHERE a.col1 = b.col1 AND a.col2 = b.col2 AND ...
a.col30 = b.col30);
You could create function which checks structures of tables and, if they are the same, creates string containing correct conditions to compare.
For example here are two tables:
create table t1 (id, name, age) as (
select 1, 'Tom', 67 from dual union all
select 2, 'Tia', 42 from dual union all
select 3, 'Bob', 16 from dual );
create table t2 (id, name, age) as (
select 1, 'Tom', 51 from dual union all
select 3, 'Bob', 16 from dual );
Now use function:
select generate_condition('T1', 'T2') from dual;
result:
T1.ID = T2.ID and T1.NAME = T2.NAME and T1.AGE = T2.AGE
Copy this, paste and run delete query:
delete from t1 where exists (select 1 from t2 where <<PASTE_HERE>>)
Here is the function, adjust it if needed. I used user_tab_columns so if tables are on different schemas you need all_tab_columns and compare owners too. If you have Oracle 11g you can replace loop with listagg(). Second table has to contain all columns of first table and they have to be same type and length.
create or replace function generate_condition(i_t1 in varchar2, i_t2 in varchar2)
return varchar2 is
v varchar2(1000) := '';
begin
for rec in (select column_name, u2.column_id
from user_tab_cols u1
left join (select * from user_tab_cols where table_name = i_t2) u2
using (column_name, data_type, data_length)
where u1.table_name = i_t1 order by u1.column_id)
loop
if rec.column_id is null then
v := 'ERR: incompatible structures';
goto end_loop;
end if;
v := v||' and '||i_t1||'.'||rec.column_name
||' = '||i_t2||'.'||rec.column_name;
end loop;
<< end_loop >>
return(ltrim(v, ' and '));
end;
If you want to avoid running process manually you need dynamic PL/SQL.
create table tableA (a NUMBER, b VARCHAR2(5), c INTEGER);
create table tableB (a NUMBER, b VARCHAR2(5), c INTEGER);
As you said
WHERE A.col1 = B.col1 etc would be a little problematical
you could intersect the tables and mention all columns from tableA one time, like this:
delete tableA
where (a,b,c) in (select * from tableA
intersect
select * from tableB);

Return non-null value from two tables in Oracle

I have two tables, T1 and T2 with same set of columns. I need to issue a query which will return me value of columns from either table whichever is not null. If both columns are null return null as the value of that column.
The columns are c1,c2,c3,cond1.
I issued the following query. The problem is that if one subquery fails the whole query fails. Somebody please help me. Probably there is another simple way.
SELECT NVL(T1.c1, T2.c1) c1,NVL(T1.c2, T2.c2) c2,NVL(T1.c3, T2.c3) c3
FROM (SELECT c1,c2,c3
FROM T1
WHERE cond1 = 'T10') T1
,(SELECT c1,c2,c3
FROM T2
WHERE cond1 = 'T200') T2 ;
You need something like this:
SELECT NVL((SELECT T1.c1
FROM T1
WHERE T1.c2 = 'T10'),
(SELECT T2.c1
FROM T2
WHERE T2.c2 = 'T200')) AS c1
FROM dual
Or you may prefer a full outer join:
SELECT NVL(T1.c1, T2.c1) AS c1
FROM T1 FULL OUTER JOIN T2 ON 1=1
WHERE T1.c2 = 'T10'
AND T2.c2 = 'T200'
Your result is logical. If the first table is null no combination of values will exist in the natural join.
EDIT. After some new requirements we can use a hack to get the row. Lets get all three possibilities, T1, T2 or all nulls and select the first one:
SELECT *
FROM ( (SELECT T1.*
FROM T1
WHERE T1.c2 = 'T10')
UNION ALL
(SELECT T2.*
FROM T2
WHERE T2.c2 = 'T200')
UNION ALL
(SELECT T2.*
FROM dual
LEFT JOIN T1 ON 1 = 0 ) )
WHERE ROWNUM = 1

Is this query OK? [Oracle 8i]

I've got this query in Oracle 11g [working fine]:
select (case
when seqnum = 1 then
t.DPR_N
when seqnum = cnt then
'0'
end) as VALUE1,
(case
when seqnum = 1 then
t.BEGIN_DT
when seqnum = cnt then
t.END_DT
end) as timestamp,
t2.APP_NAAM || '.SUBBATCH_TRIGGER' TAGNAME1
from (select t.*,t6.*,
row_number() over(partition by t.BATCH_ID, t.PLANT_UNIT,t6.DPR_ID order by t.BEGIN_DT) as seqnum,
count(*) over(partition by t.BATCH_ID, t.PLANT_UNIT,t6.DPR_ID) as cnt
FROM tb_unit_step t
INNER JOIN tb_equipment t2
ON t2.PLANT_UNIT = t.PLANT_UNIT
INNER JOIN tb_rs3 t3
ON t.BATCH_ID = t3.BATCH_ID
INNER JOIN tb_cpm t9
ON t9.BACPM_ID = t3.BACPM_ID
INNER JOIN tb_step t4
ON (t9.BV_ID = t4.BV_ID
AND t.STAP_NR1 = t4.STAP_NR1
AND t.STAP_NR2 = t4.STAP_NR2)
INNER JOIN tb_bv t5
ON t5.BV_ID = t9.BV_ID
INNER JOIN tb_bv_process t6
ON t9.BV_ID = t6.BV_ID
AND t6.DPR_ID = t4.DPR_ID
INNER JOIN tb_ins t7
ON (t7.INS_ID = t4.INS_ID)
INNER JOIN tb_cpm t8
ON t8.BV_ID = t9.BV_ID
WHERE (t.BEGIN_DT > ? AND t.END_DT < ?)
) t
join tb_equipment t2 on t2.plant_unit = t.plant_unit
where (seqnum = 1
or seqnum = cnt);
I've got to make it work on Oracle 8i [I know it's a REALLY old version, but I have no choice since it's not my DB]. I've built this query in order to get the data from Oracle 8i: [I've changed CASE WHEN for DECODE and removed all the ANSI JOINs]
SELECT DECODE(SEQNUM, 1, T.DPR_N,CNT,'0') VALUE1,
DECODE(SEQNUM, 1, T.BEGIN_DT,CNT,T.END_DT) TIMESTAMP,
'090.' || T2.APP_NAAM
|| '.SUBBATCH_TRIGGER' TAGNAME1
FROM
(SELECT T.*,
T6.*,
ROW_NUMBER() OVER(PARTITION BY T.BATCH_ID, T.PLANT_UNIT,T6.DPR_ID ORDER BY T.BEGIN_DT) SEQNUM,
COUNT(*) OVER(PARTITION BY T.BATCH_ID, T.PLANT_UNIT,T6.DPR_ID) CNT
FROM tb_unit_step T ,
tb_equipment T2 ,
tb_rs3 T3 ,
tb_cpm T9 ,
tb_step T4 ,
tb_bv T5 ,
tb_bv_process T6 ,
tb_ins T7 ,
tb_cpm T8
WHERE T2.PLANT_UNIT = T.PLANT_UNIT
AND T.BATCH_ID = T3.BATCH_ID
AND (T9.BV_ID = T4.BV_ID
AND T.STAP_NR1 = T4.STAP_NR1
AND T.STAP_NR2 = T4.STAP_NR2)
AND T5.BV_ID = T9.BV_ID
AND (T9.BV_ID = T6.BV_ID
AND T6.DPR_ID = T4.DPR_ID)
AND T7.INS_ID = T4.INS_ID
AND T8.BV_ID = T9.BV_ID
AND (T.BEGIN_DT > '15-jul-2013'
AND T.END_DT < '01-aug-2014')
) T
,tb_equipment T2
WHERE T2.PLANT_UNIT = T.PLANT_UNIT
AND (T.SEQNUM = 1
OR SEQNUM = T.CNT)
;
The new query is definately not OK because it's taking forever to run. So what would be the correct form of the first query in order to get data from Oracle 8i?
UPDATE:
Result of the query:
ORA-01652: unable to extend temp segment by 128 in tablespace TEMP
01652. 00000 - "unable to extend temp segment by %s in tablespace %s"
*Cause: Failed to allocate an extent of the required number of blocks for
a temporary segment in the tablespace indicated.
*Action: Use ALTER TABLESPACE ADD DATAFILE statement to add one or more
files to the tablespace indicated.
Thanks in advance!
I don't see this condition in your Oracle 8 version:
t9.BACPM_ID = t3.BACPM_ID
That could explain the performance problem.

Resources