Oracle Procedure to Copy data view into table - oracle

I would need to copy or insert data from my_view (that is based on 3 joined tables). Before that, I've created table my_table based on my_view .
For the procedure I've used the below :
create or replace procedure view_copy(
my_view in varchar2,
my_table in varchar2)
is
begin
execute immediate 'insert into '||my_table||' (select * from '||my_view||')';
end;
The data are not copied to my_table. Is it possible to copy data from view to table? How can I write the procedure differently?

Your procedure works; if you can't find data in your target table, maybe you should check your view.
A simple test on your procedure:
SQL> create table table1 ( id1 number)
2 /
Table created.
SQL> create table table2 ( id2 number)
2 /
Table created.
SQL> create or replace view v_t1_t2 as select id1, id2 from table1 cross join table2
2 /
View created.
SQL> create table table1_2 ( id1 number, id2 number)
2 /
Table created.
SQL> create or replace procedure view_copy(
2 my_view in varchar2,
3 my_table in varchar2)
4 is
5 begin
6 execute immediate 'insert into '||my_table||' (select * from '||my_view||')';
7 end;
8 /
Procedure created.
SQL> insert into table1 values (1);
1 row created.
SQL> insert into table2 values (2);
1 row created.
SQL> exec view_copy('v_t1_t2', 'table1_2');
PL/SQL procedure successfully completed.
SQL> select * from table1_2;
ID1 ID2
---------- ----------
1 2

Related

In Oracle SQL, I would like to call a Oracle stored procedure and then select the value of an OUT parameter as a column result. Is this possible?

CREATE OR REPLACE PROCEDURE myStoredProcedure (idParam IN VARCHAR2,
outputParam OUT VARCHAR2)
AS
BEGIN
SELECT OUTPUTCOL INTO outputParam FROM MyTable WHERE ID = idParam;
END;
DECLARE
v_OutputResults VARCHAR2(20);
BEGIN
myStoredProcedure('123', v_OutputResults);
SELECT v_OutputResults AS ColumnResult FROM DUAL;
END;
If we understand your goal, you should be using a function, not a procedure:
First, we set up the example:
SQL> -- conn system/halftrack#pdb01
SQL> conn scott/tiger#pdb01
Connected.
SQL> --
SQL> CREATE TABLE my_table (
2 user_id number not null,
3 Name varchar2(10)
4 )
5 ;
Table created.
SQL> --
SQL> insert into my_table values (1,'Bob');
1 row created.
SQL> insert into my_table values (2,'Carol');
1 row created.
SQL> insert into my_table values (3,'Ted');
1 row created.
SQL> insert into my_table values (4,'Alice');
1 row created.
SQL> commit;
Commit complete.
SQL> select * from my_table;
USER_ID NAME
---------- ----------
1 Bob
2 Carol
3 Ted
4 Alice
4 rows selected.
Now we create the function, then use it:
SQL> --
SQL> create or replace function my_function (id_param number)
2 return varchar2
3 is
4 v_name varchar2(10);
5 begin
6 select name
7 into v_name
8 from my_table
9 where user_id = id_param
10 ;
11 --
12 return v_name;
13 end;
14 /
Function created.
SQL> show errors
No errors.
SQL> --
SQL> select my_function(1) from dual;
MY_FUNCTION(1)
--------------------------------------------------------------------------------
Bob
1 row selected.
And clean up our example:
SQL> --
SQL> drop table my_table purge;
Table dropped.
No but you can do so using a stored function.

Procedures giving error : ORA-00955: name is already used by an existing object

