Stored procedure using Dynamic SQL to create new table in Oracle - oracle

The question is as follows: create a stored procedure (NEW_TABLE) with two strings as input parameters:
String 1: Name of the table to be generated
String 2: Name of columns and their datatypes.
For example: 'ID Number, ProductName varchar2(50), Quantity'
I implemented the following code:
CREATE OR REPLACE PROCEDURE GENERATE_NEW_TABLE
(TEMP_PRODS varchar2, COLUMNS_DATATYPES varchar2)
is
begin
EXECUTE IMMEDIATE
'CREATE TABLE '||TEMP_PRODS||'('||COLUMNS_DATATYPES||')';
end;
I called it as follows:
EXEC GENERATE_NEW_TABLE('PRODUCTS','ID Number, PRODUCT_NAME VARCAR2(50), QUANTITY')
The procedure was created without compilation error, but when I executed this procedure there was error
missing right parenthesis
Kindly share how can I resolve this. I'm only required to take two strings as input parameters for procedure.

When working with dynamic SQL, always display what you're going to execute (using dbms_output.put_line). Only if it is OK, then execute (immediate) it.
SQL> CREATE OR REPLACE PROCEDURE GENERATE_NEW_TABLE
2 (TEMP_PRODS varchar2, COLUMNS_DATATYPES varchar2)
3 is
4 l_str varchar2(200);
5 begin
6 l_str :=
7 'CREATE TABLE '||TEMP_PRODS||'('||COLUMNS_DATATYPES||')';
8 dbms_output.put_line(l_str);
9 --execute immediate l_str; --> don't run it until you make sure L_STR is correct
10 end;
11 /
Procedure created.
Testing:
SQL> set serveroutput on
SQL> EXEC GENERATE_NEW_TABLE('PRODUCTS','ID Number, PRODUCT_NAME VARCAR2(50), QUANTITY')
CREATE TABLE PRODUCTS(ID Number, PRODUCT_NAME VARCAR2(50), QUANTITY) --> this will be executed
PL/SQL procedure successfully completed.
SQL>
Statement you're about to execute is
CREATE TABLE PRODUCTS(ID Number, PRODUCT_NAME VARCAR2(50), QUANTITY)
------- ---
typo missing datatype
Does it look OK to you? Doesn't to me (and other as well).
When fixed:
SQL> EXEC GENERATE_NEW_TABLE('PRODUCTS','ID Number, PRODUCT_NAME VARCHAR2(50), QUANTITY NUMBER')
CREATE TABLE PRODUCTS(ID Number, PRODUCT_NAME VARCHAR2(50), QUANTITY NUMBER)
PL/SQL procedure successfully completed.
SQL>
Does CREATE TABLE look OK now? Yes, it does. So uncomment EXECUTE IMMEDIATE from the procedure and repeat everything:
SQL> CREATE OR REPLACE PROCEDURE GENERATE_NEW_TABLE
2 (TEMP_PRODS varchar2, COLUMNS_DATATYPES varchar2)
3 is
4 l_str varchar2(200);
5 begin
6 l_str :=
7 'CREATE TABLE '||TEMP_PRODS||'('||COLUMNS_DATATYPES||')';
8 dbms_output.put_line(l_str);
9 execute immediate l_str;
10 end;
11 /
Procedure created.
SQL> EXEC GENERATE_NEW_TABLE('PRODUCTS','ID Number, PRODUCT_NAME VARCHAR2(50), QUANTITY NUMBER')
CREATE TABLE PRODUCTS(ID Number, PRODUCT_NAME VARCHAR2(50), QUANTITY NUMBER)
PL/SQL procedure successfully completed.
SQL> DESC PRODUCTS
Name Null? Type
----------------------------------------------------- -------- ----------------------------------
ID NUMBER
PRODUCT_NAME VARCHAR2(50)
QUANTITY NUMBER
SQL>
The table is now created.

Related

How to write PL/SQL function which returns the result of a select statement having as pararmeters the function's parameters?

