how to fire two oracle insert statement together?Oracle 11g - oracle

I am trying to fire two insert statements at a time. Actually i have tried with below query but its inserting in only one table.
EXECUTE IMMEDIATE 'select * from abc.test where test_NAME = ''aaa''' BULK COLLECT INTO T_SC;
IF T_SC.count = 0 THEN
Insert into abc.test (test_ID,test_NAME,status)
VALUES(1,'aaa','a') BULK COLLECT INTO insert_cnt;
IF insert_cnt.count = 1 THEN
INSERT INTO abc.test1(test1_id,test1_NAME,test1_ALIAS,test_ID)
VALUES(1,'bbb','b',1);
COMMIT;
END IF;
it is only inserting in abc.test1 table..What i am going to missing. If anyone knows than plz help me in this.

This whole code of yours doesn't seem right:
why the dynamic sql ?
there are lots of syntax errors
unclosed "if" (as vj shah commented)
missing returning keyword
why do you need the bulk collect if your returning from the insert only one row ?
what is the second "if" for ?
and so on...
Anyway, this code works:
EXECUTE IMMEDIATE 'select * from abc.test where test_NAME = ''aaa''' BULK COLLECT INTO T_SC;
/* BTW, why not
select * bulk collect into T_SC from abc.test where test_NAME = 'aaa';
*/
IF T_SC.count = 0 THEN
Insert into abc.test (test_ID, test_NAME, status)
VALUES(1,'aaa','a') returning test_ID, test_NAME, status BULK COLLECT INTO insert_cnt;
IF insert_cnt.count = 1 THEN
INSERT INTO abc.test1(test1_id,test1_NAME,test1_ALIAS,test_ID)
VALUES(1,'bbb','b',1);
END IF;
COMMIT;
END IF;

