plSQL Syntax For Writing Stored Procedure w/ 3 Variables - oracle

I have this long block of SQL statements I need to make a stored procedure for that accept 3 variables. The variables are invoice, NewDate, TransactionDate and the datatypes are integer, date, and date respectively. I've tried to create the stored procedure, but the syntax is messing me up. I'll be calling the stored procedure using Coldfusion and cfstoredproc, hence the pound signs. Not that I need any help with the ColdFusion. Just trying to provide as much info as possible.
truncate table fix_the_date; commit;
insert into fix_the_date
(Location,Invoice,NewDate,TransactionDate)
values
('Corporate', '#invoice#'
, to_date('#NewDate#', 'mm/dd/yyyy')
,to_date('#TransactionDate#','mm/dd/yyyy'));
commit;
<!--- About a dozen other queries go here that I won't waste your time with--->
Update 1:
Below is what I have for the Stored Procedure as of now. I'm getting the following error:
Error Executing Database Query. [Macromedia][Oracle JDBC
Driver][Oracle]ORA-01858: a non-numeric character was found where a
numeric was expected ORA-06512: at "THEDB.FIXMISSINGDATE", line 13
ORA-06512: at line 1 The error occurred on line 4
CREATE OR REPLACE PROCEDURE THEDB."FIXMISSINGDATE" (Invoice integer, NewTransactionDate date, TransactionDate date) IS
BEGIN
EXECUTE IMMEDIATE 'TRUNCATE TABLE FixTheDate;
commit;
EXECUTE IMMEDIATE 'TRUNCATE TABLE DeletePayTrans';
commit;
insert into FixTheDate(Site, Invoice_No, Pay_Date, Old_Date)
values ('Corporate', 'TO_CHAR(#Invoice#)','#NewTransactionDate#','#TransactionDate#');
commit;
/*******************************
Plus a bunch of other queries here
*******************************/
END FIXMISSINGDATE;
/
Update 2: Per feedback in comments, removed pound signs
CREATE OR REPLACE PROCEDURE THEDB."FIXMISSINGDATE" (Invoice integer, NewTransactionDate date, TransactionDate date) IS
BEGIN
EXECUTE IMMEDIATE 'TRUNCATE TABLE FixTheDate;
commit;
EXECUTE IMMEDIATE 'TRUNCATE TABLE DeletePayTrans';
commit;
insert into FixTheDate(Site, Invoice_No, Pay_Date, Old_Date)
values ('Corporate', TO_CHAR(Invoice), NewTransactionDate, TransactionDate);
commit;
/*******************************
Plus a bunch of other queries here
*******************************/
END FIXMISSINGDATE;
/

Your issue probably relates to trying to issue DDL statements in a stored procedure (see here).
I'm going to assume that your NewDate and TransactionDate are also varchar2 (since your insert statement won't work if they're actually dates).
CREATE OR REPLACE PROCEDURE my_proc(
invoice IN integer,
NewDate IN varchar2,
TransactionDate IN varchar2) AS
BEGIN
EXECUTE IMMEDIATE 'TRUNCATE TABLE fix_the_date';
INSERT INTO fix_the_date (Location,Invoice,NewDate,TransactionDate)
VALUES ('Corporate', invoice, TO_DATE(NewDate, 'mm/dd/yyyy'),
TO_DATE(TransactionDate,'mm/dd/yyyy'));
COMMIT;
END;
/
...or if you really are passing dates:
CREATE OR REPLACE PROCEDURE my_proc(
invoice IN integer,
NewDate IN date,
TransactionDate IN date) AS
BEGIN
EXECUTE IMMEDIATE 'TRUNCATE TABLE fix_the_date';
INSERT INTO fix_the_date (Location,Invoice,NewDate,TransactionDate)
VALUES ('Corporate', invoice, NewDate, TransactionDate);
COMMIT;
END;
/

Related

how to execute delete statement inside plsql block and call it in a procedure

