Oracle DB trigger compiles with warnings. No error available - sql

This is my first db trigger. It compiles with warnings and therefore doesn't work. I've re-read the Oracle docs and searched online but can't work out where I'm going wrong. Any help with my trigger below would gratefully received.
CREATE OR REPLACE TRIGGER oa_mhd_update AFTER
INSERT ON men_mhd FOR EACH row WHEN (new.mhd_tktc LIKE 'OA_A_%'
OR new.mhd_tktc LIKE 'OA_T_%'
OR new.mhd_tktc LIKE 'OA_M_%')
DECLARE seq_var NVARCHAR2 (20);
BEGIN
SELECT (MAX (seq) + 1) into seq_var FROM oa_mhd_data;
INSERT
INTO oa_mhd_data
(
mhd_code,
seq,
mhd_mst1,
mhd_mst2,
mhd_cred,
mhd_cret,
mhd_tsks,
mhd_msgs,
mhd_tktc,
mhd_tref,
mhd_actn,
mhd_eref,
mhd_subj,
mhd_udf1,
mhd_udf2,
mhd_udf3,
mhd_udf4,
mhd_udf5,
mhd_udf6,
mhd_udf7,
mhd_udf8,
mhd_udf9,
mhd_udfa,
mhd_udfb,
mhd_udfc,
mhd_udfd,
mhd_udfe,
mhd_udff,
mhd_udfg,
mhd_udfh,
mhd_udfi,
mhd_udfj,
mhd_udfk,
mhd_updd,
mhd_begd,
mhd_begt,
mhd_endd,
mhd_endt,
mhd_mrcc,
mhd_mhdc,
mhd_mscc,
mhd_pprc,
mhd_ppss,
mhd_inst
)
VALUES
(
:new.mhd_code
seq_var,
:new.mhd_mst1,
:new.mhd_mst2,
:new.mhd_cred,
:new.mhd_cret,
:new.mhd_tsks,
:new.mhd_msgs,
:new.mhd_tktc,
:new.mhd_tref,
:new.mhd_actn,
:new.mhd_eref,
:new.mhd_subj,
:new.mhd_udf1,
:new.mhd_udf2,
:new.mhd_udf3,
:new.mhd_udf4,
:new.mhd_udf5,
:new.mhd_udf6,
:new.mhd_udf7,
:new.mhd_udf8,
:new.mhd_udf9,
:new.mhd_udfa,
:new.mhd_udfb,
:new.mhd_udfc,
:new.mhd_udfd,
:new.mhd_udfe,
:new.mhd_udff,
:new.mhd_udfg,
:new.mhd_udfh,
:new.mhd_udfi,
:new.mhd_udfj,
:new.mhd_udfk,
:new.mhd_updd,
:new.mhd_begd,
:new.mhd_begt,
:new.mhd_endd,
:new.mhd_endt,
:new.mhd_mrcc,
:new.mhd_mhdc,
:new.mhd_mscc,
:new.mhd_pprc,
:new.mhd_ppss,
:new.mhd_inst
)
END;
/

You're missing a comma between the first two elements of the values clause, and a semi-colon at the end of the insert statement:
VALUES
(
:new.mhd_code
seq_var,
:new.mhd_mst1,
...
:new.mhd_ppss,
:new.mhd_inst
)
... should be:
VALUES
(
:new.mhd_code,
seq_var,
:new.mhd_mst1,
...
:new.mhd_ppss,
:new.mhd_inst
);
Odd that you can't see the error though.
Incidentally, the max(seq) + 1 from ... pattern isn't reliable in a multi-user environment. It would be more normal (and safer) to use a proper sequence to generate that value.

Hi there are two syntactical errors
First please add a comma between two values you are inserting
VALUES
(
:new.mhd_,
seq_var,
:new.mhd_mst1,...
and second please add a semi colon at he end of insert statement
...
:new.mhd_pprc,
:new.mhd_ppss,
:new.mhd_inst
);
Hope this will solve your problem

Related

Postgres: on conflict, summing two vectrors(arrays)

