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

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;

Related

how to add audit table in package to capture timing in oracle

creating a package
inside package create 2 procedure and 2 function
now add audit to capture start timing and end timing for each procedure and function in
audit table
TBL_AUDIT_LOG(process_name,start_time,end_time,created_by,created_dttm)
Although you can create a table within a package (actually, its procedure) using dynamic SQL (see execute immediate), you shouldn't do that. That's bad practice. Just create the table at SQL level as
create table TBL_AUDIT_LOG
(process_name varchar2(60),
start_time date,
end_time date,
created_by varchar2(30),
created_dttm date);
and use it from within the package. I'd suggest you NOT to insert directly into the table from every program unit in the package - create yet another, logging procedure which will accept certain parameters and perform logging itself, possibly as an autonomous transaction. Something like this:
procedure p_log (par_proc_name in varchar2, par_start in date, par_end in date) as
begin
insert into tbl_audit_log (process_name, start_time, end_time, created_by, created_dttm)
values (par_proc_name, par_start, par_end, user, sysdate);
commit;
end;
I would create a procedure for logging :
PROCEDURE INSERT_LOG(P_FROM IN VARCHAR2,
P_START_DATE IN DATE,
P_END_DATE IN DATE) IS
BEGIN
INSERT INTO TBL_AUDIT_LOG VALUES (P_FROM , P_START_DATE, P_START_DATE,USER, NULL);
COMMIT;
END;
Not: i am not sure what is created_dttm - so i passed null.
And inside your procedures, you can use it like this :
PROCEDURE TEST_P(P_VAR IN NUMBER) IS
C_LOG_START DATE;
C_LOG_END DATE;
BEGIN
C_LOG_START := SYSDATE ;
/*
YOUR PROC LOGÄ°C
*/
C_LOG_END := SYSDATE ;
-- CALL LOG ROC HERE
INSERT_LOG('TEST_P',C_LOG_START,C_LOG_END,NULL);
END;
its smiliar with functions, but you have to declare your functions like this :
FUNCTION GET_CUSTOMER(REFNO VARCHAR2) RETURN TBL_CUSTOMER_PROFILE IS
PRAGMA AUTONOMOUS_TRANSACTION;
IS_NODATA BOOLEAN;
LORESULT TBL_CUSTOMER_PROFILE;
BEGIN
/*function logic*/
END;
Check here , it is very important :

Table name, column names as argument to stored procedure

I am a newbie to stored procedure and to PL/SQL. There is an existing procedure to copy data from one table to another. I want to rewrite the stored procedure to accept table name and column names as arguments.Did googling on the solution but couldn't come up with a solid solution.
Also planning to add column names as argument so that the column names don't have to be repeatedly added in multiple stored procedures which uses the same tables and columns, helps to reduce maintenance when columns names gets added/removed. Code has been added.
Can anyone help me with this? Any sample code will be very helpful.
create or replace procedure copy_data(startDate DATE, endDate DATE,
mainTable varchar2, subTable varchar2, cpyTbl varchar2)
IS
commit_size NUMBER :=1000;
existing_columns NUMBER;
after_deletion_columns NUMBER;
removed_columns NUMBER;
TYPE order_ids IS TABLE OF subTable.id%TYPE INDEX BY PLS_INTEGER;
removable_order_ids order_ids;
bulk_errors EXCEPTION;
PRAGMA EXCEPTION_INIT (bulk_errors, -24381);
CURSOR C1 is select id FROM subTable where ord_id in (select ord_id from
mainTable where tmstmp BETWEEN startDate AND endDate);
BEGIN
open C1;
LOOP
FETCH C1 BULK COLLECT INTO removable_order_ids LIMIT commit_size;
forall indx in 1..removable_order_ids.COUNT
INSERT INTO cpyTbl (id, ord_id, name, phon_nbr)
select id, ord_id, name, phon_nbr from subTable
where ord_id = removable_order_ids(indx) LOG ERRORS INTO
ERR$_cpyTbl('INSERT') REJECT LIMIT UNLIMITED;
COMMIT;
EXIT WHEN removable_order_ids.COUNT < commit_size;
END LOOP;
COMMIT;
end;

To get data from Stored Procedure having without parameter

