Search code examples
oracle-databaseprocedures

How do I incorporate NEXTVAL in my procedure?


I have the following procedure

create or replace 
procedure prod_add_sp
    (p_idproduct in bb_product.idproduct%type,
    p_prodname in bb_product.productname%type, 
    p_descrip in bb_product.description%type,
    p_prodimage in bb_product.productimage%type,
    p_prodprice in bb_product.price%type,
    p_prodactive in bb_product.active%type)
is
begin
   insert into bb_product(idproduct,productname,description,productimage,price,active)
   values (p_idproduct,p_prodname,p_descrip,p_prodimage,p_prodprice,p_prodactive);

commit;
end;

How do I modify the above with the seq.nextval portion so when executed a new row is inserted with a unique primary key? IDPRODUCT is a primary key so it is required.


Solution

    1. Create a sequence with any name say nextID.
    2. Now use below code:

    Create a sequence with any name say nextID. Now use below code:

    create or replace procedure prod_add_sp (p_idproduct in bb_product.idproduct%type, p_prodname in bb_product.productname%type, p_descrip in bb_product.description%type, p_prodimage in bb_product.productimage%type, p_prodprice in bb_product.price%type, p_prodactive in bb_product.active%type) is
    begin 
    insert into bb_product(idproduct,productname,description,
    productimage,price,active)
    values (nextID.nextVal,p_prodname,p_descrip,
    p_prodimage,p_prodprice,p_prodactive);
    
    commit; 
    end;