how to store the result of a set operation into a variable - oracle

i tried it this way
set #v1 := (SELECT SUBSTR(IFSC,1,4) FROM rtgs_new MINUS SELECT SUBSTR(IFSC,1,4) FROM rtgs_temp);
or
set #v1 = (SELECT SUBSTR(IFSC,1,4) FROM rtgs_new MINUS SELECT SUBSTR(IFSC,1,4) FROM rtgs_temp);
but i get this error:
SP2-0158: unknown SET option "#v1"
i want to use this variable to compare with and insert into another oracle table.
any information will be helpful.
thank you guys!

If you just need to insert into another table, why don't you do that?
insert into another_table
SELECT SUBSTR(IFSC,1,4) FROM rtgs_new MINUS SELECT SUBSTR(IFSC,1,4) FROM rtgs_temp;

Related

can I use `=` sign operator with sub-query instead of `IN`

I am just wondering if use = sign operator with sub-query instead of IN
Is it correct way ? and meet the oracle standard ?
Example
select column_name from my_table_1 where id = (select max(id) from my_table_2);
The Difference is related to the number of rows returned. If you have only one row returned from nested sql you may prefer both = or in operators. But if multiple rows returned from nested query, use in operator.
So, in your sql example you may prefer using any of the operators. Since, max functions returns only one row.
As you are fetching maximum value from subquery to compare with id, Both(= and IN )will work fine. But If you are trying to fetch more than one row then you have to use IN keyword.
If you have 1 result in sub query you are fine with using = sign, except when data type is wrong, for example , checking with same data type of dummy VARCHAR2(1)
select * from dual where 'X' = (select max(dual.dummy) from dual);
Is similar to using in (also same explain plain)
select * from dual where 'X' in (select max(dual.dummy) from dual);
But checking with different/wrong data type will result with exception ORA-01722 Invalid number
select * from dual where 1 =(select max(dual.dummy) from dual);

Select query succeeds however Insert using same query fails with ORA-00979 (not a GROUP BY expression)

I am having a hard time finding why the below insert would fail while the select alone executes successfully.
INSERT INTO I6_POC_ADJ_OUTPUT
(LOADID,ADJ_ID,ADJ_CATEGORY,ADJ_COMMENT,ITEM,CHANNEL,FDATE,ADJ_TOT_QTY,FQTY)
SELECT
ADJ.LOADID,ADJ.ADJ_ID,ADJ.ADJ_CATEGORY,ADJ.ADJ_COMMENT,ADJ.ITEM,ADJ.CHANNEL,ADJ.FDATE,MAX(ADJ.FQTY) ADJ_TOT_QTY,
SUM(ADJ.TP_SPLIT) FQTY
FROM I3_POC_ADJ_INPUT ADJ,V_POC_HIST_BASE BASE
WHERE ADJ.ITEM=BASE.ITEM(+) AND ADJ.CHANNEL=BASE.CHANNEL(+) AND ADJ.FDATE=BASE.FDATE(+)
AND ADJ.LOADID=71 AND ADJ.ITEM='10-56-034' AND ADJ.CHANNEL='CH1820' AND ADJ.FDATE=DATE'2017-09-03'
GROUP BY ADJ.LOADID,ADJ.ADJ_ID,ADJ.ADJ_CATEGORY,ADJ.ADJ_COMMENT,ADJ.ITEM,ADJ.CHANNEL,ADJ.FDATE;
Select and Insert Executions
Update:
Tried with a simple CTAS and it works.
CREATE TABLE TEST_ADJ_OUTPUT AS
SELECT * FROM (
SELECT
ADJ.LOADID,ADJ.ADJ_ID,ADJ.ADJ_CATEGORY,ADJ.ADJ_COMMENT,ADJ.ITEM,ADJ.CHANNEL,ADJ.FDATE,MAX(ADJ.FQTY),
SUM(ADJ.FQTY*ADJ.TP_SPLIT)
FROM I3_POC_ADJ_INPUT ADJ,V_POC_HIST_BASE BASE
WHERE ADJ.ITEM=BASE.ITEM(+) AND ADJ.CHANNEL=BASE.CHANNEL(+) AND ADJ.FDATE=BASE.FDATE(+)
AND ADJ.LOADID=71 AND ADJ.ITEM='10-56-034' AND ADJ.CHANNEL='CH1820' AND ADJ.FDATE=DATE'2017-09-03'
GROUP BY ADJ.LOADID,ADJ.ADJ_ID,ADJ.ADJ_CATEGORY,ADJ.ADJ_COMMENT,ADJ.ITEM,ADJ.CHANNEL,ADJ.FDATE
);
Truncated the same table and tried inserting into the same but if fails with the same error. "SQL Error: ORA-00979: not a GROUP BY expression"
TRUNCATE TABLE TEST_ADJ_OUTPUT;
INSERT INTO TEST_ADJ_OUTPUT
SELECT * FROM (
SELECT
ADJ.LOADID,ADJ.ADJ_ID,ADJ.ADJ_CATEGORY,ADJ.ADJ_COMMENT,ADJ.ITEM,ADJ.CHANNEL,ADJ.FDATE,MAX(ADJ.FQTY),
SUM(ADJ.FQTY*ADJ.TP_SPLIT)
FROM I3_POC_ADJ_INPUT ADJ,V_POC_HIST_BASE BASE
WHERE ADJ.ITEM=BASE.ITEM(+) AND ADJ.CHANNEL=BASE.CHANNEL(+) AND ADJ.FDATE=BASE.FDATE(+)
AND ADJ.LOADID=71 AND ADJ.ITEM='10-56-034' AND ADJ.CHANNEL='CH1820' AND ADJ.FDATE=DATE'2017-09-03'
GROUP BY ADJ.LOADID,ADJ.ADJ_ID,ADJ.ADJ_CATEGORY,ADJ.ADJ_COMMENT,ADJ.ITEM,ADJ.CHANNEL,ADJ.FDATE
);
Appreciate your help.
Thanks,
Manoj

