How to create procedure in PostgreSQL - postgresql

I'm trying to convert Oracle to PostgreSQL. I'm getting error like ERROR: syntax error at or near "UPDATE_USER_PWD_INVALID1" while creating procedure
CALL PROCEDURE UPDATE_USER_PWD_INVALID1 IS
-- PRAGMA AUTONOMOUS_TRANSACTION;
V_INVALID_COUNT USERSMPININVALID.INVALID_COUNT%TYPE;

Related

Is it possible to call a function/procedure in postgres without braces?

I am working on DB migration from Oracle to Postgres and came across a situation where a legacy Java code is calling a stored procedure from Oracle. It is a simple call without parentheses like this : Mgr.AdmSP.myproc = ad.ctlpk.proc
where ad is schema name, ctlpk is Package name and proc is Stored Procedure in Oracle
we have migrated procedure proc to Postgres but when it comes to calling, Postgres expects () for procedure or function calls even if it does not take any parameters.
So we tried to run it like this : Mgr.AdmSP.myproc = ad_ctlpk.proc(). Here ad_ctlpk is postgres schema name. After executing we are getting error : syntax error at or near "(".
Since this is a database migration only, we are not expected to make any changes to other components except database objects. Is there a way to mimic the Oracle like procedure calling style in Postgres?
Like Postgres have function now() and current_timestamp which perform same functionality but with different syntax. I am using Postgres Ver 11.11.
Adding some details as per comments for more details. In Oracle, it is defined as FUNCTION Proc RETURN INT and as of now we converted it into Postgres function as
CREATE FUNCTION ad_ctlpk.proc() RETURNS INTEGER
I am not sure about the Framework and other java code details since I dont have access to Java code. We have access to executables only, which when executed gives below error
[[AdmThread 1] [WARN:AbstractThreadedPublisherManager$ExceptionHandlingThreadGroup:48] Error in thread AdmThread 1 : Problem in doWork
Caused by: Problem running proc procedures
Caused by: ERROR: syntax error at or near "("
Position: 38, State: 42601, ErrorCode: 0
com.ubs.datait.common.distribution.exception.DistributionRuntimeException: Problem in doWork
at common.distribution.manager.AdmThread.doWork(AdmThread.java:72)
at common.distribution.common.AbstractDistributionThread.run(AbstractDistributionThread.java:30)
Caused by: common.distribution.exception.DistributionRuntimeException: Problem running proc procedures
at common.distribution.manager.AdmThread.runAdminProc(AdmThread.java:100)
at common.distribution.manager.AdmThread.doWork(AdmThread.java:67)
... 1 more
Caused by: org.postgresql.util.PSQLException: ERROR: syntax error at or near "("
Position: 38
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2552)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2284)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:322)
at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:481)
Update: :
Since Postgres does not support creating/calling functions without braces (), we finally had to ask for changes in java code to add () to the function calls where required.

DB2 encrypt field in select statement

So I need to do a select statement, while encrypting a field in SHA2_512. I'm not sure which DB2 version it is, but I tried both queries and neither work.
When trying this query:
SELECT HEX(HASH_SHA256('ABCDEFGHIJKLMNOPQRZTUVWXYZ'))
FROM SYSIBM.SYSDUMMY1;
I get the following error:
Error: DB2 SQL Error: SQLCODE=-440, SQLSTATE=42884,
SQLERRMC=FUNCTION;HASH_SHA256, DRIVER=3.71.22 SQLState: 42884
ErrorCode: -440 Error: DB2 SQL Error: SQLCODE=-514, SQLSTATE=26501,
SQLERRMC=SQL_CURLH200C1, DRIVER=3.71.22 SQLState: 26501 ErrorCode:
-514
While with this query
SELECT hash('Charlie at IBM', 3)
FROM SYSIBM.SYSDUMMYU;
Error: DB2 SQL Error: SQLCODE=-440, SQLSTATE=42884,
SQLERRMC=FUNCTION;HASH, DRIVER=3.71.22 SQLState: 42884 ErrorCode:
-440 Error: DB2 SQL Error: SQLCODE=-514, SQLSTATE=26501, SQLERRMC=SQL_CURLH200C1, DRIVER=3.71.22 SQLState: 26501 ErrorCode:
-514
(while without the hash functions the query runs without any issues). Anyone know how I can fix this issue?
I'm not sure which DB2 version I'm using but by using these queries I get these results:
SELECT GETVARIABLE('SYSIBM.VERSION') FROM SYSIBM.SYSDUMMY1
DSN11015
select GETVARIABLE('SYSIBM.NEWFUN') AS COMPATIBILITY from sysibm.sysdummy1
COMPATIBILITY: V11
https://www.ibm.com/docs/en/db2/11.1?topic=functions-hash
https://www.ibm.com/docs/en/db2-for-zos/12?topic=sf-hash-crc32-hash-md5-hash-sha1-hash-sha256
You appear to be using Db2-v11 for Z/OS "DSN11015" in V11 compatibility mode.
This version lacks the scalar function HASH_SHA256 (and others) which is present in Db2-v12 for z/OS. That is the reason you get sqlcode -440 (no such function HASH...) with your Db2-for-z/os version.
At Version-11 , IBM documents a function ENCRYPT_TDES which may help you.
Background information on using this function.
You can write your own functions if you have the skills.

