Creating function in oracle - database

I am trying to create a function that change the day of a football league; if the match is fixed on saturday then the function update the match day to be the previuos friday, and if the match is fixed on sunday the function update the match date to be on monday. Also the function will show how many rows have been update.
The table I use is as follow:
CREATE TABLE "183400_Matches_Details" (
"183400_Stadiums_id" INTEGER NOT NULL,
"183400_Teams_id" INTEGER NOT NULL,
"183400_Teams_id1" INTEGER NOT NULL,
"183400:Referees_id" INTEGER NOT NULL,
"183400_Matches_number" INTEGER NOT NULL,
"date" DATE NOT NULL,
result VARCHAR2(5) NOT NULL
);
I tried the following statements to build the function, but it always gives me an error:
create or replace function updateDay (
v_number "183400_Matches_Details"."183400_Matches_number"%type)
return date
as
v_fecha "183400_Matches_Details"."date"%type;
begin
SELECT TO_CHAR("date", 'DAY', 'NLS_DATE_LANGUAGE=ENGLISH') as day1 into v_fecha FROM
"183400_Matches_Details"
where "183400_Matches_number" = v_number;
if day1 = 'SATURDAY' then
update "183400_Matches_Details"
set "date" = "date"-1
where "183400_Matches_number" = v_number;
elsif day1 = 'SUNDAY' then
update "183400_Matches_Details"
set "date" = "date"+1
where "183400_Matches_number" = v_number;
end if;
return SQL%ROWCOUNT;
end;
/
select * from "183400_Matches_Details"
DECLARE
v_number "183400_Matches_Details"."183400_Matches_number"%type := &number;
v_total_filas number(8);
BEGIN
v_total_filas := actualizaPrecioCoche(v_number);
DBMS_OUTPUT.put_line('There are ' || v_total_filas || ' updated rows');
END;
/
Any ideas to make it run correctly?=)

I changed your function as it should be. Try below.
CREATE TABLE "183400_Matches_Details"
(
"183400_Stadiums_id" INTEGER NOT NULL,
"183400_Teams_id" INTEGER NOT NULL,
"183400_Teams_id1" INTEGER NOT NULL,
"183400:Referees_id" INTEGER NOT NULL,
"183400_Matches_number" INTEGER NOT NULL,
"datee" DATE NOT NULL,
RESULT VARCHAR2 (5) NOT NULL
);
CREATE OR REPLACE FUNCTION updateDay (
v_number "183400_Matches_Details"."183400_Matches_number"%TYPE)
RETURN DATE
AS
v_fecha "183400_Matches_Details"."datee"%TYPE;
sql_qry VARCHAR2 (400 CHAR);
BEGIN
sql_qry :=
'SELECT TO_CHAR(datee, ''DAY'', ''NLS_DATE_LANGUAGE=ENGLISH'') where "183400_Matches_number"='
|| v_number;
EXECUTE IMMEDIATE sql_qry INTO v_fecha;
IF v_fecha = 'SATURDAY'
THEN
UPDATE "183400_Matches_Details"
SET "datee" = "datee" - 1
WHERE "183400_Matches_number" = v_number;
ELSIF v_fecha = 'SUNDAY'
THEN
UPDATE "183400_Matches_Details"
SET "datee" = "datee" + 1
WHERE "183400_Matches_number" = v_number;
END IF;
RETURN to_date('19000101','yyyymmdd') ;
END;
/