CREATE TABLE product
( product_id number(10) NOT NULL,
product_name varchar2(50) NOT NULL,
price varchar2(50)
);
CREATE OR REPLACE PROCEDURE p1 (product_id in number,
product_name in varchar2,
price in number)
IS
BEGIN
INSERT INTO product values(1,'yash',100);
DBMS_OUTPUT.PUT_LINE('VALUE INSERTED');
end;
Error: ORA-00955: name is already used by an existing object
Can someone please provide me and suitable example
From the error message, the name p1 is already in use (as an object name whose type is different than a procedure). Just give the procedure a different (and more descriptive!) name:
CREATE OR REPLACE PROCEDURE insert_product
(product_id in number,
product_name in varchar2,
price in number)
IS
BEGIN
INSERT INTO product values(1,'yash',100);
DBMS_OUTPUT.PUT_LINE('VALUE INSERTED');
END;
For your this problem the solution is write the following line (EXECUTE p1;)
By typing this command you will get your desired output
You have twice asked for a 'suitable example' or 'justifying code'. I assume from that you don't understand or don't believe what has been explained about your procedure name conflicting with some other object name. So here's the example that proves it.
SQL> --
SQL> -- check that we don't own anything called MYTEST
SQL> --
SQL> select object_name
2 from user_objects
3 where upper(object_name) = 'MYTEST'
4 ;
no rows selected
SQL> --
SQL> -- create a table MYTEST
SQL> --
SQL> create table mytest (dob date);
Table created.
SQL> --
SQL> -- check objets
SQL> --
SQL> select object_type,
2 object_name
3 from user_objects
4 where upper(object_name) = 'MYTEST'
5 ;
OBJECT_TYPE OBJECT_NAME
------------------- --------------------
TABLE MYTEST
1 row selected.
SQL> --
SQL> -- create a procedure MYTEST
SQL> --
SQL> create or replace procedure mytest
2 as
3 begin
4 dbms_output.put_line('Hello world');
5 end;
6 /
create or replace procedure mytest
*
ERROR at line 1:
ORA-00955: name is already used by an existing object
SQL> --
SQL> -- now, drop the table and try the procedure again
SQL> --
SQL> drop table mytest purge;
Table dropped.
SQL> create or replace procedure mytest
2 as
3 begin
4 dbms_output.put_line('Hello world');
5 end;
6 /
Procedure created.
SQL> --
SQL> -- cleanup
SQL> --
SQL> drop table mytest purge;
drop table mytest purge
*
ERROR at line 1:
ORA-00942: table or view does not exist
SQL> drop procedure mytest;
Procedure dropped.
In your case if your procedure is named 'p1' then check:
select object_type,
object_name
from user_objects
where upper(object_name) = 'P1'
;

In Oracle, how do i create stored procedure to insert value in 2 tables