ERROR: syntax error at or near "FUNCTION" while db restoring

I have an error while restoring DB from dump. What does it mean?
ERROR: syntax error at or near "FUNCTION"
LINE 1: ...LETE ON public.currency_rate FOR EACH ROW EXECUTE FUNCTION p...
--
-- Name: currency_rate currency_rate_bt_delete; Type: TRIGGER; Schema: public; Owner: -
--
CREATE TRIGGER currency_rate_bt_delete
INSTEAD OF DELETE ON public.currency_rate
FOR EACH ROW
EXECUTE FUNCTION public.currency_rate_bt_delete();
The problem of your dump/restore is that your create dump with PostgreSQL v13 It generates dump you have shown.
But then you try to restore this dump on PostgreSQL v10 which does not understand that dump
You have to use PROCEDURE instead of FUNCTION before public.currency_rate_bt_delete()
Your trigger query should be like below:
CREATE TRIGGER currency_rate_bt_delete
INSTEAD OF DELETE ON public.currency_rate
FOR EACH ROW
EXECUTE PROCEDURE public.currency_rate_bt_delete();
NOTE : - This answer is limited to the error mentioned in the question.

Error DBLink from 10g to 12c

I have created a database link from my 10g database to 12c database. When I am trying to access table using 10g database it works fine. But when we try to execute the procedure from 10g database, it popups following error
SQL> exec popcust#abcd;
BEGIN popcust#abcd; END;
*
ERROR at line 1:
ORA-06550: line 1, column 7:
PLS-00201: identifier 'POPCUST#ABCD' must be declared
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
When we describe a procedure from 10g DB. it comes with like this.
SQL> desc popcust#abcd;
PROCEDURE st#abcd
Please can somebody help me

Postgresql update trigger not accepting When / OF condition

I am trying to write a trigger but getting syntax error:
CREATE TRIGGER archive_domain_trig
AFTER UPDATE OF is_rejected ON pending_domains
FOR EACH ROW
WHEN (new.is_rejected is True)
EXECUTE PROCEDURE archive_domain_fun();
ERROR: syntax error at or near "OF"
not only this but it is giving error on "when" condition also e.g.
CREATE TRIGGER archive_domain_trig
AFTER UPDATE ON pending_domains
FOR EACH ROW
WHEN (new.is_rejected is True)
EXECUTE PROCEDURE archive_domain_fun();
gives:
ERROR: syntax error at or near "WHEN".
select version();
"EnterpriseDB 8.3.0.116 on x86_64-unknown-linux-gnu, compiled by GCC gcc (GCC) 4.1.0"
This is almost similar to postgres example, can anyone tell me what am I doing wrong here?
I'd guess that EnterpriseDB 8.3 is the EnterpriseDB version of PostgreSQL 8.3. Conditional triggers (i.e. the WHEN option) are a new feature in PostgreSQL 9.0. Compare the CREATE TRIGGER documentation for 8.3 and 9.0.