After a second look I realized what your asking is actually quite simple: Given a date that is Sat update it to Fri, and that is Sun update to Mon. This can actually be done in a single SQL statement.
I changed it from a function to a procedure as the purpose is to Update the database, and return the number of rows processed. But as a function it makes the purpose to get the row count and updating the database as a side effect. Names and types (IMHO) should always reflect the purpose of the routine. I did 'return' the row count as an OUT parameter - it being an informational side effect. See fiddle for full example.
create or replace
procedure reschedule_sat_sun_match_details(
p_match_number in "183400_Matches_Details"."183400_Matches_number"%type
, p_rows_updated out number)
as
begin
update "183400_Matches_Details"
set "date" = case to_char("date", 'dy')
when 'sat' then "date"-1 -- Sat update to Fri
when 'sun' then "date"+1 -- Sun update to Mon
end
where to_char("date", 'dy') in ('sat','sun')
and "183400_Matches_number" = p_match_number;
p_rows_updated := sql%rowcount;
end reschedule_sat_sun_match_details;
For day of week values I used the format 'dy' rather than 'day'. The difference being 'dy' returns day name abbreviations with a constant length without padding, while 'day' pads the returned values to the length of the longest day name (to get constant length) thus "sunday" is returned as "sunday " to match the length of "wednesday".
A couple other suggestions. Avoid Mixed Case name and names beginning with numbers. These require double quoting (") on every reference. This becomes a pain to just write and your queries much harder to read and understand. a table name Matches_Details_183400 the exact same information without requiring the quotes. (Yes Oracle will make it upper case in messages it it issues but you can still write it in mixed case if you wish - it will still be the same name.) It gives you no benefit but a lot of pain.
As #hotfix mentioned do not use reserved or keywords as object names. Oracle has documented such words and reserves the right to enforce a specific meaning whenever they choose. If/When they do makes an almost untraceable bug to find.

Related

Why Oracle PL/SQL insert into table is not working with cursor?

I'm trying to insert date into temporary table, but it's not saving any data. How should I change cursor date parameters?
The procedure is running without any error message but output xx_cdf_output_utl$.log('inside loop'); is not working.
xx_cdf_output_utl$.log('after loop'); log is printing fine.
Right now added three parameters: p_date_from (date), p_date_to (date), p_line_type_lookup_code (varchar2)
procedure gather_data(
-- p_valid_invoices_count out number,
-- p_invalid_invoices_count out number,
p_date_from in date,
p_date_to in date,
p_line_type_lookup_code in varchar2) is
ROUTINE constant varchar2(65) := PACKAGE_NAME||'.GATHER_DATA';
cursor c_inv(
p_date_from in date,
p_date_to in date,
p_line_type_lookup_code in varchar2) is
select
inv.invoice_id,
inv.invoice_num,
inv.invoice_amount,
inv.invoice_date,
inv.amount_paid,
pas.gross_amount,
pas.payment_num,
ven.vendor_id,
ven.vendor_name
from
ap_invoices_all inv, -- invoice table
ap_payment_schedules_all pas, -- payment schedules table
po_vendors ven -- vendors table
where
inv.invoice_date between p_date_from and p_date_to and
inv.wfapproval_status in (
'NOT REQUIRED',
'WFAPPROVED',
'MANUALLY APPROVED') and
pas.amount_remaining != 0 and
nvl(pas.hold_flag, 'N') != 'Y' and
(p_line_type_lookup_code is not null and
exists(
select
1
from
ap_invoice_distributions_all ind -- distribution table
where
ind.amount != 0 and
ind.line_type_lookup_code = p_line_type_lookup_code and
ind.invoice_id = inv.invoice_id) or
p_line_type_lookup_code is null) and
pas.invoice_id = inv.invoice_id and
ven.vendor_id = inv.vendor_id;
l_date_from date := nvl(to_date(p_date_from, 'yyyy-mm-dd'), to_date('2019-01-01', 'YYYY-MM-DD'));
l_date_to date := nvl(to_date(p_date_to, 'yyyy-mm-dd'), trunc(to_date(sysdate, 'YYYY-MM-DD')));
l_line_type_lookup_code varchar2(240) := p_line_type_lookup_code;
begin
for l_inv_rec in c_inv(
l_date_from,
l_date_to,
l_line_type_lookup_code)
loop
xx_cdf_output_utl$.log('inside loop');
insert into xx_zm_invoice_temp(
invoice_id,
invoice_num,
invoice_amount,
invoice_date,
amount_paid,
gross_amount,
payment_num,
vendor_id,
vendor_name)
-- vendor_amount_total,
-- vendor_amount_valid_flg)
values(
l_inv_rec.invoice_id,
l_inv_rec.invoice_num,
l_inv_rec.invoice_amount,
l_inv_rec.invoice_date,
l_inv_rec.amount_paid,
l_inv_rec.gross_amount,
l_inv_rec.payment_num,
l_inv_rec.vendor_id,
l_inv_rec.vendor_name);
end loop;
xx_cdf_output_utl$.log('after loop');
exception
when xx_cdf_error_utl$.e_internal_exception then
xx_cdf_error_utl$.raise_error;
when others then
xx_cdf_error_utl$.output_unexp_exception(
p_routine => ROUTINE);
end;
When you do:
l_date_from date := nvl(to_date(p_date_from, 'yyyy-mm-dd'), to_date('2019-01-01', 'YYYY-MM-DD'));
l_date_to date := nvl(to_date(p_date_to, 'yyyy-mm-dd'), trunc(to_date(sysdate, 'YYYY-MM-DD')));
the
to_date(p_date_to, 'yyyy-mm-dd')
has to convert p_date from a date to a string first, and it will use the session NLS parameters to do that. As it isn't erroring I imagine your date format has a 2-digit year, possibly the kind-of-default DD-MON-RR. You are effectively doing something like:
to_date(to_char(p_date_to, 'dd-mon-rr'), 'yyyy-mm-dd')
which would translate today's date to 0023-03-20, which isn't what you intended at all. You end up looking for a range of dates in year 0023, or maybe 0020, so it's not surprising you do't find any matching data.
You don't need to convert the parameters to or from strings, you can do:
l_date_from date := nvl(p_date_from, date '2019-01-01');
l_date_to date := nvl(p_date_to, trunc(sysdate));
You can truncate the parameter values if you think that might be necessary, but with a trunc() call, not by bouncing through strings.
You don't really need l_date_from or l_date_to though (or l_line_type_lookup_code); you could do the nvl() firstly in the cursor call:
for l_inv_rec in c_inv(
nvl(p_date_from, date '2019-01-01'),
nvl(p_date_to, trunc(sysdate)),
p_line_type_lookup_code)
Having the same name for the procedure parameters and the cursor parameters might be confusing - though the way you're currently using them, the cursor doesn't really need the parameters - it can refer directly to the procedure values, and use nvl() internally:
inv.invoice_date between nvl(p_date_from, date '2019-01-01') and nvl(p_date_to, trunc(sysdate)) and
I'm always a bit wary of using between with dates because it's easy to forget it's inclusive, and you can accidentally pick up the same data - at exactly midnight - in more than one call. It's a bit safer and clearer to use a range:
inv.invoice_date >= nvl(p_date_from, date '2019-01-01') and
inv.invoice_date < nvl(p_date_to, trunc(sysdate)) and
decided explicitly if the last condition should use < or <=. The latter is usually what you mean if the table values have non-midnight times.
And you could use an implicit cursor, but that's partly a matter of taste...
You could even give your procedure arguments default values, but you might be restricted in how it's called.