Can you explain your problem a little better. Neither your logic nor the data that you show gives any idea of what you are trying to accomplish. (the logic behind the if).
This is also not functional code (too many syntax errors), can you update with the real code you are firing? May be just change the table names?
If you want to make sure both the statements either complete successfully or both of them are rolled back, your approach of including them in a block is correct.
SQL> create table test_rc_2(
2 id number
3 );
Table created.
--Sample 1 : Submitting inserts seperately (only the latest statement is rolled
SQL> insert into test_rc_2 values (100);
1 row created.
SQL> insert into test_rc_2 values ('hello');
insert into test_rc_2 values ('hello')
*
ERROR at line 1:
ORA-01722: invalid number
SQL> commit;
Commit complete.
SQL> select * from test_rc_2;
ID
----------
100
--case 2 : submittig them in a block.
SQL> truncate table test_rc_2
2 ;
Table truncated.
SQL> begin
2 insert into test_rc_2 values(100);
3 insert into test_rc_2 values('hello..');
4 end;
5 /
begin
*
ERROR at line 1:
ORA-01722: invalid number
ORA-06512: at line 3
SQL> commit;
Commit complete.
SQL> select * from test_rc_2;
no rows selected

Related

Instead of trigger with Oracle sequence

I have some old applications (legacy applications) which uses their own MAX NUMBER table. The problem is that we cannot modify and release all the applications at the same time. And therefore, we would like to do it in phases.
I came up with the following proposal. But, I do not know why Oracle executes the view's select query when I am executing the update statement.
Current usage:
-- Update the max number table
Update Sysadm.DummyTable
Set MaxNumber = MaxNumber + 1;
-- An application needs to select current value from the MaxNumber table.
Select * From Sysadm.DummyTable;
Proposed solution:
CREATE SEQUENCE SYSADM.SEQ_DummyTable START WITH 12345 INCREMENT BY 1;
--- Common function ---
--- The following function will get the current sequence number of the Oracle sequence across all the sessions.
CREATE OR REPLACE FUNCTION Sysadm.GetCurrValue(sLSeqName VARCHAR)
RETURN NUMBER
AS nLDummy NUMBER;
BEGIN
if (sLSeqName = 'SEQ_DUMMYTABLE') Then
SELECT SEQ_DUMMYTABLE.CURRVAL Into nLDummy FROM DUAL;
Else
Return null;
End If;
RETURN(nLDummy);
END;
/
--- The existing DummyTable table will be dropped and DummyTable (exactly with the same name including its structure) view will be created.
Create or Replace View Sysadm.DummyTable (MAXNUMBER)
As
Select sysadm.GetCurrValue('SEQ_DUMMYTABLE') As MAXNUMBER From Sysadm.ABC Where RowNum = 1;
--- The following trigger will get executed when the application tried to update the DummyTable table. This trigger will get executed instead of update statement from the application.
Create Or Replace Trigger Sysadm.TR_DummyTable
Instead of UPDATE ON Sysadm.DummyTable
Declare
nLDummy NUMBER;
Begin
SELECT SEQ_DUMMYTABLE.NEXTVAL Into nLDummy FROM DUAL;
End;
/
I have Instead of trigger on the view. However, When I am trying to execute the following update query I get an error message.
-- Update the max number table
Update Sysadm.DummyTable
Set MaxNumber = MaxNumber + 1;
ORA error message:
Error starting at line : 1 in command -
Update Sysadm.DUMMYTABLE Set MaxNumber = MaxNumber + 1
Error report -
SQL Error: ORA-08002: sequence SEQ_DUMMYTABLE.CURRVAL is not yet defined in this session
ORA-06512: at "SYSADM.GETCURRVALUE", line 10
08002. 00000 - "sequence %s.CURRVAL is not yet defined in this session"
*Cause: sequence CURRVAL has been selected before sequence NEXTVAL
*Action: select NEXTVAL from the sequence before selecting CURRVAL
As Oracle told you: you can't fetch currval if you didn't fetch from the sequence within this session, and you do that by selecting nextval. For example:
SQL> create sequence seqa;
Sequence created.
This doesn't work - you already know that, you hit the error already:
SQL> select seqa.currval from dual;
select seqa.currval from dual
*
ERROR at line 1:
ORA-08002: sequence SEQA.CURRVAL is not yet defined in this session
So: nextval first, currval next (if you need it):
SQL> select seqa.nextval from dual;
NEXTVAL
----------
1
SQL> select seqa.currval from dual;
CURRVAL
----------
1
SQL>
In your case, it means that this would look like
if (sLSeqName = 'SEQ_DUMMYTABLE') Then
SELECT SEQ_DUMMYTABLE.NEXTVAL Into nLDummy FROM DUAL;
Else -------
By just changing the function we can get it to work. This will fix the concurrency issue as well:
--- The following function will get the current sequence number of the Oracle sequence across all the sessions.
CREATE OR REPLACE FUNCTION Sysadm.GetCurrValue(sLSeqName VARCHAR)
RETURN NUMBER
AS nLDummy NUMBER;
BEGIN
if (sLSeqName = 'SEQ_DUMMYTABLE') Then
SELECT SEQ_DUMMYTABLE.CURRVAL Into nLDummy FROM DUAL;
Else
Return null;
End If;
RETURN(nLDummy);
EXCEPTION
WHEN OTHERS THEN
RETURN NULL;
END;
/

how to generate a table of random data from existing database table through oracle procedure

I have to generate a table (contains two columns) of random data from a database table through oracle procedure. The user can indicate the number of data required and we have to use the table data with ID values from 1001 to 1060. I am trying to use cursor loop and not sure dbms_random method dhould I use.
I am using the following code to create procedure
create or replace procedure a05_random_plant(p_count in number)
as
v_count number := p_count;
cursor c is
select plant_id, common_name
from ppl_plants
where rownum = v_count
order by dbms_random.value;
begin
delete from a05_random_plants_table;
for c_table in c
loop
insert into a05_random_plants_table(plant_id, plant_name)
values (c_table.plant_id, c_table.common_name);
end loop;
end;
/
it complied successfully. Then I executed with the following code
set serveroutput on
exec a05_random_plant(5);
it shows anonymous block completed
but when run the following code, I do not get any records
select * from a05_random_plants_table;
The rownum=value would not work for a value greater than 1
hence try the below
create or replace procedure a05_random_plant(p_count in number)
as
v_count number := p_count;
cursor c is
select plant_id, common_name
from ppl_plants
where rownum <= v_count
order by dbms_random.value;
begin
delete from a05_random_plants_table;
for c_table in c
loop
insert into a05_random_plants_table(plant_id, plant_name)
values (c_table.plant_id, c_table.common_name);
end loop;
end;
/
Query by Tom Kyte - will generate almost 75K of rows:
select trunc(sysdate,'year')+mod(rownum,365) TRANS_DATE,
mod(rownum,100) CUST_ID,
abs(dbms_random.random)/100 SALES_AMOUNT
from all_objects
/
You can use this example to write your query and add where clause to it - where id between 1001 and 1060, for example.
I don't think you should use a cursor (which is slow naturally) but do a direct insert from a select:
insert into table (col1, col2)
select colx, coly from other_table...
And, isn't missing a COMMIT on the end of your procedure?
So, all code in your procedure would be a DELETE, a INSERT WITH that SELECT and then a COMMIT.

Oracle create trigger error (bad bind variable)

I at trying to create trigger with the following code.
CREATE OR REPLACE TRIGGER MYTABLE_TRG
BEFORE INSERT ON MYTABLE
FOR EACH ROW
BEGIN
select MYTABLE_SEQ.nextval into :new.id from dual;
END;
I am getting error
Error(2,52): PLS-00049: bad bind variable 'NEW.ID'
Any ideas? Thanks.
It seems like the error code is telling you there's no such column ID in your table...
Somehow your environment is treating your code as SQL instead of a DDL statement. This works for me (running in sqlplus.exe from a command prompt):
SQL> create sequence mytable_seq;
Sequence created.
SQL> create table mytable (id number);
Table created.
SQL> CREATE OR REPLACE TRIGGER MYTABLE_TRG
2 BEFORE INSERT ON MYTABLE
3 FOR EACH ROW
4 BEGIN
5 select MYTABLE_SEQ.nextval into :new.id from dual;
6 END;
7 /
Trigger created.
Note the trailing "/" - this might be important in the application you are compiling this with.
if one would use proper naming convention the spotting of this type
of errors would be much easier ( where proper means using pre- and postfixes )
for generic object names hinting about their purpose better
i.e. something like this would have spotted the correct answer
--START -- CREATE A SEQUENCE
/*
create table "TBL_NAME" (
"TBL_NAME_ID" number(19,0) NOT NULL
, ...
*/
--------------------------------------------------------
-- drop the sequence if it exists
-- select * from user_sequences ;
--------------------------------------------------------
declare
c int;
begin
select count(*) into c from user_sequences
where SEQUENCE_NAME = upper('SEQ_TBL_NAME');
if c = 1 then
execute immediate 'DROP SEQUENCE SEQ_TBL_NAME';
end if;
end;
/
CREATE SEQUENCE "SEQ_TBL_NAME"
MINVALUE 1 MAXVALUE 999999999999999999999999999
INCREMENT BY 1 START WITH 1
CACHE 20 NOORDER NOCYCLE ;
-- CREATE
CREATE OR REPLACE TRIGGER "TRG_TBL_NAME"
BEFORE INSERT
ON "TBL_NAME"
REFERENCING NEW AS New OLD AS Old
FOR EACH ROW
DECLARE
tmpVar NUMBER;
BEGIN
tmpVar := 1 ;
SELECT SEQ_TBL_NAME.NEXTVAL INTO tmpVar FROM dual;
:NEW.TBL_NAME_ID := tmpVar;
END TRG_TBL_NAME;
/
ALTER TRIGGER "TRG_TBL_NAME" ENABLE;
-- STOP -- CREATE THE TRIGGER
If you're like me and your code should be working, try dropping the trigger explicitly before you re-create it. Stupid Oracle.

How to query and execute trigger on the same table

Hope someone can help with this. I am new to triggers and I am trying to create a trigger that checks to see if the record being modified has a specific value.
example
I have a table called Filing that has a filing_id and a filing_status, I want to prevent someone from updating or deleting any records in that table has a filing_status="FILED".
so if i have the following
Filing_id Filing_status Val
--------- ------------- ---
0 Filed X
If someone tried to modify Val the trigger should stop it
I have created the following trigger:
CREATE or replace TRIGGER TRG_PREV_FILING
BEFORE DELETE or UPDATE
ON PF.FILING
FOR EACH ROW
declare
rowcnt number;
BEGIN
SELECT COUNT(filing_id) INTO rowcnt FROM PF.FILING
where status = 'FILED'
and filing_id = :new.filing_id;
if (rowcnt > 0)
then
raise_application_error (-20100, 'You can not delete Or Update initial record');
end if;
END;
The problem I am facing is I am getting:ORA-04091 which is "Table Filing is mutating, Trigger/function may not see it"
So basically I can't query on the same table that I am executing the trigger on? Is that the problem in my case and does anyone know a work around this?
I appreciate any help
You do not have to query the table trigger is firing on to be able to do that kind of check. You can get value of a column that is being modified using :old. Here is an example:
SQL> create table filing(
2 status varchar2(31),
3 val number
4 );
Table created
SQL> create or replace trigger TRG_FILING before delete or update on FILING
2 for each row
3 begin
4 if lower(:old.status) = 'filed'
5 then
6 raise_application_error(-20000, 'You cannot delete or modify this record');
7 end if;
8 end;
SQL> /
Trigger created
SQL> insert into FILING values('FILED', null);
1 row inserted
SQL> insert into filing values('OK', 1);
1 row inserted
SQL> commit;
Commit complete
SQL> select *
2 from filing;
STATUS VAL
------------------------------- ----------
FILED
OK 1
SQL> delete
2 from filing
3 where val is null;
ORA-20000: You cannot delete or modify this record
ORA-06512: at "HR.TRG_FILING", line 4
ORA-04088: error during execution of trigger 'HR.TRG_FILING'
The basic point is that you should design you database in a way that the trigger does its validation based on the updated/deleted row. If you have several rows with the same filing_id then you can overwork you database design. Maybe you really only check against the own table in which case you can use :old. But when you have several rows to check (which I assume because you make a count) then you have to use two tables. Here is a suggestion.
create table filing_status (filing_id number, status varchar2(10));
create table filing_content (filing_id number, content_id number, content varchar2(200));
CREATE or replace TRIGGER TRG_PREV_FILING
BEFORE DELETE or UPDATE
ON FILING_content
FOR EACH ROW
declare
rowcnt number;
BEGIN
SELECT COUNT(filing_id) INTO rowcnt FROM FILING_status
where status = 'FILED'
and filing_id = :new.filing_id;
if (rowcnt > 0)
then
raise_application_error (-20100, 'You can not delete Or update filed record');
end if;
END;
/
insert into filing_status values (1, 'FILING');
insert into filing_content values (1, 1, 'foo');
insert into filing_content values (1, 2, 'bar');
insert into filing_status values (1, 'FILED');
update filing_content set content = 'bahr' where filing_id = 1 and content_id = 2;
ERROR at line 1:
ORA-20100: You can not delete Or update filed record
ORA-06512: at "DEMO.TRG_PREV_FILING", line 9
ORA-04088: error during execution of trigger 'DEMO.TRG_PREV_FILING'

Reasonable SELECT ... INTO Oracle solution for case of multiple OR no rows

I just want to SELECT values into variables from inside a procedure.
SELECT blah1,blah2 INTO var1_,var2_
FROM ...
Sometimes a large complex query will have no rows sometimes it will have more than one -- both cases lead to exceptions. I would love to replace the exception behavior with implicit behavior similiar to:
No rows = no value change, Multiple rows = use last
I can constrain the result set easily enough for the "multiple rows" case but "no rows" is much more difficult for situations where you can't use an aggregate function in the SELECT.
Is there any special workarounds or suggestions? Looking to avoid significantly rewriting queries or executing twice to get a rowcount before executing SELECT INTO.
Whats wrong with using an exception block?
create or replace
procedure p(v_job VARCHAR2) IS
v_ename VARCHAR2(255);
begin
select ename into v_ename
from (
select ename
from scott.emp
where job = v_job
order by v_ename desc )
where rownum = 1;
DBMS_OUTPUT.PUT_LINE('Found Rows Logic Here -> Found ' || v_ename);
EXCEPTION WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No Rows found logic here');
end;
SQL> begin
p('FOO');
p('CLERK');
end; 2 3 4
5 /
No Rows found logic here
Found Rows Logic Here -> Found SMITH
PL/SQL procedure successfully completed.
SQL>
You could use a for loop. A for loop would do nothing for no rows returned and would be applied to every row returned if there where multiples. You could adjust your select so that it only returns the last row.
begin
for ARow in (select *
from tableA ta
Where ta.value = ???) loop
-- do something to ARow
end loop;
end;

Resources