I have written below pl sql block and trying to create a procedue. But i am getting warnings and not able to execute the procedue.
Please suggest if something i am missing \
Please let me know if this question is duplicate as i am not able to get the exact link to refer
create or replace PROCEDURE EmployeeProc
IS
BEGIN
delete from Employeetable where EmplId in (
select EmployeeId FROM EmployeeMstrTbl where JoiningDate between to_date('2019-01-01','YYYY-MM-DD') and to_date('2019-02-28','YYYY-MM-DD'));
commit;
DBMS_OUTPUT.PUT_LINE('Deleted '||SQL%ROWCOUNT ||' records from Employeetable');
END;
Error: Object Invalid
Try using cursor
CREATE OR REPLACE PROCEDURE EMPLOYEEPROC IS
CURSOR C1 IS
SELECT EMPLOYEEID
FROM EMPLOYEEMSTRTBL
WHERE JOININGDATE BETWEEN TO_DATE('2019-01-01','YYYY-MM-DD') AND TO_DATE('2019-02-28','YYYY-MM-DD'));
BEGIN
FOR I IN C1 LOOP
DELETE FROM EMPLOYEETABLE
WHERE EMPLID=I.EMPLOYEEID;
END LOOP;
COMMIT;
DBMS_OUTPUT.PUT_LINE('DELETED '||SQL%ROWCOUNT ||' RECORDS FROM EMPLOYEETABLE');
END;
Your code works just fine.
CREATE TABLE Employeetable
(
EmplId NUMBER
);
CREATE TABLE EmployeeMstrTbl
(
EmployeeId NUMBER,
JoiningDate DATE
);
CREATE OR REPLACE PROCEDURE EmployeeProc
IS
BEGIN
DELETE FROM Employeetable
WHERE EmplId IN
(SELECT EmployeeId
FROM EmployeeMstrTbl
WHERE JoiningDate BETWEEN TO_DATE ('2019-01-01',
'YYYY-MM-DD')
AND TO_DATE ('2019-02-28',
'YYYY-MM-DD'));
COMMIT;
DBMS_OUTPUT.PUT_LINE (
'Deleted ' || SQL%ROWCOUNT || ' records from Employeetable');
END;
EXEC EmployeeProc;
DROP TABLE Employeetable;
DROP TABLE EmployeeMstrTbl;
DROP PROCEDURE EmployeeProc;
Script output:
Table created.
Table created.
Procedure created.
PL/SQL procedure successfully completed.
Table dropped.
Table dropped.
Procedure dropped.
DBMS Output:
Deleted 0 records from Employeetable
Maybe you have a typo in a table name, column name or something similar.
I suggest that you try to execute your delete statement first to check if it works.
Not sure if perhaps you mistyped something, or if it is to do with how you have it set up. But even if it is a mistype and it could work, it's not nice, so do a loop.
i = 0;
FOR r in (select * FROM EmployeeMstrTbl where JoiningDate between to_date('2019-01-01','YYYY-MM-DD') and to_date('2019-02-28','YYYY-MM-DD'))
LOOP
DELETE FROM Employeetable where EmplId = r.EmployeeId;
END LOOP;
DBMS_OUTPUT.PUT_LINE('Deleted '|| i ||' records from Employeetable');
Because this will work, and more importantly, its easier to understand. Keeping code short and abbreviated has become much less important nowadays since the size of the code is almost never the problem, but keeping it easy to understand is extremely important so that it can be maintained in the future.

ORACLE PL/SQL: Calling stored procedure function with multiple parameters (DML query)