SQL: how to do an IF check for data range parameters

I am writing a stored procedure that takes in 4 parameters: confirmation_number, payment_amount, start_range, end_range.
The parameters are optional, so I am doing a check in this fashion for the confirmation_number, and the payment_amount parameters:
IF (#s_Confirmation_Number IS NOT NULL)
SET #SQL = #SQL + ' AND pd.TransactionNumber = #s_Confirmation_Number'
IF (#d_Payment_Amount IS NOT NULL)
SET #SQL = #SQL + ' AND pd.PaymentAmount = #d_Payment_Amount'
I would like to ask for help because I am not sure what is the best method to check for the date range parameters.
If someone could give me en example, or several on how this is best achieved it would be great.
Thank you in advance.
UPDATE - after receiving some great help -.
This is what I have so far, I am following scsimon recommendation, but I am not sure about the dates, I got the idea from another post I found and some playing around with it. Would you care looking at it and tell me what you all think?
Many thanks.
#s_Confirmation_Number NVARCHAR(50) = NULL
, #d_Payment_Amount DECIMAL(18, 2) = NULL
, #d_Start_Range DATE = NULL
, #d_End_Range DATE = NULL
...
....
WHERE
ph.SourceType = #s_Source_Type
AND ((pd.TransConfirmID = #s_Confirmation_Number) OR #s_Confirmation_Number IS NULL)
AND ((pd.PaymentAmount = #d_Payment_Amount) OR #d_Payment_Amount IS NULL)
AND (((NULLIF(#d_Start_Range, '') IS NULL) OR CAST(pd.CreatedDate AS DATE) >= #d_Start_Range)
AND ((NULLIF(#d_End_Range, '') IS NULL) OR CAST(pd.CreatedDate AS DATE) <= #d_End_Range))
(The parameter sourceType is a hard-coded value)
This is called a catch all or kitchen sink query. It is usually written as such:
create procedure myProc
(#Payment_Amount int = null
,#Confirmation_Number = null
,#start_range datetime
,#end_range datetime)
as
select ...
from ...
where
(pd.TransactionNumber = #Confirmation_Number or #Confirmation_Number is null)
and (pd.PaymentAmount = #Payment_Amount or #Payment_Amount is null)
The NULL on the two parameters gives them a default of NULL and makes them "optional". The WHERE clause evaluates this to only return rows where your user input matches the column value, or all rows when no user input was supplied (i.e. parameter IS NULL). You can use this with the date parameters as well. Just pay close attention to your parentheses. They matter a lot here because we are mixing and and or logic.
Aaron Bertrand has blogged extensively on this.
I do it like this
WHERE
COALESCE(#s_Confirmation_Number,pd.TransactionNumber) = pd.TransactionNumber AND
COALESCE(#d_Payment_Amount,pd.PaymentAmount) = pd.PaymentAmount
If we have a value for each of these parameters then it will check against the filter value otherwise it will always match the filter value if the parameter is null.
I've found that using COALESCE is faster and clearer than IF control statements or using OR in the WHERE clause.
There is another way.
But I tested and realized that a scsimon query is faster than mine.
AND (CASE
WHEN #Confirmation_Number is not null
THEN (CASE
WHEN pd.TransactionNumber = #Confirmation_Number
THEN 1
ELSE 0
END)
ELSE 1
END = 1)

invalid input syntax for type timestamp

While running the below code i get an error saying invalid input syntax for type timestamp from admission_datetime.
UPDATE ccsm.stg_demographics_baseline
SET xx_los_days =
(CASE WHEN admission_datetime IS NULL OR
date_trunc('day',admission_datetime) = ''
THEN NULL
WHEN discharge_datetime IS NULL OR
date_trunc('day',discharge_datetime) = ''
THEN date_diff('day', admission_datetime, CURRENT_DATE)
ELSE
date_diff('day', admission_datetime, discharge_datetime)
END);
enter code here
See date_trunc documentation:
The return value is of type timestamp or interval with all fields that are less significant than the selected one set to zero (or one, for day and month).
So you can not compare it with an empty string:
date_trunc('day', admission_datetime) = ''
The invalid input syntax for type timestamp error message concerns the empty string (''), not the admission_datetime column.
Furthermore, there is no date_diff function in PostgreSQL. Just subtract one timestamp from another and you will get an interval result:
SELECT timestamp '2001-09-29 03:00' - timestamp '2001-09-27 12:00'
You'll get
interval '1 day 15:00:00'
If you need the difference in days, try this:
SELECT DATE_PART('day', timestamp '2001-09-29 03:00' - timestamp '2001-09-27 12:00')
The result is 1.
See here for examples of DATEDIFF-like expressions in PostgreSQL.

What code is needed to conditionally provide a value to a field in a beforeinsert trigger?

I need to store a string value in a field in a table, specifically in its Subcategory VarChar(50) column.
The value of Subcategory prior to this post processing is either 0 or 1; I need to change that to a more human-friendly value.
I haven't created a database trigger in decades and need some help with the code. This is my pseudo SQL (a hodgepodge of SQL and VB):
CREATE OR REPLACE TRIGGER tr_CustomerCategoryLog_BeforeInsert
BEFORE INSERT ON CustomerCategoryLog FOR EACH ROW
DECLARE _Category = :new.Category;
DECLARE _Subcategory = :new.Subcategory;
BEGIN
If _Category = "New"
If _Subcategory = 0
:new.Subcategory := 'New';
End If
Else If _Subcategory = 1
:new.Subcategory := 'Assumed';
End If
End If
If _Category = "Existing"
If _Subcategory = 0
:new.Subcategory := 'Existing';
End If
Else If _Subcategory = 1
:new.Subcategory := 'Organic'
End If
End If
Return "Unknown"
End Function
END;
If the logic isn't clear, in semi-plain English it is:
If the value of the Category field is "New", set the Subcategory field value also to "New" if the value of Subcategory is currently 0; otherwise, set it to "Assumed"
If the value of the Category field is "Existing", set the Subcategory field value also to "Existing" if the value of Subcategory is currently 0; otherwise, set it to "Organic"
Maybe I need to give Steely Dan's album "Trigger Logic" a listen.
UPDATE
I think the answer will work, but it's not complete enough for me.
Since I apparently have Oracle code mixed up in the pseudoSQL above, what would the complete code need to look like (to create a BeforeInsert trigger on the CustomerCategoryLog table)?
Is this more like it:
CREATE TRIGGER tr_CustomerCategoryLog_BeforeInsert
ON CustomerCategoryLog
INSTEAD OF INSERT
AS
BEGIN
SELECT
CASE
WHEN #Category = 'New'
THEN CHOOSE(#Subcategory + 1, 'New', 'Assumed')
WHEN #Category = 'Existing'
THEN CHOOSE(#Subcategory + 1, 'Existing', 'Organic')
ELSE 'Unknown'
END
END
?
I tend to avoid triggers (perhaps a character flaw... I also don't like mashed potatoes), but the following illustration could simplify your logic
Declare #Category varchar(50) = 'Existing'
Declare #Subcategory int = 1 -- works if BIT
Select case when #Category = 'New' then choose(#Subcategory+1,'New','Assumed')
when #Category = 'Existing' then choose(#Subcategory+1,'Existing','Organic')
else 'Unknown' end
Returns
Organic

convert varchar array to date array-pl sql

I am trying to convert a a varchar array into a date array. Currently the date is being accessed from a Java class in the form of a String but it needs to be a date there. Therefore I need to convert the varchar array into a date array. I can only see questions here dealing with converting a string to a date format, which is not what I want. How different is it to convert a varchar array into a date array?
this is the variable which I need the procedure to convert.
P_IN_GRID_EFFECTIVE_DATE IN P_DVP_CONSTS.T_VCHAR10ARRAY,
You can loop around one array and use its contents to populate a second one; in this case with a to_date() call for each entry. As a simple demo:
create procedure convert_array(
P_IN_GRID_EFFECTIVE_DATE IN P_DVP_CONSTS.T_VCHAR10ARRAY,
P_OUT_GRID_EFFECTIVE_DATE OUT P_DVP_CONSTS.T_DATEARRAY
) as
begin
P_OUT_GRID_EFFECTIVE_DATE := new P_DVP_CONSTS.T_DATEARRAY();
P_OUT_GRID_EFFECTIVE_DATE.extend(P_IN_GRID_EFFECTIVE_DATE.count);
for i in 1..P_IN_GRID_EFFECTIVE_DATE.count loop
P_OUT_GRID_EFFECTIVE_DATE(i) :=
to_date(P_IN_GRID_EFFECTIVE_DATE(i), 'YYYY-MM-DD');
-- or whatever format your strings are using
end loop;
end convert_array;
/
And a quick test:
set serveroutput on
declare
l_strings P_DVP_CONSTS.T_VCHAR10ARRAY;
l_dates P_DVP_CONSTS.T_DATEARRAY;
begin
l_strings := P_DVP_CONSTS.T_VCHAR10ARRAY('2015-06-08', '2015-07-09',
'2015-08-10');
convert_array(l_strings, l_dates);
dbms_output.put_line('Number of dates in array: ' || l_dates.count);
for i in 1..l_dates.count loop
dbms_output.put_line('Date ' || i
|| ': ' || to_char(l_dates(i), 'DD/MM/YYYY'));
end loop;
end;
/
PL/SQL procedure successfully completed.
Number of dates in array: 3
Date 1: 08/06/2015
Date 2: 09/07/2015
Date 3: 10/08/2015
Types and function:
create type tv as varray(5) of varchar2(15);
create type td as varray(5) of date;
create or replace function v2d (i_varchars in tv)
return td pipelined is
begin
for i in 1..i_varchars.count
loop
pipe row (to_date(i_varchars(i), 'mm/dd/yyyy'));
end loop;
end;
Test:
select * from table(v2d(tv('09/01/2010', '06/15/2015')));
COLUMN_VALUE
------------
2010-01-01
2015-06-15

Resources