I have a stored procedure proc1 without parameters. I want to extract data from this stored procedure. How can I get that? Could you help me?
Stored procedure:
create procedure proc1
as
begin
select e_id, e_nm, e_sal
from emp
where e_id like 'e%';
end proc1;
You can do this in Oracle 12.1 or above:
create or replace procedure demo
as
rc sys_refcursor;
begin
open rc for select * from dual;
dbms_sql.return_result(rc);
end demo;
This requires an Oracle 12.1 or later client/driver to handle the implicit result set.
For more details, see Implicit Result Sets in the Oracle 12.1 New Features Guide, Tom Kyte's Blog, Oracle Base etc.
Here's one possible solution:
Declaration:
create procedure proc1 (emp_row IN OUT emp%rowtype)
as
begin
select * --e_id, e_nm, e_sal
into emp_row
from emp
where e_id like 'e%';
end proc1;
Use case:
DECLARE
l_emp_row emp%rowtype;
BEGIN
proc1(l_emp_row);
-- Here you can access every column of table "emp", like so:
-- dbms_output.put_line('e_id: ' || to_char(l_emp_row.e_id));
-- dbms_output.put_line('e_nm: ' || to_char(l_emp_row.e_nm));
-- dbms_output.put_line('e_sal: ' || to_char(l_emp_row.e_sal));
END;
Is there anything special that you need to get out of the Procedure?
Cheers
you create a view this query. (recomended)
Oracle procedure is not return any data. So, you do not see any result. Your result get buffer but not print screen. if You need a procedure, insert all data another table.
create procedure proc1
as
begin
insert into new_table select e_id, e_nm, e_sal from emp where e_id like 'e%';
end proc1;
Another way; You create a function. Because function outputs and inputs. This function;
for example Create an Oracle function that returns a table

plSQL Syntax For Writing Stored Procedure w/ 3 Variables

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;
/

Declare Table Variable in Oracle Procedure

I'm having a heck of a time trying to find an example of this being done. I have a procedure, and as part of that procedure I want to store the results of a SELECT statement so that I can work against that set, and then use it as a reference to update the original records when it's all done.
The difficulty I'm having is in declaring the temporary table variable. Here's an example of what I'm trying to do:
PROCEDURE my_procedure
IS
output_text clob;
temp_table IS TABLE OF MY_TABLE%ROWTYPE; -- Error on this line
BEGIN
SELECT * BULK COLLECT INTO temp_table FROM MY_TABLE WHERE SOME_DATE IS NULL;
-- Correlate results into the clob for sending to email (working)
-- Set the SOME_DATE value of the original record set where record is in temp_table
I get an error on the second occurrence of IS, saying that it is an unexpected symbol. This suggests to me that my table variable declaration is either wrong, or in the wrong place. I've tried putting it into a DECLARE block after BEGIN, but I just get another error.
Where should this declaration go? Alternatively, if there is a better solution I'll take that too!
CREATE OR REPLACE PROCEDURE PROCEDURE1 AS
output_text clob;
type temp_table_type IS TABLE OF MY_TABLE%ROWTYPE;
temp_table temp_table_type;
BEGIN
SELECT * BULK COLLECT INTO temp_table FROM MY_TABLE;
END PROCEDURE1;
or
CREATE OR REPLACE PROCEDURE PROCEDURE1 ( output_text OUT clob ) IS
type temp_table_type IS TABLE OF MY_TABLE%ROWTYPE
INDEX BY BINARY_INTEGER;
temp_table temp_table_type;
BEGIN
SELECT * BULK COLLECT INTO temp_table FROM MY_TABLE;
FOR indx IN 1 .. temp_table.COUNT
LOOP
something := temp_table(indx).col_name;
END LOOP;
END PROCEDURE1;
I had a similiar problem and found this:
Selecting Values from Oracle Table Variable / Array?
The global temporary table can be used like a regular table, but its content is only temporary (deleted at end of session/transaction) and each session has its own table content.
If you don't need dynamic SQL this can be used as good solution:
CREATE GLOBAL TEMPORARY TABLE temp_table
(
column1 NUMBER,
column2 NUMBER
)
ON COMMIT DELETE ROWS;
PROCEDURE my_procedure
IS
output_text clob;
BEGIN
-- Clear temporary table for this session (to be sure)
DELETE FROM temp_table;
-- Insert data into temporary table (only for this session)
INSERT INTO temp_table SELECT * FROM MY_TABLE WHERE SOME_DATE IS NULL;
-- ...
END;
The only disadvantages are, in my opinion, that you got another table and that the temporary table is not dynamic.

Resources