New PL/SQL person here. I have a (successfully compiled) PL/SQL function block that manipulates a table in my database by adding a new term to it:
create or replace FUNCTION add_new_term
(TERM_ID_IN IN NUMBER, TERM_IN IN VARCHAR2, IS_METATERM_IN IN NUMBER)
RETURN VARCHAR2
IS
add_term CV_TERMS.TERM_NAME%TYPE; --TERM_NAME is VARCHAR2 type
BEGIN
INSERT INTO CV_TERMS (TERM_ID, TERM_NAME, IS_METATERM)
VALUES (TERM_ID_IN, TERM_IN, IS_METATERM_IN);
dbms_output.put_line('New term successfully added to CV_TERMS table: ' || TERM_IN);
RETURN add_term;
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
raise_application_error (-20001, 'You have tried to insert a duplicate term.');
WHEN OTHERS THEN
raise_application_error (-20002, 'An error has occurred inserting a term - '|| SQLCODE ||' -ERROR- '|| SQLERRM);
END add_new_term;
I call this function like calling a stored procedure:
DECLARE
add_term_success cv_terms.term_name%type;
BEGIN
add_term_success := add_new_term(cv_terms_pk.NEXTVAL, 'TESTTT', 0);
END;
SQLDeveloper tells me the procedure was successfully completed, however, the term has not been added to the table. I created the sequence cv_terms_pk independently (it's not in the table CV_TERMS' SQL). Does it need to be there? Am I passing it improperly? Or is something wrong with my add_term declaration? Ideas?
After the DML INSERT you have to commit the transaction.
create or replace FUNCTION add_new_term
(TERM_ID_IN IN NUMBER, TERM_IN IN VARCHAR2, IS_METATERM_IN IN NUMBER)
RETURN VARCHAR2 IS
add_term CV_TERMS.TERM_NAME%TYPE; --TERM_NAME is VARCHAR2 type
BEGIN
INSERT INTO CV_TERMS(TERM_ID, TERM_NAME, IS_METATERM
VALUES (TERM_ID_IN, TERM_IN, IS_METATERM_IN);
COMMIT; ---LINE ADDED ...

FORALL+ EXECUTE IMMEDIATE + INSERT Into tbl SELECT

I have got stuck in below and getting syntax error - Please help.
Basically I am using a collection to store few department ids and then would like to use these department ids as a filter condition while inserting data into emp table in FORALL statement.
Below is sample code:
while compiling this code i am getting error, my requirement is to use INSERT INTO table select * from table and cannot avoid it so please suggest.
create or replace Procedure abc(dblink VARCHAR2)
CURSOR dept_id is select dept_ids from dept;
TYPE nt_dept_detail IS TABLE OF VARCHAR2(25);
l_dept_array nt_dept_detail;
Begin
OPEN dept_id;
FETCH dept_id BULK COLLECT INTO l_dept_array;
IF l_dept_array.COUNT() > 0 THEN
FORALL i IN 1..l_dept_array.COUNT SAVE EXCEPTIONS
EXECUTE IMMEDIATE 'INSERT INTO stg_emp SELECT
Dept,''DEPT_10'' FROM dept_emp'||dblink||' WHERE
dept_id = '||l_dept_array(i)||'';
COMMIT;
END IF;
CLOSE dept_id;
end abc;
Why are you bothering to use cursors, arrays etc in the first place? Why can't you just do a simple insert as select?
Problems with your procedure as listed above:
You don't declare procedures like Procedure abc () - for a standalone procedure, you would do create or replace procedure abc as, or in a package: procedure abc is
You reference a variable called "dblink" that isn't declared anywhere.
You didn't put end abc; at the end of your procedure (I hope that was just a mis-c&p?)
You're effectively doing a simple insert as select, but you're way over-complicating it, plus you're making your code less performant.
You've not listed the column names that you're trying to insert into; if stg_emp has more than two columns or ends up having columns added, your code is going to fail.
Assuming your dblink name isn't known until runtime, then here's something that would do what you're after:
create Procedure abc (dblink in varchar2)
is
begin
execute immediate 'insert into stg_emp select dept, ''DEPT_10'' from dept_emp#'||dblink||
' where dept_id in (select dept_ids from dept)';
commit;
end abc;
/
If, however, you do know the dblink name, then you'd just get rid of the execute immediate and do:
create Procedure abc (dblink in varchar2)
is
begin
insert into stg_emp -- best to list the column names you're inserting into here
select dept, 'DEPT_10'
from dept_emp#dblink
where dept_id in (select dept_ids from dept);
commit;
end abc;
/
There appears te be a lot wrong with this code.
1) why the execute immediate? Is there any explicit requirement for that? No, than don't use it
2) where is the dblink variable declared?
3) as Boneist already stated, why not a simple subselect in the insert statement?
INSERT INTO stg_emp SELECT
Dept,'DEPT_10' FROM dept_emp#dblink WHERE
dept_id in (select dept_ids from dept );
For one, it would make the code actually readable ;)

how to use date in where clause while inserting values into table using PLSQL store procedure

This is the code i used in stored procedure;
CREATE OR REPLACE PROCEDURE MY_STORE_PROCEDURE (new_date in date)
IS
BEGIN
execute immediate 'INSERT INTO TEMP_1 ( ID CHAR(10),
A_CNT NUMBER,
JOIN_DT DATE,
)
SELECT
L1.ID,
L1.A_CNT,
L1.JOIN_DT,
FROM ACTVY_1 L1
WHERE L1.JOIN_DT = new_date';
END;
===========================================================
Below is the code i used to call store procedure with passing value. value is date which store procedure reciece and used to pull date from a table. but it is giving me error.
DECLARE
a_date DATE;
BEGIN
a_date :=to_DATE ('01-NOV-2013', 'DD-MON-YYYY');
MY_STORE_PROCEDURE(a_date);
END;
Please suggest is there any syntax error or what is issue.
Based on your example, there is no reason to use dynamic SQL. You also have a bunch of errors. Try this:
CREATE OR REPLACE PROCEDURE MY_STORE_PROCEDURE (new_date IN DATE)
IS
BEGIN
INSERT INTO TEMP_1 (ID, A_CNT, JOIN_DT)
SELECT L1.ID, L1.A_CNT, L1.JOIN_DT
FROM ACTVY_1 L1
WHERE L1.JOIN_DT = new_date;
END;

