SQL Stored Procedure did not insert result - sql

I have created below stored procedure under a package (retrieve_user) in the SQL database to retrieve information from another table to insert it into the global temp table (global_temp_tableB).
procedure Load_BTable (NumID IN NUMBER) AS
begin
insert into global_temp_tableB (name, age, address, country, year)
select name, age, address, country, year
from table(prepare_tableB.find_User(NumID));
commit;
end Load_LFTable;
But when I tried to test and call the procedure from a SQL window using:
begin
retrieve_user.Load_BTable(numid => 739);
end;
the global_temp_tableB is still empty but when I use the actual INSERT statement instead of calling stored procedure:
begin
insert into global_temp_tableB (name, age, address, country, year)
select name, age, address, country, year
from table(prepare_tableB.find_User(739));
end;
it can return me with the result desired. So I believe that the INSERT statement in the stored procedure is working.
But why it is that when I call the stored procedure, it does not return me with the desired result?
Thank you very much for all your time and attention.

IF OBJECT_ID(N'Load_BTable',N'P') IS NOT NULL
DROP PROCEDURE Load_BTable
GO
CREATE PROCEDURE Load_BTable
(
#NumID INT
)
WITH ENCRYPTION
AS
BEGIN
SET NOCOUNT ON
insert into global_temp_tableB (name, age, address, country, year)
select name, age, address, country, year
from prepare_tableB WHERE NumId=>#NumID
SET NOCOUNT OFF
END
GO
and try this
exec Load_BTable 739

Related

Transferring data from temporary table to main table using cursor plsql block

