Below is the sample procedure code,
create or replace procedure pro_test(start_date date, end_date date)
is
begin
insert into test1 select col1, col2, col3 from main where range_date between start_date and end_date;
exception
< what are the exception I need to capture >
end;
/
Q1 : Is this right way to pass date directly inside the procedure? Q2 : If not, can I pass varchar inside the procedure , to convert the date in declaration part? Q3 : In begin part am using between operator, can i pass procedure parameter directly ?
While executing this procedure, exec pro_test('10102015','30102015');
What i need to mention in between sentence? between start_date and end_date is this enough or i need to mask date format?
can someone help me to clear on this?
Q1 : Is this right way to pass date directly inside the procedure?
Yes.
Q3 : In begin part am using between operator, can i pass procedure parameter directly ?
Not sure what you mean by this, but your insert statement is fine. You are passing a DATE as parameter and inserting into the table.
In my opinion, all this could be done in a single INSERT..SELECT statement in pure SQL .
insert into test1
select col1, col2, col3
from main
where range_date
between TO_DATE(<date_literal>,<format mask>)
and TO_DATE(<date_literal>,<format mask>);
UPDATE Per OP's comment:
While executing this procedure, exec pro_test('10102015','30102015'); What i need to mention in between sentence? between start_date and end_date is this enough or i need to mask date format?
'10102015'
is not a DATE, it is a string literal. You must pass it as DATE, so you must either use TO_DATE with proper format mask or ANSI Date literal as you do not have any time portion. ANSI Date literal uses a fixed format 'YYYY-MM-DD'
.
For example,
Using TO_DATE :
EXEC pro_test(TO_DATE('10102015','DDMMYYYY'),TO_DATE('30102015','DDMMYYYY'));
Using ANSI Date literal :
EXEC pro_test(DATE '2015-10-10', DATE '2015-10-30');
Q1. You need say procedure what type of parameters input or output (adding in or out ), for your procedure it is input:
create or replace procedure pro_test(start_date in date, end_date in date)
Everything else in your procedure fine.
If you want to be extra cautious, you should namespace your pl/sql variables when they are used in a SQL statement:
create or replace procedure pro_test(start_date date, end_date date)
is
begin
insert into test1
select col1, col2, col3
from main where range_date
between pro_test.start_date and pro_test.end_date;
...
This prevents the SQL engine "capturing" the variables if their name is the same as a column in a referenced table.
Commonly you see people try to avoid this with:
create procedure my_procedure (p_customer_id number)
...
where customer_id = p_customer_id
Namespacing the variables allows you to keep a cleaner interface without the prefixing.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.