I tried to write a PL/SQL function having as parameters a tablename and a column name, which returns the result of the query as a table.
Here's what I tried:
CREATE TYPE TABLE_RES_OBJ AS OBJECT (
employee_id number(30) ,
person_id number(30),
joined_year number(4),
salary number(20,2),
qualification varchar2(45),
department varchar2(45)
);
/
create TYPE table_ret as TABLE OF table_res_obj;
/
create or replace function select_custom(p_tablename varchar2, p_colname varchar2 ) return table_ret
is
ret table_ret;
query_txt varchar2(100) := 'SELECT :a from :b';
begin
execute immediate query_txt bulk collect into ret using p_colname, p_tablename;
return ret;
end select_custom;
As you can see, this is not that general as wanted, but still not working, it says the table doesn't exist, even when I try to run it with an existing table.
Exactly, it won't work that way. You'll have to concatenate table and column name into the select statement. For (simple) example:
SQL> create or replace type table_res_obj as object
2 (ename varchar2(20));
3 /
Type created.
SQL> create or replace type table_ret as table of table_res_obj;
2 /
Type created.
SQL> create or replace function select_custom
2 (p_tablename varchar2, p_colname varchar2 )
3 return table_ret
4 is
5 ret table_ret;
6 query_txt varchar2(100);
7 begin
8 query_txt := 'select table_res_obj(' || dbms_assert.simple_sql_name(p_colname) ||') from ' ||
9 dbms_assert.sql_object_name(p_tablename);
10 execute immediate query_txt bulk collect into ret;
11 return ret;
12 end select_custom;
13 /
Function created.
Does it work?
SQL> select select_custom('dept', 'deptno') from dual;
SELECT_CUSTOM('DEPT','DEPTNO')(ENAME)
--------------------------------------------------------------------------------
TABLE_RET(TABLE_RES_OBJ('10'), TABLE_RES_OBJ('20'), TABLE_RES_OBJ('30'), TABLE_R
ES_OBJ('40'))
SQL>

SQL Developer ERROR: Reference to uninitialized collection

I am executing the below procedure from SQL DEVELPER, I get an option to insert the input values for z_id & i_type however i_data is greyed out.
can a procedure with input param as collection cannot be executed from SQL
DEVELOPER tool ?
What am I missing here ? The code gets compiled but unable to execute it through SQL Developer
below is what I have done but this gives compilation error on Oracle 19c
create or replace type my_type AS OBJECT (
p_id number,
p_text varchar2 (100),
p_details clob);
/
create or replace type tab1 is table of my_type;
/
create or replace procedure vehicle_det (z_id in varchar2, i_type in varchar2, i_data in tab1)
IS
BEGIN
for i in 1..i_data.count
LOOP
if (i_type ='BMW')
THEN
UPDATE STOCK_DATA
set stock_id=i_data(i).p_id, stock_des=i_data(i).p_text, clearance=i_data(i).p_details where s_id=z_id ;
end if;
end loop;
end vehicle_det;
Below is the error, please provide your wisdom::::
**ERROR --ORA-06531 Reference to uninitialized collection **
Here's code that actually works. See how it is done.
Types and sample table:
SQL> create or replace type my_type AS OBJECT (
2 p_id number,
3 p_text varchar2 (100),
4 p_details clob);
5 /
Type created.
SQL> create or replace type tab1 is table of my_type;
2 /
Type created.
SQL> create table stock_data
2 (s_id number,
3 stock_id number,
4 stock_des varchar2(20),
5 clearance clob);
Table created.
SQL> insert into stock_data
2 select 1 s_id, 1 stock_id, 'Description' stock_Des, null clearance
3 from dual;
1 row created.
SQL>
Procedure:
SQL> create or replace procedure vehicle_det
2 (z_id in varchar2, i_type in varchar2, i_data in tab1)
3 IS
4 BEGIN
5 for i in 1..i_data.count LOOP
6 if i_type = 'BMW' THEN
7 UPDATE STOCK_DATA
8 set stock_id = i_data(i).p_id,
9 stock_des = i_data(i).p_text,
10 clearance = i_data(i).p_details
11 where s_id = z_id ;
12 end if;
13 end loop;
14 end vehicle_det;
15 /
Procedure created.
Testing: current table contents / run the procedure / see the result:
SQL> select * from stock_data;
S_ID STOCK_ID STOCK_DES CLEARANCE
---------- ---------- -------------------- ------------------------------
1 1 Description
SQL> declare
2 l_data tab1 := tab1();
3 begin
4 l_data.extend;
5 l_data(1) := my_type(1, 'This is BMW', 'Oh yes, this IS a BMW!');
6
7 vehicle_det(1, 'BMW', l_data);
8 end;
9 /
PL/SQL procedure successfully completed.
SQL> select * from stock_data;
S_ID STOCK_ID STOCK_DES CLEARANCE
---------- ---------- -------------------- ------------------------------
1 1 This is BMW Oh yes, this IS a BMW!
SQL>

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.