SQL Fiddle Output Error

Actually I am quite new to PL/SQL
I created the following table using oracle pl/sql in SQL Fiddle
create table Employee(name varchar2(100),id integer, salary integer,PRIMARY KEY(id));
insert into Employee(name,id,salary) values('sa',94,100);
insert into Employee(name,id,salary) values('pr',88,150);
insert into Employee(name,id,salary) values('ji',33,900);
insert into Employee(name,id,salary) values('na',24,880);
insert into Employee(name,id,salary) values('po',65,770);
insert into Employee(name,id,salary) values('ri',69,910);
insert into Employee(name,id,salary) values('uj',12,650);
insert into Employee(name,id,salary) values('ad',43,440);
insert into Employee(name,id,salary) values('sam',40,550);
I executed the following query
DECLARE
employee_record Employee%ROWTYPE;
BEGIN
select * into employee_record from Employee where id>90;
dbms_output.put_line(employee_record.name||' '||employee_record.id||' '||employee_record.salary);
END;
/
I am getting the following output
Record Count: 0; Execution Time: 2ms
It should print the values present in the employee record, right? Is there something wrong in my sql query or some problem with sql fiddle not able to display dbms_output?
You need to emulate dbms_output.put_line :)
Schema:
create table Employee(
name varchar2(100),
id integer,
salary integer,
PRIMARY KEY(id)
);
insert into Employee(name,id,salary) values('sa',94,100);
insert into Employee(name,id,salary) values('pr',88,150);
insert into Employee(name,id,salary) values('ji',33,900);
insert into Employee(name,id,salary) values('na',24,880);
insert into Employee(name,id,salary) values('po',65,770);
insert into Employee(name,id,salary) values('ri',69,910);
insert into Employee(name,id,salary) values('uj',12,650);
insert into Employee(name,id,salary) values('ad',43,440);
insert into Employee(name,id,salary) values('sam',40,550);
create table dbmsoutput (
pos int,
mes varchar2(4000)
);
SQL:
DECLARE
employee_record Employee%ROWTYPE;
procedure put_line(p_mes in varchar2) is
v_pos int;
begin
select count(0) into v_pos from dbmsoutput;
insert into dbmsoutput (pos, mes) values (v_pos, p_mes);
end;
BEGIN
put_line('Hello! This code is powered by dbms_output emulator :)');
-- Your code here:
select * into employee_record from Employee where id>90;
put_line(employee_record.name||' '||employee_record.id||' '||employee_record.salary);
--
put_line('Bye!');
END;
/
SELECT mes FROM dbmsoutput order by pos
fiddle
Just as a curiosity really, you can get limited dbms_output results from SQL Fiddle, but you need a function to extract the buffered lines and return them in a form you can select. This uses a pipelined table:
create type t_lines as table of varchar2(4000)
/
create or replace function get_lines
return t_lines pipelined is
lines dbms_output.chararr;
numlines integer;
begin
numlines := 999;
dbms_output.get_lines(lines, numlines);
if numlines > 0 then
for i in 1..numlines loop
pipe row (lines(i));
end loop;
else
pipe row ('No data');
end if;
end;
/
And then, after whatever you have issuing dbms_output.put_line calls:
select * from table(get_lines);
Demo. And see the dbms_output documentation to see what its get_lines procedure does and how it relates to your put_lines calls.
But just because you can do something, doesn't mean you necessarily should. This is awkward and doesn't scale, but neither does trying to learn PL/SQL through SQL Fiddle really.
I'd second Ben's recommendation to get your own database to play with, but I'd suggest you look at a pre-built VM image you can run in VirtualBox, which saves you a lot of time in the setup - you don't have to worry about how to install the Oracle software or create and configure a database, it's just ready to use, and you can throw it away or easily start again if things go wrong.

Resources