I have 2 tables.
temp table has 12 columns
main table has 14 columns
Main table has incrementing id and the one more column have a default value
I need to put the values in the main table from temp table using cursors and looping
This is what I have so far
DECLARE
type R_REQ is record ( v_branch accountrequest.branch%type,
v_account_type accountrequest.account_type%type,
v_title accountrequest.title%type,
v_firstname accountrequest.firstname%type,
v_lastname accountrequest.lastname%type,
v_birthday accountrequest.birthday%type,
v_workphone accountrequest.workphone%type,
v_homephone accountrequest.homephone%type,
v_address accountrequest.address%type,
v_state accountrequest.address%type,
v_zip accountrequest.zip%type,
v_email accountrequest.email%type );
V_REQ R_REQ;
CURSOR C_REQ IS SELECT * FROM ACCOUNTREQUEST_TEMP;
BEGIN
OPEN C_REQ;
LOOP
INSERT C_REQ INTO V_REQ;
exit when c_req%notfound;
dbms.output.put_line(V_REQ.BRANCH, V_REQ.ACCOUNT_TYPE, V_REQ.TITLE, V_REQ.FIRSTNAME, V_REQ.LASTNAME, V_REQ.BIRTHDAY, V_REQ.WORKPHONE,V_REQ.HOMEPHONE,V_REQ.ADDRESS,V_REQ.STATE,V_REQ.ZIP,V_REQ.EMAIL);
END LOOP;
CLOSE C_REQ;
END;
Unless it is for educational purposes, you don't need PL/SQL (especially not a loop). SQL can handle it:
insert into main_table (branch, account_type, title, firstname, ...)
select branch, account_type, title, firstname, ...
from temp_table;
ID will be auto-incremented (so you don't have to specify it in INSERT)
column with default value will get it (so you don't have to specify it either)
If it has to be a loop, then
begin
for cur_r in (select branch, account_type, title, firstname, ...
from temp_table)
loop
insert into main_table (branch, account_type, title, firstname, ...)
values (cur_r.branch, cur_r.account_type, cur_r.title, cur_r.firstname, ...);
end loop;
end;
/
This is a cursor FOR loop which is simpler to use than explicitly declared cursor (and a cursor variable, opening the cursor, taking care about exiting the loop, closing the cursor) because Oracle does all that for you, only if you use cursor FOR loop. I suggest you do so.

Use returned values from a query in the same procedure

I'm quite new to pgpsql. I have two tables - product and product_category - with a one to many relationship. I'm creating a procedure to insert a value into the category table if it doesn't exist and I would like to use the id for the category to insert a product.
I've tried using an alias for the first insert and then returning the id value so I can select it, but I get an error.
What's the right way for using returned in the same procedure values in pgpsql?
CREATE OR REPLACE PROCEDURE insert_product(name VARCHAR, category VARCHAR)
LANGUAGE plpgsql
as $$
BEGIN
WITH ins_category AS(
INSERT INTO product_category(name)
VALUES('laptops')
ON CONFLICT (name) DO NOTHING
RETURNING id
)
INSERT INTO product(name, category)
VALUES(name, ins_category.id); // HERE IS THE ERROR
COMMIT;
END;
$$
Use a SELECT instead of VALUES.
WITH ins_category
AS
(
...
)
INSERT INTO product (name,
category)
SELECT name,
ins_category.id
FROM ins_category;

Oracle Stored Procedure Running Errors

I am attempting my first stored procedure for Oracle because my previous insert statement wasn't working. I get these weird errors that I have been googling for hours on and no one seems to have corrections to my specific issue.
I just want to be able to add records :/
My code is as follows:
CREATE OR REPLACE Insert_classifieds
(val_date IN TABLENAME.Addate%type,
val_category IN TABLENAME.Category%type,
val_user IN TABLENAME.Username%type,
val_ phone IN TABLENAME.Phonenbr%type,
val_email IN TABLENAME.Email%type,
val_shortDes IN TABLENAME.Description%type,
val_longDes IN TABLENAME.Fulldescription%type ,
val_newstandardid out TABLENAME.Classid%type
)
as num_standardid number;
begin
select t_class_seq.nextval into num_standardid from dual;
INSERT INTO TABLENAME (Classid, Addate, Category, Username, Phonenbr, Email, Description, Fulldescription)
VALUES (num_standardid, val_date, val_category, val_user, val_phone, val_email, val_shortDes, val_longDes);
commit;
val_newstandardid := num_standardid;
end;
If you are trying to create a procedure, the syntax is (you were missing the "PROCEDURE" Keyword):
CREATE OR REPLACE PROCEDURE Insert_classifieds
So my issue was that val_phone had a space and a few touch up corrections.
final:
CREATE OR REPLACE PROCEDURE Insert_classifieds
(val_date TABLENAME.Addate%type,
val_category TABLENAME.Category%type,
val_user TABLENAME.Username%type,
val_phone TABLENAME.phonenbr%type,
val_email TABLENAME.Email%type,
val_shortDes TABLENAME.Description%type,
val_longDes TABLENAME.Fulldescription%type)
as num_classid number;
begin
select t_class_seq.nextval into num_classid from dual;
INSERT INTO TABLENAME (Classid, Addate, Category, Username, Phonenbr, Email, Description, Fulldescription)
values (num_classid, val_date, val_category, val_user, val_phone, val_email, val_shortDes, val_longDes);
commit;
end;

Oracle SQL insert query - into parent and child tables

for an assignment I had something similar to the following (simplified for brevity):
STUDENT(StudentID, Fname. Lname) StudentID PK
UNIT(UnitID, UnitName) UnitID PK
STUDENT_UNIT((StudentID, UnitID) StudentID PK/FK UnitID PK/FK
Needed to insert info about a student and the units that he/she had completed.
As it is only beginner level SQL the following was accepted
INSERT INTO STUDENT
VALUES(seqStudID.NextVal, 'Bob', 'Brown');
INSERT INTO STUDENT_UNIT(seqStudID.CurrVal, 111);
INSERT INTO STUDENT_UNIT(seqStudID.CurrVal, 222);
INSERT INTO STUDENT_UNIT(seqStudID.CurrVal, 333);
But I was wondering what would be the real way to enter this data, would it be a procedure with a loop? If so what sort of loop (so that it could handle any amount of units).
Thanks in advance
One of the best approach to do this is by using stored procedure. The below procedure will do everything for you.
CREATE OR REPLACE
PROCEDURE set_stud_unit(
i_fname IN VARCHAR2,
i_lname IN VARCHAR2,
i_unitid IN VARCHAR2)
IS
l_studentid student.studentid%TYPE;
BEGIN
INSERT INTO student(studentid, fname, lname)
VALUES(seqstudid.NEXTVAL, i_fname, i_lname)
RETURNING studentid INTO l_studentid;
INSERT INTO student_unit (studentid, unitid)
(SELECT l_studentid, (COLUMN_VALUE).getNumberVal() vid FROM xmltable(i_unitid));
COMMIT;
END;
/
You can pass the unitid as comma separated as below,
EXECUTE set_stud_unit('Bob', 'Brown', '111,222,333');
you can use select in your insert:
INSERT INTO STUDENT_UNIT select t1.StudentID ,t2.UnitID from STUDENT t1 ,UNIT t2;
and you can use where to limit this selection ;-)
Probably what you need is a procedure that accept:
First Name
Last Name
an array of Units
The procedure will:
Insert into STUDENT (I suggest you to use a trigger to populate StudentID, maybe you are already doing it),
Loop over the array and insert into STUDENT_UNIT each of the element of the array and the StudentID for First Name and Last Name, but without using the sequence. My pseudo code:
FOR i IN 1..input_array.count LOOP
INSERT INTO STUDENT_UNIT
SELECT StudentID, input_array(i)
FROM STUDENT
WHERE Fname = FirstNameParam
AND Lname = LastNameParam;
END LOOP;
I suggest you to query the student table to get the actual ID to avoid problems in case of concurrency. An optimization would be to query the STUDENT table only once and save the StudentID in a variable.
You can find more info about passing an array to an Oracle procedure here:
Passing an array of data as an input parameter to an Oracle procedure

ORA-00984 column not allowed here

I am getting error
"Execute-984 ORA-00984: column not allowed here"
while I am inserting values in my table Registred_Customer using Pro*C
Registred_Customer is defined as
CREATE TABLE Registred_Customer (
Cust_id NUMBER(6) PRIMARY KEY,
Name VARCHAR2(20) NOT NULL,
Age NUMBER,
Sex CHAR,
Addr VARCHAR2(50),
Contact NUMBER(10)
);
Inserting values using a pro*c method
addCustomer(i, name,age, gender, address,contectNo);
in Pro*C method I use following code to insert
EXEC SQL INSERT INTO REGISTRED_CUSTOMER VALUES
(cust_id, cust_name, age, sex, addr, contact);
here cust_name and addr are char *; and sex is char rest as int;
It reports error while using variable but works fine using direct values
like EXEC SQL INSERT INTO REGISTRED_CUSTOMER VALUES (10, 'Pankaj', 23, 'M', 'asdfs', 45875);
I tried changing few lines but in vain.
Thanks in advance.
Your Pro*C code is basically missing the colons (assuming that your formal parameters are called cust_id, cust_name, age etc.):
EXEC SQL INSERT INTO REGISTRED_CUSTOMER VALUES
(:cust_id, :cust_name, :age, :sex, :addr, :contact);
And it would be more robust to explicitly specify the columns name. Otherwise a change to the table schema can result in difficult to find bugs:
EXEC SQL INSERT INTO REGISTRED_CUSTOMER (Cust_Id, Name, Ag, Sex, Addr, Contact)
VALUES (:cust_id, :cust_name, :age, :sex, :addr, :contact);
If im seeing correct you are trying to insert into the columns, the columns??
"EXEC SQL INSERT INTO REGISTRED_CUSTOMER VALUES (cust_id, cust_name, age, sex, addr, contact);"??
it would be more helpful if you post your procedure complete.
Regards
As Mr. mentioned, you are trying to use the columns as input values. When you provide actual values it works. Are you perhaps meaning to use PL/SQL variables or the procedure arguments? In this case, whatever your procedure parameters are called is what you should put in the values section.
i.e if addCustomer looks like
PROCEDURE addCustomer (pId NUMBER, pName VARCHAR2, pAge NUMBER, pGender CHAR, pAddress VARCHAR2, pContact NUMBER)
Then you'd do something like
INSERT INTO registered_customer (cust_id, name, age, sex, addr, contact) VALUES (pId, pName, pAge, pGender, pAddress, pContact);
But if you are inserting into all columns you can leave out the column definition and just provide values
I also got this error message in a stored procedure doing an insert. I misspelled a parameter name in the values clause and the oracle interpreter saw the misspelled name as a column name and issued the 00984.