call pl/sql from another one in oracle database problem

i have 2 stored procedures 1 is called A with the following impl
PROCEDURE A(p_id IN NUMBER, lic_cat_2 OUT varchar2,lic_cat_1 OUT varchar2,traffic_code OUT varchar2,lic_type OUT varchar2,emp_num OUT varchar2)
// Some LOGIC
end A ;
and PROCEDURE B which is a wrapper to proc A but i need to get other value with a query
PROCEDURE B(ph_id IN NUMBER, lic_cat_2 OUT varchar2,lic_cat_1 OUT varchar2,traffic_code OUT varchar2,lic_type OUT varchar2,emp_num OUT varchar2)
declare number phone_id
begin
select into phone_id parent_id from per_phones where phone_id= p_id
exec A(phone_id,lic_cat_2 OUT varchar2,lic_cat_1 OUT varchar2,traffic_code OUT varchar2,lic_type OUT varchar2,emp_num OUT varchar2);
END B;
but it gives me PLS-00103: Encountered the symbol “CREATE”
You're calling the A procedure in a wrong manner:
omit EXEC, it is a SQL*Plus command
omit parameters' description (IN/OUT, datatype) - pass only values
omit DECLARE; you need it in triggers or anonymous PL/SQL blocks, but not in stored procedures
by the way, variable name comes first, datatype next (for phone_id)
I'd suggest you to prefix parameters and variables with p_ (or par_) and l_ respectively (or any other prefix you want) to distinguish them from column names. Otherwise, it is easy to get confused.
also, use table aliases in your queries for the same reason
So:
CREATE OR REPLACE PROCEDURE B (p_ph_id IN NUMBER,
p_lic_cat_2 OUT VARCHAR2,
p_lic_cat_1 OUT VARCHAR2,
p_traffic_code OUT VARCHAR2,
p_lic_type OUT VARCHAR2,
p_emp_num OUT VARCHAR2)
IS
l_phone_id NUMBER;
BEGIN
SELECT p.parent_id
INTO l_phone_id
FROM per_phones p
WHERE p.phone_id = p_ph_id;
A (l_phone_id,
p_lic_cat_2,
p_lic_cat_1,
p_traffic_code,
p_lic_type,
p_emp_num);
END B;
As I don't have your tables, for example (to show how to do it) I used Scott's sample schema:
SQL> create or replace procedure a (par_deptno in number, par_dname out varchar2)
2 is
3 begin
4 select dname into par_dname from dept where deptno = par_deptno;
5 end;
6 /
Procedure created.
SQL>
SQL> create or replace procedure b (par_empno in number, par_dname out varchar2) is
2 l_deptno emp.deptno%type;
3 begin
4 select deptno into l_deptno from emp where empno = par_empno;
5
6 a(l_deptno, par_dname);
7 end;
8 /
Procedure created.
SQL>
SQL> set serveroutput on
SQL> declare
2 l_dname dept.dname%type;
3 begin
4 b (7654, l_dname);
5 dbms_output.put_line('Dname = ' || l_dname);
6 end;
7 /
Dname = SALES
PL/SQL procedure successfully completed.
SQL>

Resources