I'm trying to handle an array of counters column in Postgres
for example, let's say I have this table
name
counters
Joe
[1,3,1,0]
and now I'm adding 2 values ("Ben", [1,3,1,0]) and ("Joe",[2,0,2,1])
I expect the query to sum between the 2 counters vectors on conflict ([1,3,1,0] + [2,0,2,1] = [3,3,3,1])
the expected result:
name
counters
Joe
[3,3,3,1]
Ben
[1,3,1,0]
I tried this query
insert into test (name, counters)
values ("Joe",[2,0,2,1])
on conflict (name)
do update set
counters = array_agg(unnest(test.counters) + unnest([2,0,2,1]))
but it didn't seem to work, what am I missing?
There are two problems with the expression:
array_agg(unnest(test.counters) + unnest([2,0,2,1]))
there is no + operator for arrays,
you cannot use set-valued expressions as an argument in an aggregate function.
You need to unnest both arrays in a single unnest() call placed in the from clause:
insert into test (name, counters)
values ('Joe', array[2,0,2,1])
on conflict (name) do
update set
counters = (
select array_agg(e1 + e2)
from unnest(test.counters, excluded.counters) as u(e1, e2)
)
Also pay attention to the correct data syntax in values and the use of a special record excluded (find the relevant information in the documentation.)
Test it in db<>fiddle.
Based on your reply to my comments that it will always be four elements in the array and the update is being done by a program of some type, I would suggest something like this:
insert into test (name, counters)
values (:NAME, :COUNTERS)
on conflict (name) do
update set
counters[1] = counters[1] + :COUNTERS[1],
counters[2] = counters[2] + :COUNTERS[2],
counters[3] = counters[3] + :COUNTERS[3],
counters[4] = counters[4] + :COUNTERS[4]

MS Access query produces invalid procedure call

My query in access works fine if i only use the below query with a select statement. As soon as it becomes an append query it produces an "invalid procedure call" error.
I have narrowed down the offending columns as being "Publ" and "PublLong". Both are long text strings. If I remove these two columns the query updates without an error.
Here is a sample data point found in the [Bezeichung] Field:
publications.bank.com/publ-dl-ch/pdf/WhatsUp_20181113_en.pdf
I checked the table that it is being inserted to and the data types are the same nor did i see any other setting that would block the insertion.
How can i get it to work?
INSERT INTO tbl_MatomoRaw ( DownloadDate, IntExt, Publ, PublLong,
PublDate, [Language], Download_Visits, PublMonth )
SELECT
Date() AS DownloadDate,
Left([Bezeichnung],InStr([Bezeichnung],".")-1) AS IntExt,
Nz(Mid([Bezeichnung],InStrRev([Bezeichnung],"/")+1,InStr([Bezeichnung],"_")-
InStrRev([Bezeichnung],"/")-1),"") AS Publ,
Mid([Bezeichnung],InStrRev([Bezeichnung],"/")+1,InStrRev([Bezeichnung],"_")-
InStrRev([Bezeichnung],"/")-1) AS PublLong,
Mid([Bezeichnung],InStr([Bezeichnung],"_")+1,8) AS PublDate,
Mid([Bezeichnung],Len([Bezeichnung])-5,2) AS [Language],
xlsx_Output.[Eindeutige Downloads] AS Download_Visits,
Mid([Bezeichnung],InStr([Bezeichnung],"_")+1,6) AS PublMonth
FROM xlsx_Output
WHERE
(((Nz(Mid([Bezeichnung],InStrRev([Bezeichnung],"/")+1,InStr([Bezeichnung],"_")-
InStrRev([Bezeichnung],"/")-1),"")) Not Like "#Func!"));
#Func! indicates one of your functions is causing an error.
Your query uses multiple functions that run into trouble when your input doesn't meet that format, pre-filter instead of filtering on an error, since you can't filter on an error when appending:
INSERT INTO tbl_MatomoRaw ( DownloadDate, IntExt, Publ, PublLong,
PublDate, [Language], Download_Visits, PublMonth )
SELECT
Date() AS DownloadDate,
Left([Bezeichnung],InStr([Bezeichnung],".")-1) AS IntExt,
Nz(Mid([Bezeichnung],InStrRev([Bezeichnung],"/")+1,InStr([Bezeichnung],"_")-
InStrRev([Bezeichnung],"/")-1),"") AS Publ,
Mid([Bezeichnung],InStrRev([Bezeichnung],"/")+1,InStrRev([Bezeichnung],"_")-
InStrRev([Bezeichnung],"/")-1) AS PublLong,
Mid([Bezeichnung],InStr([Bezeichnung],"_")+1,8) AS PublDate,
Mid([Bezeichnung],Len([Bezeichnung])-5,2) AS [Language],
[Eindeutige Downloads] AS Download_Visits,
Mid([Bezeichnung],InStr([Bezeichnung],"_")+1,6) AS PublMonth
FROM (SELECT * FROM xlsx_Output WHERE Len(Bezeichnung) > 5 AND Bezeichnung LIKE "*?.?*" AND Bezeichnung LIKE "*_????????*" AND Bezeichnung LIKE "*?\?*")
WHERE
(((Nz(Mid([Bezeichnung],InStrRev([Bezeichnung],"/")+1,InStr([Bezeichnung],"_")-
InStrRev([Bezeichnung],"/")-1),"")) Not Like "#Func!"));
Since I don't know exactly where the errors occur, I can't write up a proper filter to identify them, but judging by your query they should include a slash and a symbol after that slash, an underscore and at least 8 symbols after that underscore, and a dot with at least one symbol before and after the dot.

