Db2 package specification - db2

I've created the following DB2 package specification based on the documentation from IBM.
https://www.ibm.com/docs/en/db2/9.7?topic=plsql-creating-package-specifications
CREATE PACKAGE claims_clm_190_pack
IS
FUNCTION calculate_cfy_offset (
payment_option varchar2
,fy varchar2
,p_from date
,p_to date
,effective_gross_amt number
)
RETURN NUMBER;
FUNCTION get_sum_cfy_offset (
payment_option varchar2
,fy varchar2
,p_from date
,p_to date
,effective_gross_amt NUMBER
)
RETURN NUMBER;
FUNCTION get_consolidated_sum (
payment_option varchar2
,fy varchar2
,p_from date
,p_to date
,effective_gross_amt NUMBER
)
RETURN NUMBER;
FUNCTION get_gross_amount (
payment_option varchar2
,fy varchar2
,p_from date
,p_to date
,effective_gross_amt NUMBER
)
RETURN NUMBER;
END claims_clm_190_pack;
I don't see any error in this code, which is similar to the IBM docs. But when compiled, I got the below error:
[Code: -104, SQL State: 42601] An unexpected token "CREATE PACKAGE claims_clm_190_p" was found following "BEGIN-OF-STATEMENT". Expected tokens may include: "<create_or_replace>".. SQLCODE=-104, SQLSTATE=42601, DRIVER=4.22.29
Any idea, what this means please?. Sorry, I am not a DB2 guy. Any help would be fantastic.
Regards,
R

Related

PostgreSQL - How to handle invalid values when casting to timestamp in SELECT from jsonb data