I am trying to insert below details in two table. but it shows error. where i am wrong?
create or replace PROCEDURE ADD_customer_order(
customer_id in varchar, Shipping_id_arg in number,
order_date_arg in date, Total_price_arg in decimal,
inventory_id_arg in number, order_quantity_arg in number)
AS
BEGIN
INSERT INTO customer_order (customer_id,Shipping_id,Order_date,total_price) VALUES(customer_id_arg,Shipping_id_arg,order_date_arg, total_price_arg);
insert into order_details (inventory_id,order_quantity) values(scope_identity(),inventory_id_arg,order_quantity_arg);
END;
It helps if you format it nicely.
create or replace procedure add_customer_order(
customer_id in varchar2, shipping_id_arg in number,
order_date_arg in date , total_price_arg in decimal,
inventory_id_arg in number , order_quantity_arg in number)
as
begin
insert into customer_order
(customer_id , shipping_id , order_date , total_price)
values
(customer_id_arg, shipping_id_arg, order_date_arg, total_price_arg);
insert into order_details
( inventory_id , order_quantity)
values
(scope_identity(), inventory_id_arg, order_quantity_arg);
end;
Doing so, you easily note that the second INSERT is invalid, as you're inserting 3 values into 2 columns:
insert into order_details
( inventory_id , order_quantity)
values
(scope_identity(), inventory_id_arg, order_quantity_arg);
Either remove scope_identity() (what is it?), or include additional column into the column list you're inserting into.
After reading your comment, it seems that returning clause might help. See the following example (somewhat simpler than yours; didn't feel like typing that much). Trigger is used to auto-increment ORDER_ID column. in CUSTOMER_ORDER table (I'm on 11g XE; don't have identity columns here).
SQL> create table customer_order (order_id number, customer_id number);
Table created.
SQL> create table order_details (order_id number, inventory_id number);
Table created.
SQL> create sequence seqo;
Sequence created.
SQL> create or replace trigger trg_co
2 before insert on customer_order
3 for each row
4 begin
5 :new.order_id := seqo.nextval;
6 end;
7 /
Trigger created.
Procedure: note local variable declared in line #4 and returning clause in line #7:
SQL> create or replace procedure p_test
2 (par_customer_id in number, par_inventory_id in number)
3 is
4 l_order_id customer_order.order_id%type;
5 begin
6 insert into customer_order (customer_id) values (par_customer_id)
7 returning order_id into l_order_id;
8
9 insert into order_details (order_id, inventory_id)
10 values (l_order_id, par_inventory_id);
11 end;
12 /
Procedure created.
Testing:
SQL> exec p_test(100, 200);
PL/SQL procedure successfully completed.
SQL> exec p_test (235, 2230);
PL/SQL procedure successfully completed.
SQL> select * From customer_order;
ORDER_ID CUSTOMER_ID
---------- -----------
1 100
2 235
SQL> select * From order_details;
ORDER_ID INVENTORY_ID
---------- ------------
1 200
2 2230
SQL>
The same ORDER_ID value is used in both tables.

Count of table before and after insert inside a trigger

Is it possible to check the count of a table before any changes happen and the count after the insert and match them inside the same trigger?
for ex: old.count and new.count (before and after insert) ?
old.count and new.count (before and after insert)
Nothing stops you from using SELECT COUNT(*) in a before insert trigger. Of course, you won't do it in a after insert trigger, since a select count(*) on the same table on which an after trigger is defined would throw mutating table error. One way is autonomous transaction. But, in your case, it isn't that complex.
You could define a BEFORE INSERT TRIGGER and take the table count. The after insert count could be taken manually after the actual insert is done.
For example, I have a table t1 with one row. I have defined a before insert trigger on it, which would give me the table count before the insert happens.
SQL> DROP TABLE t1 PURGE;
Table dropped.
SQL>
SQL> CREATE TABLE t1 (A NUMBER);
Table created.
SQL>
SQL> INSERT INTO t1 VALUES (1);
1 row created.
SQL>
SQL> SELECT * FROM t1;
A
----------
1
SQL>
SQL> CREATE OR REPLACE TRIGGER trg
2 BEFORE INSERT
3 ON t1
4 FOR EACH ROW
5
6 DECLARE
7 val number;
8 BEGIN
9 SELECT COUNT(*)
10 INTO val
11 FROM t1;
12
13 DBMS_OUTPUT.PUT_LINE('TABLE COUNT BEFORE INSERT = '||val);
14
15 END;
16 /
Trigger created.
SQL>
SQL> set serveroutput on
SQL> INSERT INTO t1 VALUES (1);
TABLE COUNT BEFORE INSERT = 1
1 row created.
SQL>
SQL> SELECT COUNT(*) FROM t1;
COUNT(*)
----------
2
SQL>
So, you see TABLE COUNT BEFORE INSERT = 1 and then after insert the count is 2.

Passing an array of strings to a procedure and using it in the WHERE clause with IN

I want to pass a list of strings to a procedure and use it in the WHERE clause of a select statement, but I can't figure out how to do this.
I have declared the following nested table:
TYPE t_strarray IS TABLE OF VARCHAR2(30);
The procedure looks as follows:
PROCEDURE getstuff(p_list IN t_strarray, io_cursor OUT t_cursor)
IS
BEGIN
OPEN io_cursor FOR
SELECT * FROM mytable
WHERE mytable.field1 in (select * from table(p_list));
END;
How can this be done?
Your array needs to be a SQL object type, created directly in SQL, not a PLSQL type declared in a package:
SQL> CREATE OR REPLACE TYPE t_strarray IS TABLE OF VARCHAR2(30);
2 /
Type created.
SQL> CREATE TABLE mytable (field1 VARCHAR2(30));
Table created.
SQL> INSERT INTO mytable VALUES ('A');
1 row created.
SQL> INSERT INTO mytable VALUES ('D');
1 row created.
SQL> CREATE OR REPLACE PROCEDURE getstuff(p_list IN t_strarray,
2 io_cursor OUT SYS_REFCURSOR) IS
3 BEGIN
4 OPEN io_cursor FOR
5 SELECT *
6 FROM mytable
7 WHERE mytable.field1 IN (SELECT COLUMN_VALUE FROM TABLE(p_list));
8 END;
9 /
Procedure created.
SQL> VARIABLE cc REFCURSOR;
SQL> EXEC getstuff (t_strarray('A', 'B', 'C'), :cc);
PL/SQL procedure successfully completed.
SQL> print cc
FIELD1
------------------------------
A

Resources