Insert into with CLOB

I have following value:
proc.procSetDebugMode(true);
etl_utils.procSetDebugMode(true);
_analysis.procRunAnalysis(360, <ID_PRCSS_EXCTN>, to_date('<BUSINESS_DATE>', 'YYYY-MM-DD'), '<CONFIG_TYPE>', <G_ROW_COUNT>);
which I need to insert into a specific clob column but unfortunately didn't succeed.
Any tips? The problem seems trivial but still I am unable to resolve it.
insert into JOB
select
1595,
clob(analysis.procSetDebugMode(true); etl_utils.procSetDebugMode(true);analysis.procRunAnalysis(360,<ID_PRCSS_EXCTN>,to_date('<BUSINESS_DATE>','YYYY-MM-DD'), '<CONFIG_TYPE>', <G_ROW_COUNT>);),
INPUT_PARAMS,
UPDATE_STAMP,
UPDAE_USER_LOGIN,
WARNING_TIME,
MAX_EXCTN_TIME,
IS_AWAITING_JOB from JOB where ID = 1585;

SQL Operand Should contain 1 colums PHPMYAdmin

i create this code
INSERT INTO `ordini`(`ora`, `nome`, `indirizzo`, `numero`, `help`,`ID`,`fattorino`,`dreturn`,`flag`)
VALUES ((SELECT `ora`, `nome`, `indirizzo`, `numero`, `help`,`ID` FROM test.dbconsegne WHERE ID = 2),'STRING','CURRENT_TIMESTAMP','0');
and when i try to Execute the query , The PHPMyAdmin push out this error
Operand should contain 1 colum(s)
why? it's not correct
sorry for my bad english
You dont need VALUES, take it out
INSERT INTO `ordini`(`ora`, `nome`, `indirizzo`, `numero`, `help`,`ID`,`fattorino`,`dreturn`,`flag`)
SELECT `ora`, `nome`, `indirizzo`, `numero`, `help`,`ID`,'STRING',CURRENT_TIMESTAMP,'0' FROM test.dbconsegne WHERE ID = 2

converting varchar to md5

I have the following sql code in oracle client 11g. I would like to convert the "ssno" to md5 hash. I've read other posts but none of them specifically say where to put the code. thanks!
SELECT FS_HIRES."rsa",
FS_HIRES."ssno",
FS_HIRES."lname",
FS_HIRES."series",
FS_HIRES."grade",
FS_HIRES."pos_title",
FS_HIRES."ethnicity",
FS_HIRES."disability",
FS_HIRES."type_appt",
FS_HIRES."Perm_Temp",
FS_HIRES."gender",
FS_HIRES."age",
FS_HIRES."age_categories",
FS_HIRES."los",
FS_HIRES."date_apnt",
FS_HIRES."mm_apnt",
FS_HIRES."yy_apnt",
FS_HIRES."apnt_noa",
FS_HIRES."apnt_auth",
FS_HIRES.L2_DESC,
FS_HIRES.L3_DESC,
FS_HIRES.L4_DESC,
FS_HIRES.L5_DESC,
FS_HIRES."fy"
FROM FS_HIRES
The HASH_MD5 constant can't be referred to directly from SQL, so a statement like:
SELECT FS_HIRES."rsa",
DBMS_CRYPTO.HASH(UTL_I18N.STRING_TO_RAW(FS_HIRES."ssno", 'AL32UTF8'),
DBMS_CRYPTO.HASH_MD5),
...
will get an error like "ORA-06553: PLS-221: 'HASH_MD5' is not a procedure or is undefined". You can either use the internal value for that constant, which is 2:
SELECT FS_HIRES."rsa",
DBMS_CRYPTO.HASH(UTL_I18N.STRING_TO_RAW(FS_HIRES."ssno", 'AL32UTF8'), 2),
...
Or if you don't want to rely on a constant that could potentially change in a future release, define your own function:
create or replace function my_md5(p_str varchar2) return raw is
begin
return dbms_crypto.hash(utl_i18n.string_to_raw(p_str, 'AL32UTF8'),
dbms_crypto.hash_md5);
end my_md5;
/
... and then call that:
SELECT FS_HIRES."rsa",
MY_MD5(FS_HIRES."ssno", 'AL32UTF8') AS "ssno",
...
If your database character isn't AL32UTF8, you may need to do more conversion as mentioned in the documentation, and it'll be easier to hide that in the function too.
Try this:
select
'123456789' as ssno,
rawtohex(
DBMS_CRYPTO.Hash (
UTL_I18N.STRING_TO_RAW ('123456789', 'AL32UTF8'),
2)
) as ssno_md5
from dual;
Output:
SSNO SSNO_MD5
123456789 25F9E794323B453885F5181F1B624D0B