I am trying to get data from a jsonb column and need to cast an attribute from the payload to timestamp. But the query aborts when the value is null or invalid.
This is my select statement.
Select
(jsonb_path_query(AnchorNode, '$.TestDate')#>> '{}')::timestamp as TestDate
From (
Select
jsonb_path_query_first(payload, '$.node1[*].node2[*]') as AnchorNode
From TestTable
) subq1
The invalid values could be only the Date or null.
How can I change the query to handle this. There are about 7 or 8 date fields where I would need to do this
Thank you
Maybie try to cast text inside a function and return null on exception
create or replace function str_to_timestamp(_date text) returns timestamp
language plpgsql AS
$$
BEGIN
return _date::timestamp;
EXCEPTION WHEN OTHERS THEN
return null::timestamp;
END;
$$;
It was possible to achieve above using CASE statement. It may also be done using COALESCE
Select
case
when jsonb_typeof(AnchorNode -> 'TestDate') is null then null
when jsonb_typeof(AnchorNode -> 'TestDate') = 'string' then (AnchorNode ->> 'TestDate')::timestamp
end TestDateTS,
From (
Select
jsonb_path_query_first(payload, '$.node1[*].node2[*]') as AnchorNode
From TestTable
) subq1
OR
COALESCE((AnchorNode ->> 'TestDate')::timestamp, null) as "TestDate"

PostgreSQL function using dblink, can I use input variables to define table structure - and return the result as a variable?

I am trying to create a function in PostgreSQL that contains a dblink query. The definition of the field that it returns could vary, so I would like to set the field name and data types as input parameters. The error being returned suggests that it isn't referencing the input variables when I name them,
CREATE OR REPLACE FUNCTION validation.dblink_date_check(
username text,
pass text,
srcschemaname text,
srctablename text,
srcdbname text,
datefield text,
datetype text DEFAULT 'date'::text,
srchostname text DEFAULT 'xxx' text,
srcport integer DEFAULT 1234,
OUT max_date timestamp)
RETURNS timestamp
LANGUAGE 'plpgsql'
COST 100
VOLATILE PARALLEL UNSAFE
AS $BODY$
BEGIN
max_date := (
select c.*
from dblink('dbname='||srcdbname||' host='||srchostname||' port='||srcport||' user='||username||' password='||pass||'',
'select
max('||datefield||') from '||srcschemaname||'.'||srctablename||' a')
--this doesn't work
as c(datefield datetype
));
/*--this works
as c(datefield date
));;*/
raise notice '% - (source): %', date_trunc('second', clock_timestamp()::timestamp),max_date;
return;
End;
$BODY$;
When it doesn't work the error I get is,
ERROR: type "datetype" does not exist
LINE 6: as c(datefield datetype
^
Is there a way to structure the syntax to work (as sometimes it might not be a date, it might be a timestamp, or an integer etc)? I have used 'execute' in other functions (to insert), but using it to select the variable 'max_date' doesn't work.

Table name in procedure is not a known variable

I'm writing a procedure in PostgreSQL by following these instruction.
My table is following code
CREATE TABLE public.sinh_vien (
id serial NOT NULL,
ma_sinh_vien numeric NULL,
ten_sinh_vien varchar(50) NULL,
ngay_sinh date NULL
);
Here is my procedure
create or replace
function sinh_vien_merge(datainput json) returns character varying language plpgsql as $function$
begin
merge
into
public.sinh_vien w
using (
values(
select
x.id ,
x.ma_sinh_vien ,
x.ten_sinh_vien ,
to_date(x.ngay_sinh , 'dd/MM/yyyy') ngay_sinh
from
(
select
*
from
json_populate_recordset(null::record, dataInput :: json) as ( id numeric,
ma_sinh_vien numeric,
ten_sinh_vien varchar,
ngay_sinh varchar )) x)) v on
v.id = w.id
when not matched
insert
(ma_sinh_vien,
ten_sinh_vien,
ngay_sinh)
values(v.ma_sinh_vien,
v.ten_sinh_vien,
v.ngay_sinh)
when matched
update
set
ma_sinh_vien = v.ma_sinh_vien;
ten_sinh_vien = v.ten_sinh_vien,
ngay_sinh = v.ngay_sinh;
return '1';
end;
I got an message
SQL Error [42601]: ERROR: "public.sinh_vien" is not a known variable
How do I solve this?
Please notice, that there is no MERGE in PostgreSQL. Your linked documentation is the "PostgreSQL 8.4devel Documentation", but the MERGE command never made it to a productive version.
Thus you cannot use MERGE in your function. You will need to "build" its functionalities with your own INSERT and UPDATE commands.
This issue has been resolved in Postgres 15.

Passing timestamp parameter to function in Postgresql 10.6

I'm building this function:
CREATE OR REPLACE FUNCTION qradar21(cliente_in VARCHAR(50), fecha_inicio timestamp, fecha_fin timestamp) RETURNS TABLE(empresa varchar, fecha timestamp, fuente text, total float) AS $$
BEGIN
RETURN QUERY
SELECT qradar_eventos_detalle.empresa, qradar_eventos_detalle.fecha, eventos->>'fuente' AS fuente, sum((eventos->>'total')::float) AS total FROM public.qradar_eventos_detalle
WHERE qradar_eventos_detalle.empresa = 'cliente_in'
AND qradar_eventos_detalle.fecha BETWEEN 'fecha_inicio' AND 'fecha_fin'
GROUP BY qradar_eventos_detalle.empresa, qradar_eventos_detalle.fecha, qradar_eventos_detalle.eventos
ORDER BY total DESC;
END;
$$ LANGUAGE plpgsql
And calling it with:
SELECT * FROM qradar21('BancoXXX', '2018-12-29 12:00:00', '2019-03-03 07:00:00');
Getting this message:
ERROR: invalid input syntax for type timestamp: «fecha_inicio»
LINE 3: AND qradar_eventos_detalle.fecha BETWEEN 'fecha_inicio' A...
If I change the function to use directly the date (AND qradar_eventos_detalle.fecha BETWEEN '2018-12-29 12:00:00' AND '2018-12-30 07:00:00'), it works great.
I don't know what I'm doing wrong... Does anyone know how to pass this "timestamp without time zone" format to the function?
Thank you in advance
Your function uses string literals, rather then referencing the parameters.
You need to remove the single quotes around your parameter names:
WHERE qradar_eventos_detalle.empresa = cliente_in --<< no quotes here!
AND qradar_eventos_detalle.fecha BETWEEN fecha_inicio AND fecha_fin
^
or here
When you call it, it's also better to use proper timestamp literal:
SELECT *
FROM qradar21('BancoXXX', timestamp '2018-12-29 12:00:00', timestamp '2019-03-03 07:00:00');

update vachar column to date in postgreSQL

Trying to update a column with varchar datatype e.g. '1950-08-14' to a date datatype using
UPDATE tablename SET columnname = to_date(columnname, 'YYYY-MM-DD');
or
ALTER TABLE tablename ALTER COLUMN columnname TYPE DATE USING to_date(columnname, 'YYYY-MM-DD');
but both return the error message
ERROR: invalid value "columnname" for "YYYY"
DETAIL: Value must be an integer.
Referencing http://www.postgresql.org/docs/9.4/static/functions-formatting.html
The query:
ALTER TABLE tablename
ALTER COLUMN columnname TYPE DATE USING to_date(columnname, 'YYYY-MM-DD');
is correct.
The error message means that you have invalid value in column columnname (actualy the invalid value is 'columnname').
All the values in the column must have the format 'YYYY-MM-DD', e.g. '2015-01-01' (or be null).
Solution with a new column.
Create a function to convert varchar to date which returns null for invalid values:
create or replace function varchar_to_date_or_null(str varchar)
returns date language plpgsql as $$
begin
return to_date(str, 'YYYY-MM-DD');
exception
when invalid_datetime_format then return null;
end $$;
Add a new column and update it using the function:
alter table tablename add new_column date;
update tablename
set new_column = varchar_to_date_or_null(columnname);