SELECT within UPDATE gives an error

I am getting SQL Error: ORA-01779: cannot modify a column which maps to a non key-preserved table error on this statement:
UPDATE
(
SELECT CELLS.NUM, UND.CLIENT_PARAMS
FROM CELLS
LEFT OUTER JOIN UND
ON CELLS.UND_ID = UND.ID
WHERE CELLS.SASE = 1
) t
SET t.CLIENT_PARAMS = 'test';
I would like to update CLIENT_PARAMS field for all rows, which select returns.
The most straightforward (though possibly not the most efficient) way to update rows in one table which correspond directly to rows in another table via an identity column would be to use WHERE table1.column IN (SELECT id FROM table2 WHERE ...).
In this case:
UPDATE UND
SET client_params = 'test'
WHERE id IN
(SELECT und_id
FROM CELLS
WHERE SASE=1)
Try this
UPDATE und u
SET client_params = 'test'
WHERE EXISTS
(SELECT 1
FROM cells c
WHERE C.SASE = 1
AND c.und_id = u.id)

Oracle Insert, Update, Delete Trigger with Join

I'm trying to implement Oracle triggers for child views but I need to be able to join the child views to their parents in order to do a role permission check.
In SQL Server I'm able to stuff like this:
ALTER TRIGGER [dbo].[ASetTrt_I] ON [dbo].[UCV_ASet_TRT]
INSTEAD OF Insert AS
BEGIN
SET NOCOUNT ON;
IF EXISTS (SELECT 1 from INSERTED i INNER JOIN [Analysis_Sets] p on i.[key] = p.[ID]
WHERE ([dbo].IsMemberOf(p.[UpdateRole]) <> 1 and [dbo].IsMemberOf('db_owner') <> 1))
RAISERROR ('Update failed due to insufficient permission',11,1)
INSERT INTO [Set_Trts] ( [ID], [Name], [key], [f_lTreatmentKey], [f_lOrder] )
SELECT
inserted.[ID], inserted.[Name], inserted.[key], inserted.[f_lTreatmentKey], inserted.[f_lOrder]
FROM inserted
INNER JOIN [Sets] parentT
on inserted.[key] = parentT.[ID]
WHERE (([dbo].IsMemberOf('db_owner')=1) or ([dbo].IsMemberOf(parentT.[UpdateRole])=1))
END
Is there anything I can do in Oracle to replicate the join functionality?
I've tried selecting from :New the way that SS selects from inserted but that doesn't seem to work..
Thanks.
You don't need to join. The:new pseudorow is just available and can be referenced like a record type. It isn't a table-like structure, and is only available in a for each row trigger. So your insert would be something like:
INSERT INTO Analysis_Set_Trts ( ID, Name, f_lAnalysisSetKey, f_lTreatmentKey,
f_lOrder )
SELECT :new.ID, :new.Name, :new.f_lAnalysisSetKey, :new.f_lTreatmentKey,
:new.f_lOrder
FROM Analysis_Sets parentT
WHERE parentT.ID = :new.f_lAnalysisSetKey
AND ((IsMemberOf('db_owner')=1) or (IsMemberOf(parentT.UpdateRole)=1));
... although not quite sure what the last line is doing or what the equivalent is.

Oracle-update multiple row value where the the id column equals specific values

I am new in oracle and I want to update multiple row value where the column id = 1 , 2.
I tried this :
update Tester
set employee_phone_number = ( 0789456123,0789456321)
where employee_id in (1,2);
but it gives me "missing right parenthesis"
any help please and thanks in advance.
Another approach:
merge into
tester
using (
select 1 id,'0123456785' employee_phone_number from dual union all
select 2 id,'0123456786' employee_phone_number from dual) new_values
on (
tester.id = new_values.id)
when matched then update
set employee_phone_number = new_values.employee_phone_number;
More words, but allows the values to be specified in only one place and extends to allow inserts where the id does not already exist.
http://sqlfiddle.com/#!4/8fc86/3
Try this instead:
update Tester
set employee_phone_number = CASE WHEN employee_id = 1 THEN 0789456123
WHEN employee_id = 2 THEN 0789456321
END
where employee_id in (1,2);

Resources